forked from codeforamerica/sheltraustin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMSHandler.py
30 lines (24 loc) · 925 Bytes
/
SMSHandler.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
from twilio.rest import TwilioRestClient
from twilio.rest import *
from twilio import TwilioRestException
import sys
import os
class SMSHandler():
__acc_sid = (os.environ.get("TWILIO_ID"))
__auth_token = (os.environ.get("TWILIO_KEY"))
__from_number = "+15128618876"
### recipient phone number is a string with the country code in front. e.g. +12345678900 for America
def sendSMS(self, recipient, msgbody):
if (len(msgbody) >= 160):
return {"error":"Message must be less that 160 characters!"}
try:
client = TwilioRestClient(self.__acc_sid, self.__auth_token)
message = client.sms.messages.create(to=recipient, from_=self.__from_number, body=msgbody)
return {}
except TwilioRestException:
cla, exc, trbk = sys.exc_info()
try:
excArgs = exc.__dict__["args"]
except KeyError:
excArgs = "Unable to send SMS. Please check your phone number and try again"
return {"error" : excArgs}