-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
101 lines (78 loc) · 3.02 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
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
from urllib.parse import parse_qs
import structlog
from chalice import CORSConfig
from chalice.app import BadRequestError, Chalice, Response
from chalicelib.errors import LocationNotFound
from chalicelib.geocode import geolocate
from chalicelib.responses import response_type_from_place_type
from chalicelib.twilio import twilio_response
logger = structlog.get_logger()
app = Chalice(app_name='ak_land_aws')
HELLO_RESPONSE = "Hello. Please tell me the town and state you are in. For example, 'Anchorage, AK'"
def get_query(raw_body: bytes) -> str:
if not raw_body:
raise BadRequestError("Requests must post a 'Body' parameter")
str_body = raw_body.decode('utf-8')
str_body = parse_qs(str_body)
body = str_body.get('Body', [])
if len(body):
return body[0].strip()
return ''
def process_query(query: str) -> str:
log = logger.bind(body=query)
greetings = {'hello', 'hi', 'help'}
if not query or query.lower() in greetings:
log.info('no_location')
return HELLO_RESPONSE
elif len(query) < 3:
log.info('short_query')
return "Hmm, that seems a little vague. Try sending a city and state such as 'Anchorage, AK'"
else:
try:
location = geolocate(query)
except LocationNotFound as e:
log.info('location_not_found')
return str(e)
place_type = location['place_type'][0]
response_class = response_type_from_place_type(place_type)
ret_object = response_class(query, location)
log = logger.bind(**ret_object.to_dict())
log.info('success')
return str(ret_object)
@app.route('/', methods=['POST'], content_types=['application/x-www-form-urlencoded'])
def index_post():
'''
This is the route Twilio should access, e.g. from texting or from the
Facebook Messenger bot.
The user input should be in a www-form-encoded `body`.
It will respond with TwilML xml output.
'''
query = get_query(app.current_request.raw_body)
resp_text = process_query(query)
return Response(body=twilio_response(resp_text),
status_code=200,
headers={'Content-Type': 'application/xml'})
@app.route('/', methods=['GET'])
def index_empty_get():
return _GET_response(HELLO_RESPONSE)
CORS_CONFIG = CORSConfig(
allow_origin='https://land.codeforanchorage.org',
allow_headers=['X-Special-Header'],
max_age=600,
expose_headers=['X-Special-Header'],
allow_credentials=True
)
@app.route('/{query}', cors=CORS_CONFIG, methods=['GET'])
def index_get(query: str):
'''
Non-Twilio users can access this route with a GET request
and will receive a string response.
This for instance is what the land.codeforanchorage.org website
uses to get the response.
'''
resp_text = process_query(query)
return _GET_response(resp_text)
def _GET_response(resp_text: str) -> Response:
return Response(body=resp_text,
status_code=200,
headers={'Content-Type': 'application/json'})