-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (25 loc) · 972 Bytes
/
utils.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
# Standard library import
import logging
# Third-party imports
from twilio.rest import Client
from decouple import config
# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = config("TWILIO_ACCOUNT_SID")
auth_token = config("TWILIO_AUTH_TOKEN")
client = Client(account_sid, auth_token)
twilio_number = config('TWILIO_NUMBER')
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Sending message logic through Twilio Messaging API
def send_message(to_number, body_text):
try:
message = client.messages.create(
from_=f"whatsapp:{twilio_number}",
body=body_text,
to=f"whatsapp:{to_number}"
)
logger.info(f"Message sent to {to_number}: {message.body}")
except Exception as e:
logger.error(f"Error sending message to {to_number}: {e}")