-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.py
46 lines (36 loc) · 1.65 KB
/
messenger.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
from twilio.rest import Client
from twilio.twiml.messaging_response import MessagingResponse
import os
class Messenger:
def __init__(self, db) -> None:
self.client = Client(os.environ.get("TWILIO_ACCOUNT_SID"),
os.environ.get("TWILIO_AUTH_TOKEN"))
self.from_number = os.environ.get("TWILIO_PHONE_NUMBER")
self.db = db
def send_message(self, to_number: str, message: str) -> None:
"""Sends a message to a phone number.
This function will be called by the API to send messages to subscribers.
"""
self.client.messages.create(to=to_number,
from_=self.from_number,
body=message,
messaging_service_sid=os.environ.get("TWILIO_MESSAGING_SERVICE_SID")
)
def subscribe(self, to_number: str) -> None:
"""Subscribes a phone number to the messaging service.
This will be the first function checked in the /sms route of the API.
"""
# Subscribe a phone number to a messaging service
self.client.messaging.services(os.environ.get("TWILIO_MESSAGING_SERVICE_SID")) \
.phone_numbers(to_number).create()
def unsubscribe(self, to_number: str) -> None:
"""Unsubscribes a phone number from the messaging service.
This will be the second function checked in the /sms route of the API.
"""
# Unsubscribe a phone number from a messaging service
self.client.messaging.services(os.environ.get("TWILIO_MESSAGING_SERVICE_SID")) \
.phone_numbers(to_number).delete()
def handle_message(self, request):
"""
"""
pass