square-flag-66304
12/04/2025, 6:16 AMtall-airline-16756
12/04/2025, 6:16 AMtall-airline-16756
12/04/2025, 6:17 AMsquare-flag-66304
12/04/2025, 6:19 AMtall-airline-16756
12/04/2025, 6:19 AMsquare-flag-66304
12/04/2025, 6:19 AMtall-airline-16756
12/04/2025, 6:20 AM# 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).