Vonage (Nexmo) Integration for Voice AI Vonage Voice API (formerly Nexmo) is a Twilio alternative with a Web
Socket API for real-time audio. Strengths: SIP support, good European coverage, competitive outgoing call rates. ### Vonage's integration architecture uses NCCO (Nexmo Call Control Objects) – JSON-based call control:```python from fastapi import FastAPI, Request from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/answer") async def answer_call(uuid: str, conversation_uuid: str): """NCCO для входящего звонка""" return JSONResponse([ { "action": "talk", "text": "Здравствуйте! Я голосовой ассистент.", "language": "ru-RU", "style": 4 }, { "action": "connect", "endpoint": [{ "type": "websocket", "uri": f"wss://api.yourapp.com/voice-stream/{uuid}", "content-type": "audio/l16;rate=16000", "headers": {"call_id": uuid} }] } ])
@app.post("/events")
async def call_events(request: Request):
data = await request.json()
status = data.get("status")
if status in ["completed", "failed"]:
await cleanup_session(data.get("uuid"))
return JSONResponse({"status": "ok"})
### WebSocket handlerpython
from fastapi import WebSocket
@app.websocket("/voice-stream/{call_id}") async def voice_stream(websocket: WebSocket, call_id: str): await websocket.accept() session = VoiceSession(call_id)
try:
async for message in websocket.iter_bytes():
# Vonage отправляет PCM 16-bit 16kHz
pcm_audio = message
# Обрабатываем аудио через наш AI pipeline
response_text = await process_audio(pcm_audio, session)
if response_text:
audio_response = await synthesize(response_text)
await websocket.send_bytes(audio_response)
except Exception as e:
logger.error(f"WebSocket error: {e}")
finally:
await session.finalize()
### Sending events and call controlpython
import vonage
client = vonage.Client(key=VONAGE_KEY, secret=VONAGE_SECRET) voice = vonage.Voice(client)
def transfer_to_agent(call_uuid: str, agent_number: str): """Перевод на оператора""" voice.update_call(call_uuid, { "action": "transfer", "destination": { "type": "ncco", "ncco": [{ "action": "connect", "endpoint": [{"type": "phone", "number": agent_number}] }] } })







