This is an example of warm handoff from documentation. I want to dynamically create a unique room and add the caller and the new agent to so the real agent talks to caller. what to do?
@function_tool
async def transfer_call(self, context: RunContext, phone_number: str):
"""
Transfer the current call to a human agent at the specified phone number.
Args:
context: The call context
phone_number: The phone number to transfer the call to
"""
if not self.job_context:
await self.session.say("I'm sorry, I can't transfer the call at this time.")
return None, "Failed to transfer call: No job context available"
# Get room name from environment variable
room_name = os.environ.get('LIVEKIT_ROOM_NAME', self.job_context.room.name)
# Generate a unique identity for the SIP participant
identity = f"transfer_{uuid.uuid4().hex[:8]}"
# Create LiveKit API client
livekit_url = os.environ.get('LIVEKIT_URL')
livekit_api_key = os.environ.get('LIVEKIT_API_KEY')
livekit_api_secret = os.environ.get('LIVEKIT_API_SECRET')
sip_trunk_id = os.environ.get('SIP_TRUNK_ID')
try:
print(f"Transferring call to {phone_number}")
# Using the API from the job context if available
if self.job_context and hasattr(self.job_context, 'api'):
response = await self.job_context.api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id=sip_trunk_id,
sip_call_to=phone_number,
room_name=room_name,
participant_identity=identity,
participant_name="Human Agent",
krisp_enabled=True
)
)
else:
# Fallback to creating our own API client
livekit_api = api.LiveKitAPI(
url=livekit_url,
api_key=livekit_api_key,
api_secret=livekit_api_secret
)
response = await livekit_api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id=sip_trunk_id,
sip_call_to=phone_number,
room_name=room_name,
participant_identity=identity,
participant_name="Human Agent",
krisp_enabled=True
)
)
await livekit_api.aclose()
await self.session.say(f"I'm transferring you to a human agent now. Please hold while we connect you.")
return None, f"I've transferred you to a human agent at {phone_number}. Please hold while we connect you."
except Exception as e:
print(f"Error transferring call: {e}")
await self.session.say(f"I'm sorry, I couldn't transfer the call at this time.")
return None, f"Failed to transfer call: {e}"