|
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 |
2 | 5 | 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 |
34 | 7 |
|
| 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() |
35 | 15 |
|
36 |
| -@app.route("/webhooks/recordings", methods=['POST']) |
37 |
| -def recordings(): |
38 |
| - data = request.get_json() |
39 |
| - pprint(data) |
40 |
| - return "Webhook received" |
41 | 16 |
|
| 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] |
42 | 36 |
|
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'} |
0 commit comments