Skip to content

Commit 3159c5f

Browse files
committed
add final voice snippets
1 parent c3db853 commit 3159c5f

12 files changed

+163
-228
lines changed

.env.dist

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ CONFERENCE_NAME='NAME_OF_YOUR_CONFERENCE'
8181
YOUR_SECOND_NUMBER='YOUR_SECOND_NUMBER'
8282
RECORDING_URL='RECORDING_URL'
8383
CALL_UUID='CALL_UUID'
84+
LANGUAGE='en-US'
8485

8586
# Numbers
8687
COUNTRY_CODE='GB'

voice/connect-an-inbound-call.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
app = FastAPI()
1313

1414

15-
@app.get('/answer')
15+
@app.get('/webhooks/answer')
1616
async def inbound_call():
1717
ncco = [
1818
Connect(
+15-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
#!/usr/bin/env python3
2-
from flask import Flask, jsonify
1+
import os
32
from os.path import join, dirname
43
from dotenv import load_dotenv
5-
import os
4+
from fastapi import FastAPI
5+
from vonage_voice.models import Conversation, NccoAction, Talk
66

7-
app = Flask(__name__)
8-
9-
dotenv_path = join(dirname(__file__), "../.env")
7+
dotenv_path = join(dirname(__file__), '../.env')
108
load_dotenv(dotenv_path)
119

10+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
11+
YOUR_SECOND_NUMBER = os.environ.get('YOUR_SECOND_NUMBER')
1212
CONFERENCE_NAME = os.environ.get("CONFERENCE_NAME")
1313

14-
@app.route("/webhooks/answer")
15-
def answer_call():
16-
ncco = [
17-
{
18-
"action": "talk",
19-
"text": "Please wait while we connect you to the conference"
20-
},
21-
{
22-
"action": "conversation",
23-
"name": CONFERENCE_NAME
24-
}]
25-
return jsonify(ncco)
14+
app = FastAPI()
15+
2616

17+
@app.get('/webhooks/answer')
18+
async def answer_call():
19+
ncco: list[NccoAction] = [
20+
Talk(text="Please wait while we connect you to the conference"),
21+
Conversation(name=CONFERENCE_NAME),
22+
]
2723

28-
if __name__ == '__main__':
29-
app.run(port=3000)
24+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]

voice/handle-user-input-with-asr.py

+43-40
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
1-
#!/usr/bin/env python3
2-
from flask import Flask, request, jsonify
3-
4-
app = Flask(__name__)
5-
6-
7-
@app.route("/webhooks/answer", methods=["POST", "GET"])
8-
def answer_call():
9-
ncco = [
10-
{"action": "talk", "text": "Please, tell me something",},
11-
{
12-
"action": "input",
13-
"type": ["speech"],
14-
"eventUrl": [
15-
"{host}{endpoint}".format(
16-
host=request.host_url, endpoint="webhooks/asr"
17-
)
18-
],
19-
"speech": {
20-
"endOnSilence": 1,
21-
"language": "en-US",
22-
"uuid": [request.args.get("uuid")], # Change to request.json.get("uuid") if using POST-JSON webhook format
23-
},
24-
},
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from fastapi import FastAPI, Body, Request
5+
from vonage_voice.models import Input, NccoAction, Speech, Talk
6+
7+
dotenv_path = join(dirname(__file__), '../.env')
8+
load_dotenv(dotenv_path)
9+
10+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
11+
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12+
13+
app = FastAPI()
14+
15+
16+
@app.get('/webhooks/answer')
17+
async def answer_call(request: Request):
18+
ncco: list[NccoAction] = [
19+
Talk(text=f'Please tell me something.'),
20+
Input(
21+
type=['speech'],
22+
speech=Speech(
23+
endOnSilence=1,
24+
language='en-US',
25+
uuid=[request.query_params.get('uuid')],
26+
),
27+
eventUrl=[str(request.base_url) + '/webhooks/asr'],
28+
),
2529
]
26-
return jsonify(ncco)
2730

28-
29-
@app.route("/webhooks/asr", methods=["POST", "GET"])
30-
def answer_asr():
31-
body = request.get_json()
32-
if body is not None and "speech" in body:
33-
speech = body["speech"]["results"][0]["text"]
34-
ncco = [
35-
{"action": "talk", "text": "Hello ,you said {speech}".format(speech=speech)}
36-
]
37-
else:
38-
ncco = [{"action": "talk", "text": "Sorry, i don't undertand. Bye"}]
39-
40-
return jsonify(ncco)
31+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
4132

4233

43-
if __name__ == "__main__":
44-
app.run(port=3000)
34+
@app.post('/webhooks/asr')
35+
async def answer_asr(data: dict = Body(...)):
36+
if data is not None and 'speech' in data:
37+
speech = data['speech']['results'][0]['text']
38+
return [
39+
Talk(text=f'Hello ,you said {speech}').model_dump(
40+
by_alias=True, exclude_none=True
41+
)
42+
]
43+
return [
44+
Talk(text=f'Sorry, I didn\'t understand your input.').model_dump(
45+
by_alias=True, exclude_none=True
46+
)
47+
]

voice/handle-user-input.py

+33-39
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
1-
#!/usr/bin/env python3
2-
from pprint import pprint
3-
from flask import Flask, request, jsonify
4-
5-
app = Flask(__name__)
6-
7-
8-
@app.route("/webhooks/answer")
9-
def answer_call():
10-
for param_key, param_value in request.args.items():
11-
print("{}: {}".format(param_key, param_value))
12-
input_webhook_url = request.url_root + "webhooks/dtmf"
13-
ncco = [
14-
{
15-
"action": "talk",
16-
"text": "Hello, please press any key to continue."
17-
},
18-
{
19-
"action": "input",
20-
"type": ["dtmf"],
21-
"maxDigits": 1,
22-
"eventUrl": [input_webhook_url]
23-
}
24-
]
25-
return jsonify(ncco)
26-
27-
28-
@app.route("/webhooks/dtmf", methods=['POST'])
29-
def dtmf():
30-
data = request.get_json()
31-
pprint(data)
32-
ncco = [
33-
{
34-
"action": "talk",
35-
"text": "You pressed {}, goodbye".format(data['dtmf'])
36-
}
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from fastapi import FastAPI, Body, Request
5+
from vonage_voice.models import Input, NccoAction, Talk
6+
7+
dotenv_path = join(dirname(__file__), '../.env')
8+
load_dotenv(dotenv_path)
9+
10+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
11+
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12+
13+
app = FastAPI()
14+
15+
16+
@app.get('/webhooks/answer')
17+
async def answer_call(request: Request):
18+
ncco: list[NccoAction] = [
19+
Talk(text=f'Please enter a digit.'),
20+
Input(
21+
type=['dtmf'],
22+
maxDigits=1,
23+
eventUrl=[str(request.base_url) + '/webhooks/dtmf'],
24+
),
3725
]
38-
return jsonify(ncco)
3926

27+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
4028

41-
if __name__ == '__main__':
42-
app.run(port=3000)
29+
30+
@app.post('/webhooks/dtmf')
31+
async def answer_dtmf(data: dict = Body(...)):
32+
return [
33+
Talk(text=f'Hello, you pressed {data['dtmf']}').model_dump(
34+
by_alias=True, exclude_none=True
35+
)
36+
]

voice/join-outbound-calls.py

-31
This file was deleted.

voice/play-tts-into-call.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313

1414
CALL_UUID = os.environ.get('CALL_UUID')
15+
LANGUAGE = os.environ.get('LANGUAGE')
1516

1617
from vonage import Auth, Vonage
1718
from vonage_voice.models import CallMessage, TtsStreamOptions
@@ -25,7 +26,8 @@
2526
)
2627

2728
response: CallMessage = client.voice.play_tts_into_call(
28-
uuid=CALL_UUID, tts_options=TtsStreamOptions(text='Hello from Vonage.')
29+
uuid=CALL_UUID,
30+
tts_options=TtsStreamOptions(text='Hello from Vonage.', language=LANGUAGE),
2931
)
3032

3133
pprint(response)

voice/record-a-call.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def inbound_call():
2828
),
2929
]
3030

31-
return [step.model_dump(by_alias=True, exclude_none=True) for step in ncco]
31+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
3232

3333

3434
@app.post('/webhooks/recordings')

voice/record-a-message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async def answer_call(request: Request):
1818
Talk(text='Thank you for your message.'),
1919
]
2020

21-
return [step.model_dump(by_alias=True, exclude_none=True) for step in ncco]
21+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
2222

2323

2424
@app.post('/webhooks/recordings')

voice/track-ncco.py

+36-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
1-
from flask import Flask, request, jsonify
2-
3-
app = Flask(__name__)
4-
5-
6-
@app.route("/webhooks/answer")
7-
def answer_call():
8-
ncco = [{
9-
"action": "talk",
10-
"text": "Thanks for calling the notification line"
11-
},
12-
{
13-
"action": "notify",
14-
"payload": {
15-
"foo": "bar"
16-
},
17-
"eventUrl": [
18-
"{url_root}webhooks/notification".format(url_root=request.url_root)
19-
]
20-
},
21-
{
22-
"action": "talk",
23-
"text": "You will never hear me as the notification URL will return an NCCO "
24-
}]
25-
return jsonify(ncco)
26-
27-
28-
@app.route("/webhooks/notification", methods=['POST'])
29-
def notification():
30-
ncco = [{
31-
"action": "talk",
32-
"text": "Your notification has been received, loud and clear"
33-
}]
34-
return jsonify(ncco)
35-
36-
37-
@app.route("/webhooks/event", methods=['POST'])
38-
def event():
39-
return "OK"
40-
41-
42-
if __name__ == '__main__':
43-
app.run(host="localhost", port=3000)
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from fastapi import FastAPI, Body, Request
5+
from vonage_voice.models import NccoAction, Notify, Talk
6+
7+
dotenv_path = join(dirname(__file__), '../.env')
8+
load_dotenv(dotenv_path)
9+
10+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
11+
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12+
13+
app = FastAPI()
14+
15+
16+
@app.get('/webhooks/answer')
17+
async def inbound_call(request: Request):
18+
ncco: list[NccoAction] = [
19+
Talk(text=f'Thanks for calling the notification line.'),
20+
Notify(
21+
payload={"foo": "bar"},
22+
eventUrl=[str(request.base_url) + '/webhooks/notification'],
23+
),
24+
Talk(text=f'You will never hear me as the notification URL will return an NCCO.'),
25+
]
26+
27+
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
28+
29+
30+
@app.post('/webhooks/notification')
31+
async def on_notification():
32+
return [
33+
Talk(text=f'Your notification has been received, loud and clear').model_dump(
34+
by_alias=True, exclude_none=True
35+
)
36+
]

0 commit comments

Comments
 (0)