Call Transfer demo (#1348)
* Updated code to dial out to an operator, keep track of operator conversation while escalated and then return to conversation when finished * Removed unnecessary imports * Updated bot runner code, added call routing file and then updated the call transfer and voicemail detection examples * Updated the bot files * Made prompt one level higher in the body and an array * Updated call transfer examples to work correctly * Updated gemini voicemail detection example to work * Added twilio bot support back to the bot_runner * Moved some state management, participant management and other logic to the helper file. * Updated comments * Updated env and requirements file * Ran the examples and made sure code works. Still need to work on the prompts a bit * Fixed format issue * Add support to disable summary in call transfer * Added support for operator transfer mode * Updated readme file * Updated readme based on feedback, and handling of various properties in the json to be more flexible for future examples * Updated number of endpoints * Updated readme to remove fly deployment text and replaced with Pipecat Cloud * Starting to tweak function calls and prompts * Updated examples to more consistently call the functions and say what they need to say * Updated examples * Updated examples * Updated examples to work correctly * Add simple bot versions of dialin and dialout * Refactored the bot runner file to make adding future examples easier * Based on feedback, removed examples for multiple LLMs and also adjusted voicemail detection code to be simpler * Made sure to only capture the users transcription once * Updated readme with latest changes * Forgot to update the order of examples in one place * Fixed formatting issue * Adjusted based on james feedback * Changed default_mode to default_calltransfer_mode
This commit is contained in:
@@ -1,24 +1,22 @@
|
||||
#
|
||||
# Copyright (c) 2024–2025, Daily
|
||||
#
|
||||
# SPDX-License-Identifier: BSD 2-Clause License
|
||||
#
|
||||
|
||||
"""
|
||||
bot_runner.py
|
||||
|
||||
HTTP service that listens for incoming calls from either Daily or Twilio,
|
||||
provisioning a room and starting a Pipecat bot in response.
|
||||
|
||||
Refer to README for more information.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, Dict
|
||||
|
||||
import aiohttp
|
||||
from bot_constants import (
|
||||
MAX_SESSION_TIME,
|
||||
REQUIRED_ENV_VARS,
|
||||
)
|
||||
from bot_definitions import bot_registry
|
||||
from bot_runner_helpers import (
|
||||
determine_room_capabilities,
|
||||
ensure_prompt_config,
|
||||
process_dialin_request,
|
||||
)
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
@@ -27,7 +25,6 @@ from twilio.twiml.voice_response import VoiceResponse
|
||||
|
||||
from pipecat.transports.services.helpers.daily_rest import (
|
||||
DailyRESTHelper,
|
||||
DailyRoomObject,
|
||||
DailyRoomParams,
|
||||
DailyRoomProperties,
|
||||
DailyRoomSipParams,
|
||||
@@ -35,15 +32,126 @@ from pipecat.transports.services.helpers.daily_rest import (
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
|
||||
# ------------ Configuration ------------ #
|
||||
|
||||
MAX_SESSION_TIME = 5 * 60 # 5 minutes
|
||||
REQUIRED_ENV_VARS = ["OPENAI_API_KEY", "DAILY_API_KEY", "ELEVENLABS_API_KEY", "ELEVENLABS_VOICE_ID"]
|
||||
|
||||
daily_helpers = {}
|
||||
|
||||
# ----------------- API ----------------- #
|
||||
|
||||
# ----------------- Daily Room Management ----------------- #
|
||||
|
||||
|
||||
async def create_daily_room(room_url: str = None, config_body: Dict[str, Any] = None):
|
||||
"""Create or retrieve a Daily room with appropriate properties based on the configuration.
|
||||
|
||||
Args:
|
||||
room_url: Optional existing room URL
|
||||
config_body: Optional configuration that determines room capabilities
|
||||
|
||||
Returns:
|
||||
Dict containing room URL, token, and SIP endpoint
|
||||
"""
|
||||
if not room_url:
|
||||
# Get room capabilities based on the configuration
|
||||
capabilities = determine_room_capabilities(config_body)
|
||||
|
||||
# Configure SIP parameters if dialin is needed
|
||||
sip_params = None
|
||||
if capabilities["enable_dialin"]:
|
||||
sip_params = DailyRoomSipParams(
|
||||
display_name="dialin-user", video=False, sip_mode="dial-in", num_endpoints=2
|
||||
)
|
||||
|
||||
# Create the properties object with the appropriate settings
|
||||
properties = DailyRoomProperties(sip=sip_params)
|
||||
|
||||
# Set dialout capability if needed
|
||||
if capabilities["enable_dialout"]:
|
||||
properties.enable_dialout = True
|
||||
|
||||
# Log the capabilities being used
|
||||
capability_str = ", ".join([f"{k}={v}" for k, v in capabilities.items()])
|
||||
print(f"Creating room with capabilities: {capability_str}")
|
||||
|
||||
params = DailyRoomParams(properties=properties)
|
||||
|
||||
print("Creating new room...")
|
||||
room = await daily_helpers["rest"].create_room(params=params)
|
||||
else:
|
||||
# Check if passed room URL exists
|
||||
try:
|
||||
room = await daily_helpers["rest"].get_room_from_url(room_url)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=500, detail=f"Room not found: {room_url}")
|
||||
|
||||
print(f"Daily room: {room.url} {room.config.sip_endpoint}")
|
||||
|
||||
# Get token for the agent
|
||||
token = await daily_helpers["rest"].get_token(room.url, MAX_SESSION_TIME)
|
||||
|
||||
if not room or not token:
|
||||
raise HTTPException(status_code=500, detail="Failed to get room or token")
|
||||
|
||||
return {"room": room.url, "token": token, "sip_endpoint": room.config.sip_endpoint}
|
||||
|
||||
|
||||
# ----------------- Bot Process Management ----------------- #
|
||||
|
||||
|
||||
async def start_bot(room_details: Dict[str, str], body: Dict[str, Any], example: str) -> bool:
|
||||
"""Start a bot process with the given configuration.
|
||||
|
||||
Args:
|
||||
room_details: Room URL and token
|
||||
body: Bot configuration
|
||||
example: Example script to run
|
||||
|
||||
Returns:
|
||||
Boolean indicating success
|
||||
"""
|
||||
room_url = room_details["room"]
|
||||
token = room_details["token"]
|
||||
|
||||
# Properly format body as JSON string for command line
|
||||
body_json = json.dumps(body).replace('"', '\\"')
|
||||
print(f"++++ Body JSON: {body_json}")
|
||||
|
||||
# Modified to use non-LLM-specific bot module names
|
||||
bot_proc = f'python3 -m {example} -u {room_url} -t {token} -b "{body_json}"'
|
||||
print(f"Starting bot. Example: {example}, Room: {room_url}")
|
||||
|
||||
try:
|
||||
command_parts = shlex.split(bot_proc)
|
||||
subprocess.Popen(command_parts, bufsize=1, cwd=os.path.dirname(os.path.abspath(__file__)))
|
||||
return True
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start subprocess: {e}")
|
||||
|
||||
|
||||
async def start_twilio_bot(room_details: Dict[str, str], call_id: str) -> bool:
|
||||
"""Start a Twilio bot process with the given configuration.
|
||||
|
||||
Args:
|
||||
room_details: Room URL, token, and SIP endpoint
|
||||
call_id: Twilio call ID (CallSid)
|
||||
|
||||
Returns:
|
||||
Boolean indicating success
|
||||
"""
|
||||
room_url = room_details["room"]
|
||||
token = room_details["token"]
|
||||
sip_endpoint = room_details["sip_endpoint"]
|
||||
|
||||
# Format command for Twilio bot
|
||||
bot_proc = f"python3 -m bot_twilio -u {room_url} -t {token} -i {call_id} -s {sip_endpoint}"
|
||||
print(f"Starting Twilio bot. Room: {room_url}")
|
||||
|
||||
try:
|
||||
command_parts = shlex.split(bot_proc)
|
||||
subprocess.Popen(command_parts, bufsize=1, cwd=os.path.dirname(os.path.abspath(__file__)))
|
||||
return True
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start subprocess: {e}")
|
||||
|
||||
|
||||
# ----------------- API Setup ----------------- #
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -68,111 +176,44 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
"""
|
||||
Create Daily room, tell the bot if the room is created for Twilio's SIP or Daily's SIP (vendor).
|
||||
When the vendor is Daily, the bot handles the call forwarding automatically,
|
||||
i.e, forwards the call from the "hold music state" to the Daily Room's SIP URI.
|
||||
|
||||
Alternatively, when the vendor is Twilio (not Daily), the bot is responsible for
|
||||
updating the state on Twilio. So when `dialin-ready` fires, it takes appropriate
|
||||
action using the Twilio Client library.
|
||||
"""
|
||||
|
||||
|
||||
async def _create_daily_room(
|
||||
room_url, callId, callDomain=None, dialoutNumber=None, vendor="daily", detect_voicemail=False
|
||||
):
|
||||
if not room_url:
|
||||
# Create base properties with SIP settings
|
||||
properties = DailyRoomProperties(
|
||||
sip=DailyRoomSipParams(
|
||||
display_name="dialin-user", video=False, sip_mode="dial-in", num_endpoints=1
|
||||
)
|
||||
)
|
||||
|
||||
# Only enable dialout if dialoutNumber is provided
|
||||
if dialoutNumber:
|
||||
properties.enable_dialout = True
|
||||
|
||||
params = DailyRoomParams(properties=properties)
|
||||
|
||||
print(f"Creating new room...")
|
||||
room: DailyRoomObject = await daily_helpers["rest"].create_room(params=params)
|
||||
|
||||
else:
|
||||
# Check passed room URL exist (we assume that it already has a sip set up!)
|
||||
try:
|
||||
room: DailyRoomObject = await daily_helpers["rest"].get_room_from_url(room_url)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=500, detail=f"Room not found: {room_url}")
|
||||
|
||||
print(f"Daily room: {room.url} {room.config.sip_endpoint}")
|
||||
|
||||
# Give the agent a token to join the session
|
||||
token = await daily_helpers["rest"].get_token(room.url, MAX_SESSION_TIME)
|
||||
|
||||
if not room or not token:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to get room or token token")
|
||||
|
||||
# Spawn a new agent, and join the user session
|
||||
# Note: this is mostly for demonstration purposes (refer to 'deployment' in docs)
|
||||
print(f"Vendor: {vendor}")
|
||||
if vendor == "daily":
|
||||
bot_proc = f"python3 -m bot_daily -u {room.url} -t {token} -i {callId} -d {callDomain}{' -v' if detect_voicemail else ''}"
|
||||
if dialoutNumber:
|
||||
bot_proc += f" -o {dialoutNumber}"
|
||||
elif vendor == "daily-gemini":
|
||||
bot_proc = f"python3 -m bot_daily_gemini -u {room.url} -t {token} -i {callId} -d {callDomain}{' -v' if detect_voicemail else ''}"
|
||||
if dialoutNumber:
|
||||
bot_proc += f" -o {dialoutNumber}"
|
||||
else:
|
||||
bot_proc = f"python3 -m bot_twilio -u {room.url} -t {token} -i {callId} -s {room.config.sip_endpoint}"
|
||||
|
||||
try:
|
||||
subprocess.Popen(
|
||||
[bot_proc], shell=True, bufsize=1, cwd=os.path.dirname(os.path.abspath(__file__))
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to start subprocess: {e}")
|
||||
|
||||
return room
|
||||
# ----------------- API Endpoints ----------------- #
|
||||
|
||||
|
||||
@app.post("/twilio_start_bot", response_class=PlainTextResponse)
|
||||
async def twilio_start_bot(request: Request):
|
||||
print(f"POST /twilio_voice_bot")
|
||||
"""Handle incoming Twilio webhook calls and start a Twilio bot.
|
||||
|
||||
# twilio_start_bot is invoked directly by Twilio (as a web hook).
|
||||
# On Twilio, under Active Numbers, pick the phone number
|
||||
# Click Configure and under Voice Configuration,
|
||||
# "a call comes in" choose webhook and point the URL to
|
||||
# where this code is hosted.
|
||||
data = {}
|
||||
This endpoint is called directly by Twilio as a webhook when a call is received.
|
||||
It puts the call on hold with music and starts a bot that will handle the call.
|
||||
"""
|
||||
print("POST /twilio_start_bot")
|
||||
|
||||
# Get form data from Twilio webhook
|
||||
try:
|
||||
# shouldnt have received json, twilio sends form data
|
||||
form_data = await request.form()
|
||||
data = dict(form_data)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Failed to parse Twilio form data: {str(e)}")
|
||||
|
||||
# Get default room URL from environment
|
||||
room_url = os.getenv("DAILY_SAMPLE_ROOM_URL", None)
|
||||
callId = data.get("CallSid")
|
||||
|
||||
if not callId:
|
||||
raise HTTPException(status_code=500, detail="Missing 'CallSid' in request")
|
||||
# Extract call ID from Twilio data
|
||||
call_id = data.get("CallSid")
|
||||
if not call_id:
|
||||
raise HTTPException(status_code=400, detail="Missing 'CallSid' in request")
|
||||
|
||||
print("CallId: %s" % callId)
|
||||
print(f"CallId: {call_id}")
|
||||
|
||||
# create room and tell the bot to join the created room
|
||||
# note: Twilio does not require a callDomain
|
||||
room: DailyRoomObject = await _create_daily_room(room_url, callId, None, "twilio")
|
||||
# Create Daily room for the Twilio call
|
||||
room_details = await create_daily_room(room_url, None) # No special config for Twilio rooms
|
||||
|
||||
print(f"Put Twilio on hold...")
|
||||
# We have the room and the SIP URI,
|
||||
# but we do not know if the Daily SIP Worker and the Bot have joined the call
|
||||
# put the call on hold until the 'on_dialin_ready' fires.
|
||||
# Then, the bot will update the called sid with the sip uri.
|
||||
# http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3
|
||||
# Start the Twilio bot
|
||||
await start_twilio_bot(room_details, call_id)
|
||||
|
||||
# Put the call on hold until the bot is ready to handle it
|
||||
# The bot will update the call with the SIP URI when it's ready
|
||||
resp = VoiceResponse()
|
||||
resp.play(
|
||||
url="http://com.twilio.sounds.music.s3.amazonaws.com/MARKOVICHAMP-Borghestral.mp3", loop=10
|
||||
@@ -180,73 +221,98 @@ async def twilio_start_bot(request: Request):
|
||||
return str(resp)
|
||||
|
||||
|
||||
@app.post("/daily_start_bot")
|
||||
async def daily_start_bot(request: Request) -> JSONResponse:
|
||||
# The /daily_start_bot is invoked when a call is received on Daily's SIP URI
|
||||
# daily_start_bot will create the room, put the call on hold until
|
||||
# the bot and sip worker are ready. Daily will automatically
|
||||
# forward the call to the SIP URi when dialin_ready fires.
|
||||
|
||||
# Use specified room URL, or create a new one if not specified
|
||||
@app.post("/start")
|
||||
async def handle_start_request(request: Request) -> JSONResponse:
|
||||
"""Unified endpoint to handle bot configuration for different scenarios."""
|
||||
# Get default room URL from environment
|
||||
room_url = os.getenv("DAILY_SAMPLE_ROOM_URL", None)
|
||||
# Get the dial-in properties from the request
|
||||
|
||||
try:
|
||||
data = await request.json()
|
||||
# Check if this is form data (from Twilio) or JSON
|
||||
content_type = request.headers.get("content-type", "").lower()
|
||||
|
||||
if "application/x-www-form-urlencoded" in content_type:
|
||||
# Handle form data from Twilio
|
||||
form_data = await request.form()
|
||||
data = dict(form_data)
|
||||
|
||||
# Check for CallSid which indicates this is a Twilio webhook
|
||||
if "CallSid" in data:
|
||||
# Redirect to Twilio handler for backward compatibility
|
||||
return await twilio_start_bot(request)
|
||||
else:
|
||||
# Parse JSON request data
|
||||
data = await request.json()
|
||||
|
||||
# Handle webhook test
|
||||
if "test" in data:
|
||||
# Pass through any webhook checks
|
||||
return JSONResponse({"test": True})
|
||||
detect_voicemail = data.get("detectVoicemail", False)
|
||||
callId = data.get("callId", None)
|
||||
callDomain = data.get("callDomain", None)
|
||||
dialoutNumber = data.get("dialoutNumber", None)
|
||||
except Exception:
|
||||
raise HTTPException(
|
||||
status_code=500, detail="Missing properties 'callId', 'callDomain', or 'dialoutNumber'"
|
||||
)
|
||||
|
||||
room: DailyRoomObject = await _create_daily_room(
|
||||
room_url, callId, callDomain, dialoutNumber, "daily", detect_voicemail
|
||||
)
|
||||
# Handle direct dialin webhook from Daily
|
||||
if all(key in data for key in ["From", "To", "callId", "callDomain"]):
|
||||
body = await process_dialin_request(data)
|
||||
# Handle body-based request
|
||||
elif "config" in data:
|
||||
# Use the registry to set up the bot configuration
|
||||
body = bot_registry.setup_configuration(data["config"])
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail="Invalid request format")
|
||||
|
||||
# Grab a token for the user to join with
|
||||
return JSONResponse({"room_url": room.url, "sipUri": room.config.sip_endpoint})
|
||||
# Ensure prompt configuration
|
||||
body = ensure_prompt_config(body)
|
||||
|
||||
# Detect which bot type to use
|
||||
bot_type_name = bot_registry.detect_bot_type(body)
|
||||
if not bot_type_name:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Configuration doesn't match any supported scenario"
|
||||
)
|
||||
|
||||
@app.post("/daily_gemini_start_bot")
|
||||
async def daily_gemini_start_bot(request: Request) -> JSONResponse:
|
||||
# The /daily_start_bot is invoked when a call is received on Daily's SIP URI
|
||||
# daily_start_bot will create the room, put the call on hold until
|
||||
# the bot and sip worker are ready. Daily will automatically
|
||||
# forward the call to the SIP URi when dialin_ready fires.
|
||||
# Create the Daily room
|
||||
room_details = await create_daily_room(room_url, body)
|
||||
|
||||
# Use specified room URL, or create a new one if not specified
|
||||
room_url = os.getenv("DAILY_SAMPLE_ROOM_URL", None)
|
||||
# Get the dial-in properties from the request
|
||||
try:
|
||||
data = await request.json()
|
||||
if "test" in data:
|
||||
# Pass through any webhook checks
|
||||
return JSONResponse({"test": True})
|
||||
detect_voicemail = data.get("detectVoicemail", False)
|
||||
callId = data.get("callId", None)
|
||||
callDomain = data.get("callDomain", None)
|
||||
dialoutNumber = data.get("dialoutNumber", None)
|
||||
except Exception:
|
||||
raise HTTPException(
|
||||
status_code=500, detail="Missing properties 'callId', 'callDomain', or 'dialoutNumber'"
|
||||
)
|
||||
# Start the bot
|
||||
await start_bot(room_details, body, bot_type_name)
|
||||
|
||||
room: DailyRoomObject = await _create_daily_room(
|
||||
room_url, callId, callDomain, dialoutNumber, "daily-gemini", detect_voicemail
|
||||
)
|
||||
# Get the bot type
|
||||
bot_type = bot_registry.get_bot(bot_type_name)
|
||||
|
||||
# Grab a token for the user to join with
|
||||
return JSONResponse({"room_url": room.url, "sipUri": room.config.sip_endpoint})
|
||||
# Build the response
|
||||
response = {"status": "Bot started", "bot_type": bot_type_name}
|
||||
|
||||
# Add room URL for test mode
|
||||
if bot_type.has_test_mode(body):
|
||||
response["room_url"] = room_details["room"]
|
||||
# Remove llm_model from response as it's no longer relevant
|
||||
if "llm" in body:
|
||||
response["llm_provider"] = body["llm"] # Optionally keep track of provider
|
||||
|
||||
# Add dialout info for dialout scenarios
|
||||
if "dialout_settings" in body and len(body["dialout_settings"]) > 0:
|
||||
first_setting = body["dialout_settings"][0]
|
||||
if "phoneNumber" in first_setting:
|
||||
response["dialing_to"] = f"phone:{first_setting['phoneNumber']}"
|
||||
elif "sipUri" in first_setting:
|
||||
response["dialing_to"] = f"sip:{first_setting['sipUri']}"
|
||||
|
||||
return JSONResponse(response)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
# Check if this might be form data from Twilio
|
||||
try:
|
||||
content_type = request.headers.get("content-type", "").lower()
|
||||
if "application/x-www-form-urlencoded" in content_type:
|
||||
return await twilio_start_bot(request)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raise HTTPException(status_code=400, detail="Invalid JSON in request body")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Request processing error: {str(e)}")
|
||||
|
||||
|
||||
# ----------------- Main ----------------- #
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Check environment variables
|
||||
for env_var in REQUIRED_ENV_VARS:
|
||||
|
||||
Reference in New Issue
Block a user