Skip to content

Commit 835457b

Browse files
committed
update voice snippets
1 parent 71bc00d commit 835457b

19 files changed

+72
-101
lines changed

.env.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,8 @@ VOICE_CONFERENCE_NAME='VOICE_CONFERENCE_NAME'
9797
VOICE_RECORDING_URL='VOICE_RECORDING_URL'
9898
VOICE_ANSWER_URL='VOICE_ANSWER_URL'
9999
VOICE_CALL_ID='VOICE_CALL_ID'
100+
VOICE_STREAM_URL='VOICE_STREAM_URL'
101+
VOICE_DTMF_DIGITS='VOICE_DTMF_DIGITS'
102+
VOICE_TEXT='VOICE_TEXT'
103+
VOICE_LANGUAGE='VOICE_LANGUAGE'
104+
VOICE_NCCO_URL='VOICE_NCCO_URL'

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
import os
2-
from os.path import join, dirname
3-
from dotenv import load_dotenv
1+
from pprint import pprint
42
from fastapi import FastAPI, Body, Request
53
from vonage_voice import Input, NccoAction, Speech, Talk
64

7-
dotenv_path = join(dirname(__file__), '../.env')
8-
load_dotenv(dotenv_path)
9-
10-
VONAGE_VIRTUAL_NUMBER = os.environ.get('VONAGE_VIRTUAL_NUMBER')
11-
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12-
135
app = FastAPI()
146

157

168
@app.get('/webhooks/answer')
179
async def answer_call(request: Request):
1810
ncco: list[NccoAction] = [
19-
Talk(text=f'Please tell me something.'),
11+
Talk(text=f'Please say something'),
2012
Input(
2113
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'],
14+
speech=Speech(endOnSilence=1, language='en-US'),
15+
eventUrl=[str(request.base_url) + 'webhooks/asr'],
2816
),
2917
]
3018

@@ -34,9 +22,10 @@ async def answer_call(request: Request):
3422
@app.post('/webhooks/asr')
3523
async def answer_asr(data: dict = Body(...)):
3624
if data is not None and 'speech' in data:
25+
pprint(data)
3726
speech = data['speech']['results'][0]['text']
3827
return [
39-
Talk(text=f'Hello ,you said {speech}').model_dump(
28+
Talk(text=f'Hello, you said {speech}').model_dump(
4029
by_alias=True, exclude_none=True
4130
)
4231
]

voice/handle-user-input.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import os
2-
from os.path import join, dirname
3-
from dotenv import load_dotenv
1+
from pprint import pprint
42
from fastapi import FastAPI, Body, Request
5-
from vonage_voice import Input, NccoAction, Talk
6-
7-
dotenv_path = join(dirname(__file__), '../.env')
8-
load_dotenv(dotenv_path)
9-
10-
VONAGE_VIRTUAL_NUMBER = os.environ.get('VONAGE_VIRTUAL_NUMBER')
11-
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
3+
from vonage_voice import Dtmf, Input, NccoAction, Talk
124

135
app = FastAPI()
146

157

168
@app.get('/webhooks/answer')
179
async def answer_call(request: Request):
1810
ncco: list[NccoAction] = [
19-
Talk(text=f'Please enter a digit.'),
11+
Talk(text=f'Hello, please press any key to continue.'),
2012
Input(
2113
type=['dtmf'],
22-
maxDigits=1,
23-
eventUrl=[str(request.base_url) + '/webhooks/dtmf'],
14+
dtmf=Dtmf(timeOut=5, maxDigits=1),
15+
eventUrl=[str(request.base_url) + 'webhooks/dtmf'],
2416
),
2517
]
2618

@@ -29,8 +21,9 @@ async def answer_call(request: Request):
2921

3022
@app.post('/webhooks/dtmf')
3123
async def answer_dtmf(data: dict = Body(...)):
24+
pprint(data)
3225
return [
33-
Talk(text=f'Hello, you pressed {data['dtmf']}').model_dump(
26+
Talk(text=f'Hello, you pressed {data['dtmf']['digits']}').model_dump(
3427
by_alias=True, exclude_none=True
3528
)
3629
]

voice/make-an-outbound-call.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import os
32
from os.path import join, dirname
43
from pprint import pprint
@@ -9,8 +8,9 @@
98

109
VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
1110
VONAGE_PRIVATE_KEY = os.environ.get("VONAGE_PRIVATE_KEY")
12-
FROM_NUMBER = os.environ.get("FROM_NUMBER")
1311
VOICE_TO_NUMBER = os.environ.get("VOICE_TO_NUMBER")
12+
VONAGE_VIRTUAL_NUMBER = os.environ.get("VONAGE_VIRTUAL_NUMBER")
13+
VOICE_ANSWER_URL = os.environ.get("VOICE_ANSWER_URL")
1414

1515
from vonage import Auth, Vonage
1616
from vonage_voice import CreateCallRequest, Phone, ToPhone
@@ -24,11 +24,9 @@
2424

2525
response = client.voice.create_call(
2626
CreateCallRequest(
27-
answer_url=[
28-
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
29-
],
27+
answer_url=[VOICE_ANSWER_URL],
3028
to=[ToPhone(number=VOICE_TO_NUMBER)],
31-
from_=Phone(number=FROM_NUMBER),
29+
from_=Phone(number=VONAGE_VIRTUAL_NUMBER),
3230
)
3331
)
3432

voice/make-outbound-call-ncco.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
from pprint import pprint
32
import os
43
from os.path import join, dirname
@@ -25,7 +24,7 @@
2524

2625
response = client.voice.create_call(
2726
CreateCallRequest(
28-
ncco=[Talk(text='Hello world, this is a test call.', loop=10)],
27+
ncco=[Talk(text='This is a text to speech call from Vonage.')],
2928
to=[ToPhone(number=VOICE_TO_NUMBER)],
3029
from_=Phone(number=VONAGE_VIRTUAL_NUMBER),
3130
)

voice/mute-a-call.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
1010
VONAGE_PRIVATE_KEY = os.environ.get('VONAGE_PRIVATE_KEY')
11-
1211
VOICE_CALL_ID = os.environ.get('VOICE_CALL_ID')
1312

1413
from vonage import Auth, Vonage

voice/play-audio-stream-into-call.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
1010
VONAGE_PRIVATE_KEY = os.environ.get('VONAGE_PRIVATE_KEY')
11-
1211
VOICE_CALL_ID = os.environ.get('VOICE_CALL_ID')
12+
VOICE_STREAM_URL = os.environ.get('VOICE_STREAM_URL')
1313

1414
from vonage import Auth, Vonage
1515
from vonage_voice import AudioStreamOptions, CallMessage
@@ -24,9 +24,7 @@
2424

2525
response: CallMessage = client.voice.play_audio_into_call(
2626
VOICE_CALL_ID,
27-
audio_stream_options=AudioStreamOptions(
28-
stream_url=['https://example.com/ringtone.mp3']
29-
),
27+
audio_stream_options=AudioStreamOptions(stream_url=[VOICE_STREAM_URL]),
3028
)
3129

3230
pprint(response)

voice/play-dtmf-into-call.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
1010
VONAGE_PRIVATE_KEY = os.environ.get('VONAGE_PRIVATE_KEY')
11-
1211
VOICE_CALL_ID = os.environ.get('VOICE_CALL_ID')
12+
VOICE_DTMF_DIGITS = os.environ.get('VOICE_DTMF_DIGITS')
1313

1414
from vonage import Auth, Vonage
1515
from vonage_voice import CallMessage
@@ -23,7 +23,7 @@
2323
)
2424

2525
response: CallMessage = client.voice.play_dtmf_into_call(
26-
uuid=VOICE_CALL_ID, dtmf='1234p*#'
26+
uuid=VOICE_CALL_ID, dtmf=VOICE_DTMF_DIGITS
2727
)
2828

2929
pprint(response)

voice/play-tts-into-call.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
1010
VONAGE_PRIVATE_KEY = os.environ.get('VONAGE_PRIVATE_KEY')
11-
1211
VOICE_CALL_ID = os.environ.get('VOICE_CALL_ID')
13-
LANGUAGE = os.environ.get('LANGUAGE')
12+
VOICE_TEXT = os.environ.get('VOICE_TEXT')
13+
VOICE_LANGUAGE = os.environ.get('VOICE_LANGUAGE')
1414

1515
from vonage import Auth, Vonage
1616
from vonage_voice import CallMessage, TtsStreamOptions
@@ -25,7 +25,7 @@
2525

2626
response: CallMessage = client.voice.play_tts_into_call(
2727
uuid=VOICE_CALL_ID,
28-
tts_options=TtsStreamOptions(text='Hello from Vonage.', language=LANGUAGE),
28+
tts_options=TtsStreamOptions(text=VOICE_TEXT, language=VOICE_LANGUAGE),
2929
)
3030

3131
pprint(response)

voice/receive-an-inbound-call.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
app = FastAPI()
55

66

7-
@app.get('/answer')
7+
@app.get('/webhooks/answer')
88
async def answer_call(from_: str = Query(..., alias='from')):
9+
from_ = '-'.join(from_)
910
return [
1011
Talk(text=f'Thank you for calling from {from_}').model_dump(
1112
by_alias=True, exclude_none=True

0 commit comments

Comments
 (0)