Skip to content

Commit 835457b

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

19 files changed

+72
-101
lines changed

Diff for: .env.dist

+5
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'

Diff for: voice/handle-user-input-with-asr.py

+6-17
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
]

Diff for: voice/handle-user-input.py

+7-14
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
]

Diff for: voice/make-an-outbound-call.py

+4-6
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

Diff for: voice/make-outbound-call-ncco.py

+1-2
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
)

Diff for: voice/mute-a-call.py

-1
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

Diff for: voice/play-audio-stream-into-call.py

+2-4
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)

Diff for: voice/play-dtmf-into-call.py

+2-2
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)

Diff for: voice/play-tts-into-call.py

+3-3
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)

Diff for: voice/receive-an-inbound-call.py

+2-1
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

Diff for: voice/record-a-call-with-split-audio.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,27 @@
33
from dotenv import load_dotenv
44
from fastapi import FastAPI, Body
55
from pprint import pprint
6-
from vonage_voice import Connect, NccoAction, PhoneEndpoint, Record, Talk
6+
from vonage_voice import Connect, NccoAction, PhoneEndpoint, Record
77

88
dotenv_path = join(dirname(__file__), '../.env')
99
load_dotenv(dotenv_path)
1010

1111
VONAGE_VIRTUAL_NUMBER = os.environ.get('VONAGE_VIRTUAL_NUMBER')
12-
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12+
VOICE_TO_NUMBER = os.environ.get('VOICE_TO_NUMBER')
1313

1414
app = FastAPI()
1515

1616

1717
@app.get('/webhooks/answer')
1818
async def inbound_call():
1919
ncco: list[NccoAction] = [
20-
Talk(
21-
text=f'Hi, we will shortly forward your call. This call is recorded for quality assurance purposes.'
22-
),
2320
Record(
2421
split='conversation',
2522
channels=2,
2623
eventUrl=['https://demo.ngrok.io/webhooks/recordings'],
2724
),
2825
Connect(
29-
endpoint=[PhoneEndpoint(number=RECIPIENT_NUMBER)],
30-
from_=VONAGE_VIRTUAL_NUMBER,
31-
eventUrl=['https://demo.ngrok.io/webhooks/event'],
26+
from_=VONAGE_VIRTUAL_NUMBER, endpoint=[PhoneEndpoint(number=VOICE_TO_NUMBER)]
3227
),
3328
]
3429

Diff for: voice/record-a-call.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33
from dotenv import load_dotenv
44
from fastapi import FastAPI, Body
55
from pprint import pprint
6-
from vonage_voice import Connect, NccoAction, PhoneEndpoint, Record, Talk
6+
from vonage_voice import Connect, NccoAction, PhoneEndpoint, Record
77

88
dotenv_path = join(dirname(__file__), '../.env')
99
load_dotenv(dotenv_path)
1010

1111
VONAGE_VIRTUAL_NUMBER = os.environ.get('VONAGE_VIRTUAL_NUMBER')
12-
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
12+
VOICE_TO_NUMBER = os.environ.get('VOICE_TO_NUMBER')
1313

1414
app = FastAPI()
1515

1616

1717
@app.get('/webhooks/answer')
1818
async def inbound_call():
1919
ncco: list[NccoAction] = [
20-
Talk(
21-
text=f'Hi, we will shortly forward your call. This call is recorded for quality assurance purposes.'
22-
),
2320
Record(eventUrl=['https://demo.ngrok.io/webhooks/recordings']),
2421
Connect(
25-
endpoint=[PhoneEndpoint(number=RECIPIENT_NUMBER)],
26-
from_=VONAGE_VIRTUAL_NUMBER,
27-
eventUrl=['https://demo.ngrok.io/webhooks/event'],
22+
from_=VONAGE_VIRTUAL_NUMBER, endpoint=[PhoneEndpoint(number=VOICE_TO_NUMBER)]
2823
),
2924
]
3025

Diff for: voice/record-a-conversation.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
14
from fastapi import FastAPI, Body
25
from pprint import pprint
6+
from vonage_voice import Conversation
7+
8+
dotenv_path = join(dirname(__file__), '../.env')
9+
load_dotenv(dotenv_path)
10+
11+
VOICE_CONFERENCE_NAME = os.environ.get('VOICE_CONFERENCE_NAME')
312

413
app = FastAPI()
514

615

716
@app.get('/webhooks/answer')
817
async def answer_call():
918
ncco = [
10-
{
11-
"action": "conversation",
12-
"name": "CONF_NAME",
13-
"record": "true",
14-
"eventMethod": "POST",
15-
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"],
16-
}
19+
Conversation(
20+
name=VOICE_CONFERENCE_NAME,
21+
record=True,
22+
eventMethod='POST',
23+
eventUrl=['https://demo.ngrok.io/webhooks/recordings'],
24+
)
1725
]
1826

1927
return ncco

Diff for: voice/record-a-message.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
async def answer_call(request: Request):
1010
print(request.base_url)
1111
ncco: list[NccoAction] = [
12-
Talk(text='Please leave a message after the tone, then press the hash key.'),
12+
Talk(
13+
text='Please leave a message after the tone, then press #. We will get back to you as soon as we can.'
14+
),
1315
Record(
16+
endOnSilence=3,
1417
endOnKey='#',
1518
beepStart=True,
16-
eventUrl=[str(request.base_url) + '/webhooks/recordings'],
19+
eventUrl=[str(request.base_url) + 'webhooks/recordings'],
1720
),
18-
Talk(text='Thank you for your message.'),
21+
Talk(text='Thank you for your message. Goodbye.'),
1922
]
2023

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

Diff for: voice/retrieve-info-for-a-call.py

-1
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

Diff for: voice/retrieve-info-for-all-calls.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
)
2121
)
2222

23-
24-
NOW = datetime.now(timezone.utc)
25-
DATE_END = NOW.replace(microsecond=0).isoformat()
26-
DATE_START = (NOW - timedelta(hours=24, minutes=00)).replace(microsecond=0).isoformat()
23+
now = datetime.now(timezone.utc)
24+
date_end = now.strftime('%Y-%m-%dT%H:%M:%SZ')
25+
start = now - timedelta(hours=24)
26+
date_start = start.strftime('%Y-%m-%dT%H:%M:%SZ')
2727

2828
calls, _ = client.voice.list_calls(
29-
ListCallsFilter(date_start=DATE_START, date_end=DATE_END)
29+
ListCallsFilter(date_start=date_start, date_end=date_end)
3030
)
3131

3232
for call in calls:

Diff for: voice/track-ncco.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import os
2-
from os.path import join, dirname
3-
from dotenv import load_dotenv
4-
from fastapi import FastAPI, Body, Request
1+
from fastapi import FastAPI, Request
52
from vonage_voice import NccoAction, Notify, Talk
63

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-
134
app = FastAPI()
145

156

@@ -19,7 +10,7 @@ async def inbound_call(request: Request):
1910
Talk(text=f'Thanks for calling the notification line.'),
2011
Notify(
2112
payload={"foo": "bar"},
22-
eventUrl=[str(request.base_url) + '/webhooks/notification'],
13+
eventUrl=[str(request.base_url) + 'webhooks/notification'],
2314
),
2415
Talk(text=f'You will never hear me as the notification URL will return an NCCO.'),
2516
]

0 commit comments

Comments
 (0)