-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (63 loc) · 2.29 KB
/
Copy pathapp.py
File metadata and controls
83 lines (63 loc) · 2.29 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
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
import os
import json
from flask import Flask, request
import twilio.twiml
from twilio.rest import TwilioRestClient
app = Flask(__name__)
# Retrieve environment variables set by Heroku
account_sid = os.environ['TWILIO_SID']
auth_token = os.environ['TWILIO_TOKEN']
message_target = os.environ['SMS_TO']
cid = os.environ['SMS_FROM']
@app.route("/", methods=['POST'])
def respond():
from_number = request.values.get('From', None)
# Extract the Next Caller AddOn results
try:
addons = json.loads(request.values.get('AddOns', '{}'))
next_caller = addons['results']['nextcaller_advanced_caller_id']
except KeyError:
next_caller = None
resp = twilio.twiml.Response()
if not next_caller:
print('Next Caller AddOn not enabled')
resp.hangup()
return str(resp)
elif next_caller['status'] != 'successful':
print 'Next Caller lookup failed'
resp.hangup()
return str(resp)
records = next_caller['result']['records']
if not records:
print 'No Next Caller match found'
resp.hangup()
return str(resp)
caller = records[0]
fname = caller.get('first_name')
name = caller.get('name') or 'Anonymous'
gender = caller.get('gender') or'Unknown Gender'
marital_status = caller.get('marital_status') or '?'
phones = caller.get('phone')
if phones:
linetype = phones[0].get('line_type')
else:
linetype = None
# Greet the caller by name and tell them the type of phone they're
# calling form (mobile or landline).
greet = 'Hi, anonymous caller!'
if name:
greet = 'Hi, {}!'.format(fname)
if linetype:
greet = greet + ' You are calling from a {} phone.'.format(linetype)
resp.say(greet)
# Send an SMS to the configured address with info about the caller
body = 'Call from {}: {} ({} / {}).'.format(from_number, name,
marital_status, gender)
client = TwilioRestClient(account_sid, auth_token)
client.messages.create(to=message_target, from_=cid, body=body)
return str(resp)
if __name__ == "__main__":
# PORT is set by Heroku
port = int(os.environ.get('PORT', 8080))
print "starting on port {}".format(port)
app.run(debug=True, host='0.0.0.0', port=port)