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>
This commit is contained in:
Shaon Debnath
2025-04-14 19:00:48 +05:30
committed by GitHub
parent dcde624449
commit 5835e74377

View File

@@ -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="/")
```