Skip to content

Commit

Permalink
add final voice snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Jan 13, 2025
1 parent c3db853 commit 3159c5f
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 228 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ CONFERENCE_NAME='NAME_OF_YOUR_CONFERENCE'
YOUR_SECOND_NUMBER='YOUR_SECOND_NUMBER'
RECORDING_URL='RECORDING_URL'
CALL_UUID='CALL_UUID'
LANGUAGE='en-US'

# Numbers
COUNTRY_CODE='GB'
Expand Down
2 changes: 1 addition & 1 deletion voice/connect-an-inbound-call.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
app = FastAPI()


@app.get('/answer')
@app.get('/webhooks/answer')
async def inbound_call():
ncco = [
Connect(
Expand Down
35 changes: 15 additions & 20 deletions voice/connect-callers-to-a-conference.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
#!/usr/bin/env python3
from flask import Flask, jsonify
import os
from os.path import join, dirname
from dotenv import load_dotenv
import os
from fastapi import FastAPI
from vonage_voice.models import Conversation, NccoAction, Talk

app = Flask(__name__)

dotenv_path = join(dirname(__file__), "../.env")
dotenv_path = join(dirname(__file__), '../.env')
load_dotenv(dotenv_path)

VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
YOUR_SECOND_NUMBER = os.environ.get('YOUR_SECOND_NUMBER')
CONFERENCE_NAME = os.environ.get("CONFERENCE_NAME")

@app.route("/webhooks/answer")
def answer_call():
ncco = [
{
"action": "talk",
"text": "Please wait while we connect you to the conference"
},
{
"action": "conversation",
"name": CONFERENCE_NAME
}]
return jsonify(ncco)
app = FastAPI()


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

if __name__ == '__main__':
app.run(port=3000)
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
83 changes: 43 additions & 40 deletions voice/handle-user-input-with-asr.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
#!/usr/bin/env python3
from flask import Flask, request, jsonify

app = Flask(__name__)


@app.route("/webhooks/answer", methods=["POST", "GET"])
def answer_call():
ncco = [
{"action": "talk", "text": "Please, tell me something",},
{
"action": "input",
"type": ["speech"],
"eventUrl": [
"{host}{endpoint}".format(
host=request.host_url, endpoint="webhooks/asr"
)
],
"speech": {
"endOnSilence": 1,
"language": "en-US",
"uuid": [request.args.get("uuid")], # Change to request.json.get("uuid") if using POST-JSON webhook format
},
},
import os
from os.path import join, dirname
from dotenv import load_dotenv
from fastapi import FastAPI, Body, Request
from vonage_voice.models import Input, NccoAction, Speech, Talk

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

VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')

app = FastAPI()


@app.get('/webhooks/answer')
async def answer_call(request: Request):
ncco: list[NccoAction] = [
Talk(text=f'Please tell me something.'),
Input(
type=['speech'],
speech=Speech(
endOnSilence=1,
language='en-US',
uuid=[request.query_params.get('uuid')],
),
eventUrl=[str(request.base_url) + '/webhooks/asr'],
),
]
return jsonify(ncco)


@app.route("/webhooks/asr", methods=["POST", "GET"])
def answer_asr():
body = request.get_json()
if body is not None and "speech" in body:
speech = body["speech"]["results"][0]["text"]
ncco = [
{"action": "talk", "text": "Hello ,you said {speech}".format(speech=speech)}
]
else:
ncco = [{"action": "talk", "text": "Sorry, i don't undertand. Bye"}]

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


if __name__ == "__main__":
app.run(port=3000)
@app.post('/webhooks/asr')
async def answer_asr(data: dict = Body(...)):
if data is not None and 'speech' in data:
speech = data['speech']['results'][0]['text']
return [
Talk(text=f'Hello ,you said {speech}').model_dump(
by_alias=True, exclude_none=True
)
]
return [
Talk(text=f'Sorry, I didn\'t understand your input.').model_dump(
by_alias=True, exclude_none=True
)
]
72 changes: 33 additions & 39 deletions voice/handle-user-input.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
#!/usr/bin/env python3
from pprint import pprint
from flask import Flask, request, jsonify

app = Flask(__name__)


@app.route("/webhooks/answer")
def answer_call():
for param_key, param_value in request.args.items():
print("{}: {}".format(param_key, param_value))
input_webhook_url = request.url_root + "webhooks/dtmf"
ncco = [
{
"action": "talk",
"text": "Hello, please press any key to continue."
},
{
"action": "input",
"type": ["dtmf"],
"maxDigits": 1,
"eventUrl": [input_webhook_url]
}
]
return jsonify(ncco)


@app.route("/webhooks/dtmf", methods=['POST'])
def dtmf():
data = request.get_json()
pprint(data)
ncco = [
{
"action": "talk",
"text": "You pressed {}, goodbye".format(data['dtmf'])
}
import os
from os.path import join, dirname
from dotenv import load_dotenv
from fastapi import FastAPI, Body, Request
from vonage_voice.models import Input, NccoAction, Talk

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

VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')

app = FastAPI()


@app.get('/webhooks/answer')
async def answer_call(request: Request):
ncco: list[NccoAction] = [
Talk(text=f'Please enter a digit.'),
Input(
type=['dtmf'],
maxDigits=1,
eventUrl=[str(request.base_url) + '/webhooks/dtmf'],
),
]
return jsonify(ncco)

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

if __name__ == '__main__':
app.run(port=3000)

@app.post('/webhooks/dtmf')
async def answer_dtmf(data: dict = Body(...)):
return [
Talk(text=f'Hello, you pressed {data['dtmf']}').model_dump(
by_alias=True, exclude_none=True
)
]
31 changes: 0 additions & 31 deletions voice/join-outbound-calls.py

This file was deleted.

4 changes: 3 additions & 1 deletion voice/play-tts-into-call.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

CALL_UUID = os.environ.get('CALL_UUID')
LANGUAGE = os.environ.get('LANGUAGE')

from vonage import Auth, Vonage
from vonage_voice.models import CallMessage, TtsStreamOptions
Expand All @@ -25,7 +26,8 @@
)

response: CallMessage = client.voice.play_tts_into_call(
uuid=CALL_UUID, tts_options=TtsStreamOptions(text='Hello from Vonage.')
uuid=CALL_UUID,
tts_options=TtsStreamOptions(text='Hello from Vonage.', language=LANGUAGE),
)

pprint(response)
2 changes: 1 addition & 1 deletion voice/record-a-call.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def inbound_call():
),
]

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


@app.post('/webhooks/recordings')
Expand Down
2 changes: 1 addition & 1 deletion voice/record-a-message.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def answer_call(request: Request):
Talk(text='Thank you for your message.'),
]

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


@app.post('/webhooks/recordings')
Expand Down
79 changes: 36 additions & 43 deletions voice/track-ncco.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
from flask import Flask, request, jsonify

app = Flask(__name__)


@app.route("/webhooks/answer")
def answer_call():
ncco = [{
"action": "talk",
"text": "Thanks for calling the notification line"
},
{
"action": "notify",
"payload": {
"foo": "bar"
},
"eventUrl": [
"{url_root}webhooks/notification".format(url_root=request.url_root)
]
},
{
"action": "talk",
"text": "You will never hear me as the notification URL will return an NCCO "
}]
return jsonify(ncco)


@app.route("/webhooks/notification", methods=['POST'])
def notification():
ncco = [{
"action": "talk",
"text": "Your notification has been received, loud and clear"
}]
return jsonify(ncco)


@app.route("/webhooks/event", methods=['POST'])
def event():
return "OK"


if __name__ == '__main__':
app.run(host="localhost", port=3000)
import os
from os.path import join, dirname
from dotenv import load_dotenv
from fastapi import FastAPI, Body, Request
from vonage_voice.models import NccoAction, Notify, Talk

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

VONAGE_NUMBER = os.environ.get('VONAGE_NUMBER')
RECIPIENT_NUMBER = os.environ.get('RECIPIENT_NUMBER')

app = FastAPI()


@app.get('/webhooks/answer')
async def inbound_call(request: Request):
ncco: list[NccoAction] = [
Talk(text=f'Thanks for calling the notification line.'),
Notify(
payload={"foo": "bar"},
eventUrl=[str(request.base_url) + '/webhooks/notification'],
),
Talk(text=f'You will never hear me as the notification URL will return an NCCO.'),
]

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


@app.post('/webhooks/notification')
async def on_notification():
return [
Talk(text=f'Your notification has been received, loud and clear').model_dump(
by_alias=True, exclude_none=True
)
]
Loading

0 comments on commit 3159c5f

Please sign in to comment.