Hi , I’m using Exotel for both outbound and inboun...
# ask-ai
s
Hi , I’m using Exotel for both outbound and inbound calls. For outbound calls, the agent can’t directly dial out through Exotel, so we implemented a workaround: when an outbound call is needed, we originate two separate calls—one to the user and one to the agent—so for the agent it effectively behaves like an inbound call. Both legs are then bridged so they can talk. Given this setup, I want to know: is it possible for the agent to initiate a call transfer? If so, what would be the recommended approach?
t
Thanks for reaching out, Ajay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking whether an agent can initiate a call transfer given your Exotel setup where outbound dialing is implemented as two bridged calls. Short answer: yes — but not as a traditional SIP REFER cold transfer. Use a warm-transfer pattern instead. What I’m responding to: your dual-call/bridge model (one call to user, one to agent) and whether the agent can initiate a transfer. Recommended approach (warm transfer) 1. Agent requests transfer in your UI (select target). 2. Server calls LiveKit to create a new outbound SIP participant to the transfer target (use your Exotel outbound trunk) and joins that party into the same LiveKit room. 3. Once the transferee answers, confirm connection, then remove the original agent from the room (or drop the original agent’s leg). 4. Optionally: place the caller on hold or play hold music while the transfer is attempted, and surface context/chat to the transferee before completing the handoff. Why this is recommended • Your agent leg is not a single SIP session that supports SIP REFER, so cold SIP transfer won’t work reliably with Exotel. • Creating a new outbound call and bridging it keeps control in LiveKit and avoids relying on Exotel to support SIP REFER. • This supports consultation/warm transfer flows, voicemail detection, and preserving call context. Key API/actions to use • Create an outbound SIP participant (dial transfer target) and join them into the existing room. • Mute/unmute or play hold audio as needed. • Disconnect the original agent after the transferee is connected. Requirements/considerations • Outbound SIP trunk configured for dialing via Exotel. • Handle caller ID/number presentation if you need to preserve original caller ID. • Plan for edge cases: unanswered transfer target, voicemail, or failed outbound call — provide fallback (requeue, notify agent, or try alternate target). • Optionally implement a consultation step so the agent can speak privately to the transferee before leaving. If you’d like, I can provide a concise code example showing how to 1) create the SIP participant to dial the transfer target and join the room, and 2) sequence hold + disconnect the agent after the transfer succeeds. Which server language do you prefer (Node, Python, or Go)? Sources: Preserving Caller ID During SIP Transfers in LiveKit Agents | How Vicidial Handles Bridging, Transfers, and SIP Calls | Agent-assisted warm transfer | LiveKit docs | SIP overview | LiveKit docs
s
in that case the agent has to initiate a call to the third person
t
Yes — that's correct: the agent needs to initiate a new outbound call to the third person (the transfer target) which is then bridged into the same LiveKit room (warm transfer). Would you like a concise code example for that flow? Which language do you prefer: Node, Python, or Go?
s
python code please
t
Below is a concise Python example showing the warm-transfer flow: agent requests transfer → server creates an outbound SIP participant (dials the third person via your Exotel trunk) → add transferee to the same LiveKit room → optionally put caller on hold → once connected, disconnect the agent. Replace placeholders with your values.
Copy code
# pip install livekit-client
from livekit import RESTClient
import time

LIVEKIT_API_KEY = "LIVEKIT_API_KEY"
LIVEKIT_API_SECRET = "LIVEKIT_API_SECRET"
LIVEKIT_URL = "https://<your-livekit-host>"   # e.g. https://api.livekit.cloud
ROOM_NAME = "example-room"
AGENT_PARTICIPANT_SID = "agent-participant-sid"  # participant identity or SID for agent
EXOTEL_SIP_TRUNK_ID = "exotel-trunk-id"
TRANSFER_TARGET_NUMBER = "+1234567890"
TRANSFEREE_IDENTITY = f"transferee-{TRANSFER_TARGET_NUMBER}"

rest = RESTClient(url=LIVEKIT_URL, api_key=LIVEKIT_API_KEY, api_secret=LIVEKIT_API_SECRET)

def put_participant_on_hold(room_name: str, participant_identity: str, hold: bool = True):
    # Simple approach: mute participant's audio so caller hears hold music instead.
    # Alternatively, use server-side audio injection for hold music.
    rest.update_participant(room_name, participant_identity, metadata=None, hidden=None,
                            muted_audio=hold)

def create_sip_participant_and_bridge(room_name: str, target_number: str, transferee_identity: str):
    # Create a SIP participant that dials the target_number via configured SIP trunk.
    # wait_for_answer instructs LiveKit to wait until the SIP leg answers before returning connected participant info.
    resp = rest.create_sip_participant(
        room=room_name,
        sip_trunk_id=EXOTEL_SIP_TRUNK_ID,
        sip_call_to=target_number,
        participant_identity=transferee_identity,
        wait_until_answered=True
    )
    return resp  # contains participant info and connection state

def disconnect_participant(room_name: str, participant_identity: str):
    rest.disconnect_participant(room_name, participant_identity)

# Warm transfer flow
def warm_transfer(room_name: str, agent_identity: str, caller_identity: str, target_number: str):
    # 1) Optionally put caller on hold
    put_participant_on_hold(room_name, caller_identity, hold=True)

    # 2) Dial transfer target and bridge into room
    transferee = create_sip_participant_and_bridge(room_name, target_number, TRANSFEREE_IDENTITY)
    if not transferee or transferee.get("state") != "joined":
        # failed to connect transferee — restore hold state and return failure
        put_participant_on_hold(room_name, caller_identity, hold=False)
        return {"ok": False, "reason": "transferee_not_connected"}

    # 3) Optionally let agent consult privately with transferee:
    #    - Mute caller so agent and transferee can speak, or move agent & transferee to a private room for consultation.
    rest.update_participant(room_name, caller_identity, muted_audio=True)

    # (Optional) wait for agent confirmation that transfer is ready
    time.sleep(1)

    # 4) Remove agent from the room (complete the transfer)
    disconnect_participant(room_name, agent_identity)

    # 5) Restore caller audio (now connected to transferee)
    rest.update_participant(room_name, caller_identity, muted_audio=False)

    return {"ok": True, "transferee": transferee}

# Example usage
result = warm_transfer(ROOM_NAME, AGENT_PARTICIPANT_SID, "caller-identity", TRANSFER_TARGET_NUMBER)
print(result)
Notes: • Ensure your LiveKit REST client supports these methods and your API credentials/trunk are configured. • Use hold music by injecting audio or using an audio track/source if muting isn't sufficient. • Handle edge cases: unanswered transfer, voicemail, failed outbound call — provide fallback behavior. • If you want, I can adapt this to your exact LiveKit client library version or show a private consultation flow (move to a separate room).