rhythmic-jordan-68259
08/03/2025, 8:15 AMtall-airline-16756
08/03/2025, 8:15 AMtall-airline-16756
08/03/2025, 8:16 AMtel:+15105550100
◦ <sip:+15105550100@sip.telnyx>.com
For more details, refer to the LiveKit SIP documentation or let me know if you need further assistance.rhythmic-jordan-68259
08/03/2025, 8:38 AMtall-airline-16756
08/03/2025, 8:39 AMimport os
import asyncio
from livekit import api
from livekit.protocol import sip as proto_sip
async def transfer_call(participant_identity: str, transfer_to: str):
"""
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:
# Initialize LiveKit API client
livekit_url = os.getenv('LIVEKIT_URL')
api_key = os.getenv('LIVEKIT_API_KEY')
api_secret = os.getenv('LIVEKIT_API_SECRET')
livekit_api = api.LiveKitAPI(
url=livekit_url,
api_key=api_key,
api_secret=api_secret
)
# Create transfer request
transfer_request = proto_sip.TransferSIPParticipantRequest(
participant_identity=participant_identity,
room_name="your_room_name",
transfer_to=transfer_to,
play_dialtone=True
)
# Perform transfer
await livekit_api.sip.transfer_sip_participant(transfer_request)
print(f"Successfully transferred participant {participant_identity} to {transfer_to}")
except Exception as e:
print(f"Failed to transfer call: {e}")
Configuration
1. Environment Variables: Just set these up in your .env file: LIVEKIT_URL=<wss://your-livekit-url.livekit.cloud>
LIVEKIT_API_KEY=your-livekit-api-key
LIVEKIT_API_SECRET=your-livekit-api-secret
2. Telnyx SIP Trunk Configuration: Here's what you need to do: You can find all the detailed steps in the Telnyx SIP trunk configuration guide.
◦ Purchase a Telnyx phone number
◦ Create an FQDN connection
◦ Associate your phone number with the trunk
Handling DTMF Input
If you want to handle DTMF input and kick off a transfer based on what the user presses, here’s a handy example:
@room.on("sip_dtmf_received")
def handle_dtmf(dtmf_event):
digit = dtmf_event.digit
identity = dtmf_event.participant.identity
department_numbers = {
"1": ("BILLING_PHONE_NUMBER", "Billing"),
"2": ("TECH_SUPPORT_PHONE_NUMBER", "Tech Support"),
"3": ("CUSTOMER_SERVICE_PHONE_NUMBER", "Customer Service")
}
if digit in department_numbers:
env_var, dept_name = department_numbers[digit]
transfer_number = f"tel:{os.getenv(env_var)}"
asyncio.create_task(transfer_call(identity, transfer_number))
else:
print("Oops! That’s not a valid option. Please try again.")
If you have any questions or need a hand with anything else, just let me know—I’m here to help!