From f24a85cc94daaf716783940a0965207855d5b6bc Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Sun, 27 Apr 2025 14:21:14 -0400 Subject: [PATCH] Add logic to only forward the first on_dialin_ready event --- examples/phone-chatbot-daily-twilio-sip/bot.py | 11 +++++++++++ examples/phone-chatbot-daily-twilio-sip/server.py | 6 +++--- .../utils/daily_helpers.py | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/phone-chatbot-daily-twilio-sip/bot.py b/examples/phone-chatbot-daily-twilio-sip/bot.py index 5a7e07398..69010e0cc 100644 --- a/examples/phone-chatbot-daily-twilio-sip/bot.py +++ b/examples/phone-chatbot-daily-twilio-sip/bot.py @@ -39,6 +39,8 @@ async def run_bot(room_url: str, token: str, call_id: str, sip_uri: str) -> None logger.info(f"Starting bot with room: {room_url}") logger.info(f"SIP endpoint: {sip_uri}") + call_already_forwarded = False + # Setup the Daily transport transport = DailyTransport( room_url, @@ -113,6 +115,14 @@ async def run_bot(room_url: str, token: str, call_id: str, sip_uri: str) -> None # Handle call ready to forward @transport.event_handler("on_dialin_ready") async def on_dialin_ready(transport, cdata): + nonlocal call_already_forwarded + + # We only want to forward the call once + # The on_dialin_ready event will be triggered for each sip endpoint provisioned + if call_already_forwarded: + logger.warning("Call already forwarded, ignoring this event.") + return + logger.info(f"Forwarding call {call_id} to {sip_uri}") try: @@ -121,6 +131,7 @@ async def run_bot(room_url: str, token: str, call_id: str, sip_uri: str) -> None twiml=f"{sip_uri}" ) logger.info("Call forwarded successfully") + call_already_forwarded = True except Exception as e: logger.error(f"Failed to forward call: {str(e)}") raise diff --git a/examples/phone-chatbot-daily-twilio-sip/server.py b/examples/phone-chatbot-daily-twilio-sip/server.py index ddbc188ea..47b7a4a22 100644 --- a/examples/phone-chatbot-daily-twilio-sip/server.py +++ b/examples/phone-chatbot-daily-twilio-sip/server.py @@ -21,10 +21,10 @@ load_dotenv() @asynccontextmanager async def lifespan(app: FastAPI): # Create aiohttp session to be used for Daily API calls - app.session = aiohttp.ClientSession() + app.state.session = aiohttp.ClientSession() yield # Close session when shutting down - await app.session.close() + await app.state.session.close() app = FastAPI(lifespan=lifespan) @@ -51,7 +51,7 @@ async def handle_call(request: Request): # Create a Daily room with SIP capabilities try: - room_details = await create_sip_room(request.app.session, caller_phone) + room_details = await create_sip_room(request.app.state.session, caller_phone) except Exception as e: print(f"Error creating Daily room: {e}") raise HTTPException(status_code=500, detail=f"Failed to create Daily room: {str(e)}") diff --git a/examples/phone-chatbot-daily-twilio-sip/utils/daily_helpers.py b/examples/phone-chatbot-daily-twilio-sip/utils/daily_helpers.py index 2c42efb9d..ab3148b42 100644 --- a/examples/phone-chatbot-daily-twilio-sip/utils/daily_helpers.py +++ b/examples/phone-chatbot-daily-twilio-sip/utils/daily_helpers.py @@ -1,7 +1,7 @@ """Helper functions for interacting with the Daily API.""" import os -from typing import Any, Dict, Optional +from typing import Dict, Optional import aiohttp from dotenv import load_dotenv