-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_app.py
More file actions
71 lines (55 loc) · 2.03 KB
/
webhook_app.py
File metadata and controls
71 lines (55 loc) · 2.03 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
"""Speech Echo - Webhook (HTTP) example.
Listens for speech input and echoes it back to the caller.
Demonstrates the gather verb with speech recognition and actionHook handling.
Usage:
python webhook_app.py
"""
from aiohttp import web
from jambonz_sdk.webhook import WebhookResponse
async def handle_incoming(request: web.Request) -> web.Response:
"""Handle incoming call - prompt for speech."""
jambonz = WebhookResponse()
jambonz.pause(length=1).gather(
input=["speech"],
actionHook="/echo",
timeout=10,
say={"text": "Please say something and I will echo it back to you."},
)
return web.json_response(jambonz.to_json())
async def handle_echo(request: web.Request) -> web.Response:
"""Handle gather result - echo speech back."""
body = await request.json()
jambonz = WebhookResponse()
reason = body.get("reason", "")
if reason == "speechDetected":
transcript = (
body.get("speech", {})
.get("alternatives", [{}])[0]
.get("transcript", "nothing")
)
jambonz.say(text=f"You said: {transcript}.").gather(
input=["speech"],
actionHook="/echo",
timeout=10,
say={"text": "Please say something else."},
)
elif reason == "timeout":
jambonz.gather(
input=["speech"],
actionHook="/echo",
timeout=10,
say={"text": "Are you still there? I didn't hear anything."},
)
else:
jambonz.say(text="Goodbye.").hangup()
return web.json_response(jambonz.to_json())
async def handle_call_status(request: web.Request) -> web.Response:
body = await request.json()
print(f"Call {body.get('call_sid')} status: {body.get('call_status')}")
return web.Response(status=200)
app = web.Application()
app.router.add_post("/incoming", handle_incoming)
app.router.add_post("/echo", handle_echo)
app.router.add_post("/call-status", handle_call_status)
if __name__ == "__main__":
web.run_app(app, port=3000)