Files
Smartwave/cloud/APIs/twilio.py
T
Ninluc 3ba65f0106
Build, push image, and notify Watchtower / build-image (push) Successful in 38s
Build, push image, and notify Watchtower / notify (push) Successful in 13s
Removed logging
2026-08-17 16:07:40 +02:00

55 lines
2.0 KiB
Python

import os
from twilio.rest import Client
def send_alert_sms(to_phone: str, room_title: str, room_link: str = None, client_email: str = None) -> bool:
"""
Sends an SMS alert using Twilio notifying the client of an issue and support room creation.
"""
account_sid = os.environ.get("TWILIO_SID")
auth_token = os.environ.get("TWILIO_ACCOUNT_SECRET")
from_phone = os.environ.get("TWILIO_PHONE_NUMBER") # Requires your Twilio sender phone number
if not account_sid or not auth_token or not from_phone:
print("Twilio environment variables (TWILIO_SID, TWILIO_ACCOUNT_SECRET, TWILIO_PHONE_NUMBER) missing.")
return False
if not to_phone:
print("No recipient phone number provided for SMS.")
return False
# --- PRINT LINK/INFO BEFORE SENDING ---
print("\n" + "="*50)
print(f"[Twilio] Preparing SMS for: {to_phone}")
print(f"[Twilio] Room Title: {room_title}")
if room_link:
print(f"[Twilio] Room Link: {room_link}")
else:
print(f"[Twilio] Link: Invitation email sent to {client_email or 'client inbox'}")
print("="*50 + "\n")
# --- BUILD SMS CONTENT ---
body = (
f"Alert: A technical issue occurred with your SmartWave equipment/microwave. "
f"A support chat room '{room_title}' has been created to assist you.\n\n"
)
if room_link:
body += f"Join chat room: {room_link}\n"
if client_email:
body += f"An invitation link has also been sent to your email ({client_email})."
else:
body += "An invitation link has been sent to your inbox."
try:
client = Client(account_sid, auth_token)
message = client.messages.create(
body=body,
from_=from_phone,
to=to_phone
)
print(f"SMS sent successfully to {to_phone}. Message SID: {message.sid}")
return True
except Exception as e:
print(f"Failed to send SMS via Twilio to {to_phone}: {e}")
return False