clever-doctor-15083
06/26/2025, 10:03 AMtall-airline-16756
06/26/2025, 10:03 AMtall-airline-16756
06/26/2025, 10:03 AMclever-doctor-15083
06/26/2025, 10:05 AMtall-airline-16756
06/26/2025, 10:06 AMtransfer_sip_participant
API that'll make call transfers a breeze.
@function_tool()
async def transfer_call(self, participant_identity: str, transfer_to: str) -> None:
"""
Transfer the SIP call to another number.
Args:
participant_identity (str): The identity of the participant.
transfer_to (str): The phone number to transfer the call to.
"""
try:
userdata = self.session.userdata
if not userdata.livekit_api:
livekit_url = os.getenv('LIVEKIT_URL')
api_key = os.getenv('LIVEKIT_API_KEY')
api_secret = os.getenv('LIVEKIT_API_SECRET')
userdata.livekit_api = api.LiveKitAPI(
url=livekit_url,
api_key=api_key,
api_secret=api_secret
)
transfer_request = proto_sip.TransferSIPParticipantRequest(
participant_identity=participant_identity,
room_name=userdata.ctx.room.name,
transfer_to=transfer_to,
play_dialtone=True
)
await userdata.livekit_api.sip.transfer_sip_participant(transfer_request)
except Exception as e:
logger.error(f"Failed to transfer call: {e}", exc_info=True)
await self.session.generate_reply(user_input="I'm sorry, I couldn't transfer your call. Is there something else I can help with?")
And hey, we can even get more specific! Check out how you can create department-specific transfer methods. For instance, transferring to billing is super simple:
@function_tool()
async def transfer_to_billing(self, context: RunContext_T) -> str:
"""Transfer the call to the billing department."""
room = context.userdata.ctx.room
identity = room.local_participant.identity
transfer_number = f"tel:{os.getenv('BILLING_PHONE_NUMBER')}"
dept_name = "Billing"
context.userdata.selected_department = dept_name
await self._handle_transfer(identity, transfer_number, dept_name)
return f"Transferring to {dept_name} department."
Quick heads up – there are a few things you'll want to keep in mind:
• You must enable SIP REFER on your SIP trunk provider to use this functionality.
• For Twilio, you'll also need to enable "Enable PSTN Transfer".
• The transfer can be to a phone number (format: tel:+15105550123
) or a SIP destination (format: sip:user@host
).
Need more help? I'm right here! Just give me a shout, and we'll figure it out together. 😊 Sources: Company directory phone assistant | LiveKit Docs | Agents telephony integration | LiveKit Docs | GitHub::livekit::agents | livekit-agents/livekit/agents/job.py