|
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 | + ), |
25 | 29 | ]
|
26 |
| - return jsonify(ncco) |
27 | 30 |
|
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] |
41 | 32 |
|
42 | 33 |
|
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 | + ] |
0 commit comments