send SMS
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import logging
|
||||
from twilio.rest import Client
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
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:
|
||||
logger.error("Twilio environment variables (TWILIO_SID, TWILIO_ACCOUNT_SECRET, TWILIO_PHONE_NUMBER) missing.")
|
||||
return False
|
||||
|
||||
if not to_phone:
|
||||
logger.warning("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 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
|
||||
)
|
||||
logger.info(f"SMS sent successfully to {to_phone}. Message SID: {message.sid}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to send SMS via Twilio to {to_phone}: {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user