-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
108 lines (84 loc) · 2.86 KB
/
controller.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from app import app, search
from flask import request, redirect, session
import twilio.twiml
import pdb
SECRET_KEY = 'a secret key'
callers = {}
addresses = {}
def parseInitialResponse(body, from_number):
resp = twilio.twiml.Response()
initial_response = int(body)
if initial_response == 1:
callers[from_number] = 'shelter'
resp.message("Great, enter your current street address.")
elif initial_response == 2:
callers[from_number] = 'meal'
resp.message("Great, enter your current street address.")
else:
callers[from_number] = 'nothing'
resp.message("Invalid Response. Press 1 for the nearest shelter, press 2 for a meal.")
return str(resp)
def getShelter(address, from_number):
resp = twilio.twiml.Response()
request = search.get_closest_service('shelter', address)
status = request[1]
messsage = search.get_details(request)
if status:
callers[from_number] = 'shelter_address'
addresses[from_number] = address
else:
callers[from_number] = 'init'
resp.message("Press 1 for the nearest shelter, press 2 for a meal.")
resp.message(messsage)
return str(resp)
def getMeal(address, from_number):
resp = twilio.twiml.Response()
callers[from_number] = 'meal_address'
request = search.get_closest_service('food', address)
status = request[1]
messsage = search.get_details(request)
if status:
callers[from_number] = 'food_address'
addresses[from_number] = address
else:
callers[from_number] = 'init'
resp.message("Press 1 for the nearest shelter, press 2 for a meal.")
resp.message(messsage)
return str(resp)
def getShelterAddress(address, from_number):
resp = twilio.twiml.Response()
request = search.get_closest_service('shelter', addresses[from_number])
messsage = search.get_directions(request)
callers[from_number] = 'nothing'
resp.message(messsage)
return str(resp)
def getFoodAddress(address, from_number):
resp = twilio.twiml.Response()
request = search.get_closest_service('food', addresses[from_number])
messsage = search.get_directions(request)
callers[from_number] = 'nothing'
resp.message(messsage)
return str(resp)
options = {
'init' : parseInitialResponse,
'shelter' : getShelter,
'meal' : getMeal,
'shelter_address': getShelterAddress,
'food_address': getFoodAddress
}
@app.route("/text_messaging", methods=['GET', 'POST'])
def index():
resp = twilio.twiml.Response()
from_number = request.values.get('From')
if from_number in callers:
state = callers[from_number]
if state in options:
body = request.values.get('Body')
return options[state](body, from_number)
else:
callers[from_number] = 'init'
resp.message("Press 1 for the nearest shelter, press 2 for a meal.")
else:
callers[from_number] = 'init'
resp.message("Welcome to Text2Help. Press 1 for the nearest shelter, press 2 for a meal.")
return str(resp)