-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
80 lines (60 loc) · 1.93 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
from flask import Flask, render_template
from flask_sock import Sock
import json
from great_things import audio
app = Flask(__name__)
app.config["SOCK_SERVER_OPTIONS"] = {"ping_interval": 25}
sock = Sock(app)
def log(msg, *args):
print(msg, *args)
@app.route("/twiml", methods=["GET", "POST"])
def index():
return render_template("streams.xml")
@sock.route("/")
def echo(ws):
log("Connection Accepted!")
played_tone = False
count = 0
stream_sid = ""
while True:
message = ws.receive()
if message is None:
log("No message receieved!")
continue
data = json.loads(message)
if data["event"] == "connected":
log(message)
if data["event"] == "start":
stream_sid = data["streamSid"]
log(message)
if data["event"] == "dtmf":
log(message)
if data["event"] == "media":
if int(data["media"]["chunk"]) >= 100 and not played_tone:
send_to_twilio = {
"event": "media",
"streamSid": stream_sid,
"media": {"payload": audio},
}
mark_message = {
"event": "mark",
"streamSid": stream_sid,
"mark": {
"name": "This is my mark message!"
}
}
ws.send(json.dumps(send_to_twilio))
log(f"Sending the following message to Twilio: {mark_message}")
ws.send(json.dumps(mark_message))
played_tone = True
continue
log(message)
if data["event"] == "mark":
log("We got a mark message back from Twilio!")
log(message)
if data["event"] == "stop":
log(message)
ws.close()
break
if __name__ == "__main__":
app.run()