Hey I am working on a transfer call logic with War...
# ask-ai
r
Hey I am working on a transfer call logic with Warm call functionality using livekit. Where the agent actually transfers the call after providing bit of information to the receiving human agent on the other end.
t
Thanks for reaching out, Rajendra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To implement a warm call transfer functionality using LiveKit, you can follow these steps: Core Implementation: • Create an agent with a transfer function tool that uses the
create_sip_participant
API. This allows you to add a new SIP participant (the receiving human agent) to the same room as the current call. Steps to Create the SIP Participant: 1. Retrieve the required environment variables such as
LIVEKIT_URL
,
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
,
SIP_TRUNK_ID
, and optionally
LIVEKIT_ROOM_NAME
. 2. Use the
create_sip_participant
API to add the human agent to the room. This ensures the original call remains active while the new participant joins. 3. Provide a message to the caller, such as "I'm transferring you to a human agent now. Please hold while we connect you." Key Differences from Cold Transfer: • Warm transfer keeps the original call active in the same room, unlike cold transfer which ends the current session. • It allows for handoff coordination between the AI agent, caller, and human agent. • Session continuity is maintained until the handoff is complete. Required Environment Variables:
Copy code
LIVEKIT_URL=your-livekit-server-url
LIVEKIT_API_KEY=your-api-key
LIVEKIT_API_SECRET=your-api-secret
SIP_TRUNK_ID=your-sip-trunk-id
LIVEKIT_ROOM_NAME=your-room-name  # optional
This setup ensures a seamless warm handoff experience where the AI agent can introduce the human agent to the caller and facilitate a smooth transition. Sources: telephony/warm_handoff.py | Transferring calls | LiveKit Docs
r
okay so what is the use of function
transfer_sip_participant
?
@alert-easter-19258
t
Hey there! Let me break down the
transfer_sip_participant
function in LiveKit for you - it's a super handy tool for moving SIP calls around. Think of it like call forwarding, but with a bit more technical magic. 😊 What's This Function All About? • Imagine you want to smoothly move a SIP call to another phone number or endpoint - this function does exactly that! • It uses something called the SIP REFER method to basically "hand off" the active session. The Nuts and Bolts: • `room_name`: Where your call is starting from. • `participant_identity`: Who's making the move. • `transfer_to`: Where they're heading - could be a phone number like
tel:+15105550100
or a SIP address like
<sip:+15105550100@sip.telnyx>.com
. • `play_dialtone`: A fun little option to play a dial tone when the transfer starts. Real-World Uses: • Shuffling callers between different departments • Getting customers to the right support team • Setting up smart call forwarding Quick Pro Tips: • Just so you know, this transfer actually ends the LiveKit session for that participant. • Make sure your SIP provider is cool with transfers (like setting up Twilio SIP trunk transfers). • You'll need some specific permissions - SIP
call
grant and room admin access. Got questions? I'm all ears! Shoot me a message, and I'll help you navigate this. 👍
r
okay so the warm call transfer can not be done with the
transfer_sip_participant
function?
I want a proper example for the warm call transfer.
t
Hey there! I totally get why you might have thought about using
transfer_sip_participant
for a warm transfer, but it's actually designed for cold transfers only. No worries, though – I've got a great solution for you that'll help you smoothly transition calls! Let me walk you through a warm call transfer method that keeps things super simple and seamless. Warm Call Transfer Example:
Copy code
from livekit import api
import os
import uuid

# Required environment variables
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')
ROOM_NAME = os.environ.get('LIVEKIT_ROOM_NAME', 'default-room')

# Create a LiveKit API client
livekit_api = api.LiveKitAPI(
    url=LIVEKIT_URL,
    api_key=LIVEKIT_API_KEY,
    api_secret=LIVEKIT_API_SECRET
)

# Function to add a new SIP participant for warm transfer
def warm_transfer(phone_number):
    identity = f"transfer_{uuid.uuid4().hex[:8]}"
    try:
        response = 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
            )
        )
        print(f"Successfully transferred call to {phone_number}")
        return response
    except Exception as e:
        print(f"Error during warm transfer: {e}")
        return None

# Example usage
phone_number = "+15105550100"
warm_transfer(phone_number)
Key Points: • This approach is super cool because it keeps the original call active in the same room while adding a new SIP participant (the human agent). • The AI agent can introduce the human agent to the caller and make the transition super smooth. • Just make sure you've set up those environment variables correctly –
LIVEKIT_URL
,
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
, and
SIP_TRUNK_ID
are your best friends here! Need any more help or have questions? I'm right here to help you out! Just give me a shout, and we'll figure this out together. 😊 Sources: Transferring calls | LiveKit Docs | sip-refer/agent.py | Agents telephony integration | LiveKit Docs
r
I don't want caller to know what I am saying to the human agent. I want to first give the context to the human agent first and than only connect both of them
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
r
Thank you for this. Can you give me the logic for merging two rooms together?
@alert-easter-19258