-
Notifications
You must be signed in to change notification settings - Fork 0
/
incoming.py
37 lines (30 loc) · 957 Bytes
/
incoming.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
from bottle import post, request, response, route, run
from twilio import twiml
from update import update
import re
class BadInput(Exception):
pass
@route('/')
def check():
return 'Working!'
@post('/twilio')
def inbound_sms():
inbound_message = request.forms.get('Body')
try:
allowable_patterns = [re.compile(r'(add) ((\S+ )*(\S+))'),re.compile(r'(delete) ([0-9]+)')]
operation = info = None
for p in allowable_patterns:
match = re.search(p, inbound_message)
if match:
operation = match.group(1)
info = match.group(2)
if not operation or not info:
raise BadInput
update(operation, info)
except BadInput:
twiml_response = twiml.Response()
twiml_response.message('Bad input. Try again.')
response.content_type = 'application/xml'
return str(twiml_response)
if __name__ == '__main__':
run(host='127.0.0.1', port=5000, reloader=True)