Skip to content

Commit 5809813

Browse files
committed
add voice recording snippets
1 parent ecd37c6 commit 5809813

7 files changed

+119
-141
lines changed

.env.dist

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ START_DATE='START_DATE'
7979
# Voice
8080
CONFERENCE_NAME='NAME_OF_YOUR_CONFERENCE'
8181
YOUR_SECOND_NUMBER='YOUR_SECOND_NUMBER'
82+
RECORDING_URL='RECORDING_URL'
8283

8384
# Numbers
8485
COUNTRY_CODE='GB'

voice/get-recording.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
#!/usr/bin/env python3
1+
from pprint import pprint
22
import os
33
from os.path import join, dirname
4-
from pprint import pprint
5-
import vonage
64
from dotenv import load_dotenv
75

8-
dotenv_path = join(dirname(__file__), "../.env")
6+
dotenv_path = join(dirname(__file__), '../.env')
97
load_dotenv(dotenv_path)
108

11-
VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
12-
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get("VONAGE_APPLICATION_PRIVATE_KEY_PATH")
13-
14-
client = vonage.Client(
15-
application_id=VONAGE_APPLICATION_ID,
16-
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
9+
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
10+
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
11+
'VONAGE_APPLICATION_PRIVATE_KEY_PATH'
1712
)
1813

19-
response = client.voice.get_recording("RECORDING_URL")
20-
pprint(response)
14+
RECORDING_URL = os.environ.get('RECORDING_URL')
15+
16+
from vonage import Auth, Vonage
17+
18+
client = Vonage(
19+
Auth(
20+
application_id=VONAGE_APPLICATION_ID,
21+
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
22+
)
23+
)
2124

25+
client.voice.download_recording(RECORDING_URL, 'recording.mp3')

voice/make-outbound-call-ncco.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
from os.path import join, dirname
55
from dotenv import load_dotenv
66

7-
dotenv_path = join(dirname(__file__), "../.env")
7+
dotenv_path = join(dirname(__file__), '../.env')
88
load_dotenv(dotenv_path)
99

10-
VONAGE_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
10+
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID')
1111
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
12-
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
12+
'VONAGE_APPLICATION_PRIVATE_KEY_PATH'
1313
)
1414

15-
VONAGE_NUMBER = os.environ.get("VONAGE_NUMBER")
16-
TO_NUMBER = os.environ.get("TO_NUMBER")
15+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
16+
TO_NUMBER = os.environ.get('TO_NUMBER')
1717

1818
from vonage import Auth, Vonage
1919
from vonage_voice.models import CreateCallRequest, Phone, Talk, ToPhone
+36-39
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
#!/usr/bin/env python3
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from fastapi import FastAPI, Body
25
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-
ncco = [
11-
{
12-
"action": "talk",
13-
"text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes."
14-
},
15-
{
16-
"action": "record",
17-
"split": "conversation",
18-
"channels": 2,
19-
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
20-
},
21-
{
22-
"action": "connect",
23-
"eventUrl": ["https://demo.ngrok.io/webhooks/event"],
24-
"from": "VONAGE_NUMBER",
25-
"endpoint": [
26-
{
27-
"type": "phone",
28-
"number": "RECIPIENT_NUMBER"
29-
}
30-
]
31-
}
32-
]
33-
return jsonify(ncco)
6+
from vonage_voice.models import Connect, NccoAction, PhoneEndpoint, Record, Talk
347

8+
dotenv_path = join(dirname(__file__), '../.env')
9+
load_dotenv(dotenv_path)
10+
11+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
12+
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
13+
14+
app = FastAPI()
3515

36-
@app.route("/webhooks/recordings", methods=['POST'])
37-
def recordings():
38-
data = request.get_json()
39-
pprint(data)
40-
return "Webhook received"
4116

17+
@app.get('/webhooks/answer')
18+
async def inbound_call():
19+
ncco: list[NccoAction] = [
20+
Talk(
21+
text=f'Hi, we will shortly forward your call. This call is recorded for quality assurance purposes.'
22+
),
23+
Record(
24+
split='conversation',
25+
channels=2,
26+
eventUrl=['https://demo.ngrok.io/webhooks/recordings'],
27+
),
28+
Connect(
29+
endpoint=[PhoneEndpoint(number=RECIPIENT_NUMBER)],
30+
from_=VONAGE_NUMBER,
31+
eventUrl=['https://demo.ngrok.io/webhooks/event'],
32+
),
33+
]
34+
35+
return [step.model_dump(by_alias=True, exclude_none=True) for step in ncco]
4236

43-
if __name__ == '__main__':
44-
app.run(port=3000)
37+
38+
@app.post('/webhooks/recordings')
39+
async def recordings(data: dict = Body(...)):
40+
pprint(data)
41+
return {'message': 'webhook received'}

voice/record-a-call.py

+32-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1-
#!/usr/bin/env python3
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from fastapi import FastAPI, Body
25
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-
ncco = [
11-
{
12-
"action": "talk",
13-
"text": "Hi, we will shortly forward your call. This call is recorded for quality assurance purposes."
14-
},
15-
{
16-
"action": "record",
17-
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
18-
},
19-
{
20-
"action": "connect",
21-
"eventUrl": ["https://demo.ngrok.io/webhooks/event"],
22-
"from": "VONAGE_NUMBER",
23-
"endpoint": [
24-
{
25-
"type": "phone",
26-
"number": "RECIPIENT_NUMBER"
27-
}
28-
]
29-
}
30-
]
31-
return jsonify(ncco)
6+
from vonage_voice.models import Connect, NccoAction, PhoneEndpoint, Record, Talk
327

8+
dotenv_path = join(dirname(__file__), '../.env')
9+
load_dotenv(dotenv_path)
10+
11+
VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
12+
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')
13+
14+
app = FastAPI()
3315

34-
@app.route("/webhooks/recordings", methods=['POST'])
35-
def recordings():
36-
data = request.get_json()
37-
pprint(data)
38-
return "webhook received"
3916

17+
@app.get('/webhooks/answer')
18+
async def inbound_call():
19+
ncco: list[NccoAction] = [
20+
Talk(
21+
text=f'Hi, we will shortly forward your call. This call is recorded for quality assurance purposes.'
22+
),
23+
Record(eventUrl=['https://demo.ngrok.io/webhooks/recordings']),
24+
Connect(
25+
endpoint=[PhoneEndpoint(number=RECIPIENT_NUMBER)],
26+
from_=VONAGE_NUMBER,
27+
eventUrl=['https://demo.ngrok.io/webhooks/event'],
28+
),
29+
]
30+
31+
return [step.model_dump(by_alias=True, exclude_none=True) for step in ncco]
4032

41-
if __name__ == '__main__':
42-
app.run(port=3000)
33+
34+
@app.post('/webhooks/recordings')
35+
async def recordings(data: dict = Body(...)):
36+
pprint(data)
37+
return {'message': 'webhook received'}

voice/record-a-conversation.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
#!/usr/bin/env python3
2-
# `eventMethod` is a required workaround currently, otherwise `/webhooks/recordings` is never called.
1+
from fastapi import FastAPI, Body
32
from pprint import pprint
4-
from flask import Flask, request, jsonify
53

6-
app = Flask(__name__)
4+
app = FastAPI()
75

86

9-
@app.route("/webhooks/answer")
10-
def answer_call():
7+
@app.get('/webhooks/answer')
8+
async def answer_call():
119
ncco = [
1210
{
1311
"action": "conversation",
1412
"name": "CONF_NAME",
1513
"record": "true",
1614
"eventMethod": "POST",
17-
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"]
15+
"eventUrl": ["https://demo.ngrok.io/webhooks/recordings"],
1816
}
1917
]
20-
return jsonify(ncco)
2118

22-
23-
@app.route("/webhooks/recordings", methods=['POST'])
24-
def recordings():
25-
data = request.get_json()
26-
pprint(data)
27-
return "Webhook received"
19+
return ncco
2820

2921

30-
if __name__ == '__main__':
31-
app.run(port=3000)
22+
@app.post('/webhooks/recordings')
23+
async def recordings(data: dict = Body(...)):
24+
pprint(data)
25+
return {'message': 'webhook received'}

voice/record-a-message.py

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
1-
#!/usr/bin/env python3
1+
from fastapi import FastAPI, Body, Request
22
from pprint import pprint
3-
import http
4-
from flask import Flask, request, jsonify
3+
from vonage_voice.models import NccoAction, Record, Talk
54

6-
app = Flask(__name__)
5+
app = FastAPI()
76

87

9-
@app.route("/webhooks/answer")
10-
def answer_call():
11-
for param_key, param_value in request.args.items():
12-
print("{}: {}".format(param_key, param_value))
13-
recording_webhook_url = request.url_root + "webhooks/recording"
14-
ncco = [
15-
{
16-
"action": "talk",
17-
"text": "Please leave a message after the tone, then press the hash key."
18-
},
19-
{
20-
"action": "record",
21-
"endOnKey": "#",
22-
"beepStart": "true",
23-
"eventUrl": [recording_webhook_url]
24-
},
25-
{
26-
"action": "talk",
27-
"text": "Thank you for your message."
28-
}
8+
@app.get('/webhooks/answer')
9+
async def answer_call(request: Request):
10+
print(request.base_url)
11+
ncco: list[NccoAction] = [
12+
Talk(text='Please leave a message after the tone, then press the hash key.'),
13+
Record(
14+
endOnKey='#',
15+
beepStart=True,
16+
eventUrl=[str(request.base_url) + '/webhooks/recordings'],
17+
),
18+
Talk(text='Thank you for your message.'),
2919
]
30-
return jsonify(ncco)
3120

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

33-
@app.route("/webhooks/recording", methods=['POST'])
34-
def recording_webhook():
35-
pprint(request.get_json())
36-
return ('', http.HTTPStatus.NO_CONTENT)
3723

38-
39-
if __name__ == '__main__':
40-
app.run(port=3000)
24+
@app.post('/webhooks/recordings')
25+
async def recordings(data: dict = Body(...)):
26+
pprint(data)
27+
return {'message': 'webhook received'}

0 commit comments

Comments
 (0)