-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
52 lines (41 loc) · 1.77 KB
/
Copy pathrun.py
File metadata and controls
52 lines (41 loc) · 1.77 KB
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
from flask import Flask, request, redirect
import twilio.twiml
import requests, os, json
app = Flask(__name__)
key = os.environ['GOOGLE_KEY']
@app.route("/", methods=['GET', 'POST'])
def lookup():
message_body = request.values.get('Body')
try:
url = "https://www.googleapis.com/civicinfo/v2/representatives"
querystring = {"key": key, "levels":["country","administrativeArea1"],
"roles":["legislatorUpperBody","legislatorLowerBody"], "address": message_body}
response = requests.request("GET", url, params=querystring)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(e)
message_response = "This is awkward. There was an error. Please enter your address again."
resp = twilio.twiml.Response()
resp.message(message_response)
return str(resp)
json_response = json.loads(response.text)
offices = json_response['offices']
officials = json_response['officials']
str_list=[]
str_list.append("Hi! You are represented by...\n")
for x in offices:
str_list.append("\n" + x["name"] + ": \n")
for y in x['officialIndices']:
str_list.append(officials[y]['name'] + " ")
for z in officials[y]['phones']:
str_list.append(z + "\n")
str_list.append("\nWant help calling your reps? Check out: http://echothroughthefog.cordeliadillon.com/post/153393286626/how-to-call-your-reps-when-you-have-social-anxiety")
message_response = ''.join(str_list)
resp = twilio.twiml.Response()
resp.message(message_response)
return str(resp)
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if port == 5000:
app.debug = True
app.run(host='0.0.0.0', port=port)