47 lines
1.7 KiB
Python
47 lines
1.7 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
|
|
|
|
# --- 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."
|
|
|
|
print(f"Sending SMS to {to_phone} : \"{body}\"")
|
|
|
|
try:
|
|
client = Client(account_sid, auth_token)
|
|
message = client.messages.create(
|
|
to="+32492857876",
|
|
from_="+4915888620339",
|
|
body="sms_account_alerts",
|
|
)
|
|
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 |