-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsms_outbound.py
47 lines (36 loc) · 1.65 KB
/
sms_outbound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
from utils import get_env_var, get_logger
TWILIO_ACCOUNT_SID = get_env_var("TWILIO_ACCOUNT_SID", strict=True)
TWILIO_AUTH_TOKEN = get_env_var("TWILIO_AUTH_TOKEN", strict=True)
TWILIO_SERVICE_SID = get_env_var("TWILIO_SERVICE_SID", strict=True)
TWILIO_PHONE_NUMBER = get_env_var("TWILIO_PHONE_NUMBER", strict=True)
logger = get_logger(__name__)
def send_with_twilio(phone_number: str, message: str) -> bool:
"""
Sends a message using Twilio to a specified phone number.
Args:
phone_number (str): The recipient's phone number in E.164 format (e.g., +237123456789).
message (str): The content to be sent to the specified phone number.
Returns:
bool: True if the message was sent successfully, False otherwise.
"""
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
try:
message_response = client.messages.create(
body=message, from_=TWILIO_PHONE_NUMBER, to=phone_number
)
status = message_response.status
if status in ("accepted", "pending", "queued"):
logger.info("Message sent successfully.")
return True
logger.error("Failed to send message. Twilio status: %s", status)
return True
except TwilioRestException as exc:
logger.error("Error sending message with Twilio: %s", exc)
return False