From 5835e74377139f3dc783a1895e7362d2a0721be6 Mon Sep 17 00:00:00 2001 From: Shaon Debnath Date: Mon, 14 Apr 2025 19:00:48 +0530 Subject: [PATCH] Add docs for outbound calls with twilio (#273) * Add docs for outbound calls with twilio * Add code --------- Co-authored-by: Freddy Boulton <41651716+freddyaboulton@users.noreply.github.com> --- docs/userguide/audio.md | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/userguide/audio.md b/docs/userguide/audio.md index e012d31..aeff0d8 100644 --- a/docs/userguide/audio.md +++ b/docs/userguide/audio.md @@ -386,3 +386,48 @@ stream.mount(app) # run with `uvicorn main:app` ``` + +### Outbound calls with Twilio + +Here's a simple example to call someone using the twilio-python module: + +```py +app = FastAPI() + +@app.post("/call") +async def start_call(req: Request): + body = await req.json() + from_no = body.get("from") + to_no = body.get("to") + account_sid = os.getenv("TWILIO_ACCOUNT_SID") + auth_token = os.getenv("TWILIO_AUTH_TOKEN") + client = Client(account_sid, auth_token) + + # Use the public URL of your application + # here we're using ngrok to expose an app + # running locally + call = client.calls.create( + to=to_no, + from_=from_no, + url="https://[your_ngrok_subdomain].ngrok.app/incoming-call" + ) + + return {"sid": f"{call.sid}"} + +@app.api_route("/incoming-call", methods=["GET", "POST"]) +async def handle_incoming_call(req: Request): + from twilio.twiml.voice_response import VoiceResponse, Connect + response = VoiceResponse() + response.say("Connecting to AI assistant") + connect = Connect() + connect.stream(url=f'wss://{req.url.hostname}/media-stream') + response.append(connect) + return HTMLResponse(content=str(response), media_type="application/xml") + +@app.websocket("/media-stream") +async def handle_media_stream(websocket: WebSocket): + # stream is a FastRTC stream defined elsewhere + await stream.telephone_handler(websocket) + +app = gr.mount_gradio_app(app, stream.ui, path="/") +```