-
Notifications
You must be signed in to change notification settings - Fork 3
/
dial-a-carol.py
78 lines (69 loc) · 2.02 KB
/
dial-a-carol.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
#!/usr/bin/env python3
from flask import Flask, request, jsonify
from pprint import pprint
app = Flask(__name__)
base_url = 'https://raw.githubusercontent.com/tbedford/git-testing-repo/master/tunes/xmas/'
# Tunes courtesy http://www.freexmasmp3.com/
tunes = [
["Little Town of Bethlehem", "bethlem-jazz.mp3"],
["Ding Dong Merrily", "ding-dong-merrily.mp3"],
["First Noel", "first-noel-r-and-b.mp3"],
["Jingle Bells", "jingle-bells-country.mp3"],
["Silent Night", "silent-night-piano.mp3"],
["Twelve Days of Christmas", "twelve-days-funk.mp3"]
]
# Build options menu
menu = "Welcome to dial a Christmas carol. You can choose from the following cheesy carols."
i = 1
for t in tunes:
menu = menu + " Option " + str(i) + " is " + t[0] +"."
i = i + 1
menu = menu + " Please make your selection now."
@app.route("/webhooks/answer")
def answer_call():
params = request.args
input_webhook_url = request.url_root + "webhooks/dtmf"
ncco = [
{
"action": "talk",
"bargeIn": "true",
"text": menu
},
{
"action": "input",
"maxDigits": 1,
"timeOut": 5,
"eventUrl": [input_webhook_url]
}
]
return jsonify(ncco)
@app.route("/webhooks/dtmf", methods=['POST'])
def dtmf_webhook():
data = request.get_json()
selection = data['dtmf']
if selection == "":
selection = "1"
index = int(selection)-1
if index < 0 or index > len(tunes)-1:
index = 0
carol_url = base_url + tunes[index][1]
print(tunes[index][1])
msg = "Playing Christmas carol " + str(index+1)
ncco = [
{
"action": "talk",
"text": msg
},
{
"action": "stream",
"streamUrl": [carol_url]
}
]
return jsonify(ncco)
@app.route("/webhooks/event", methods=['POST'])
def events():
data = request.get_json()
pprint(data)
return ("OK")
if __name__ == '__main__':
app.run(port=3000)