-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
40 lines (34 loc) · 1.48 KB
/
app.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
from os import environ as env
from chalice import Chalice, Response
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException
#Twilio Config
ACCOUNT_SID = env.get('ACCOUNT_SID')
AUTH_TOKEN = env.get('AUTH_TOKEN')
FROM_NUMBER = env.get('FROM_NUMBER')
TO_NUMBER =env.get('TO_NUMBER')
app = Chalice(app_name='sms-trigger')
# Create a Twilio client using account_sid and auth token
tw_client = Client(ACCOUNT_SID, AUTH_TOKEN)
@app.route('/service/sms/send', methods=['POST'])
def send_sms():
request_body = app.current_request.json_body
if request_body:
try:
msg = tw_client.message.create(
from_ = FROM_NUMBER,
body = request_body['msg'],
to = TO_NUMBER)
if msg.sid:
return Response(status_code=200,
headers={'content-type':'application/json'},
body={'status':'success', 'data':msg.sid, 'message':'SMS successfully sent'})
else:
return Response(status_code=201,
headers={'content-type':'application/json'},
body={'status':'failure','message':'Please try again'})
except TwilioRestException as exc:
return Response(status_code=400,
headers={'content-type':'application/json'},
body={'status':'failure',
'message':exc.msg})