when using telnyx Programmable Voice, and livekit ...
# ask-ai
r
when using telnyx Programmable Voice, and livekit , how we can transfer calls to livekit without calling the transfer call api?
t
Thanks for reaching out, Umair. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To transfer calls from Telnyx Programmable Voice to LiveKit without using the Transfer Call API, you can use the SIP REFER method. Here’s how: • Set Up SIP Trunk: Configure a SIP trunk with Telnyx and LiveKit to route calls. Follow the Telnyx SIP trunk setup guide for configuration. • Enable Call Transfers: Ensure your SIP trunk is configured to allow call transfers. For example, enable call transfers for your trunk using the appropriate settings or commands. • Use SIP REFER: SIP REFER is a standard SIP method that allows call transfers. LiveKit supports SIP REFER for transferring calls to any valid telephone number or SIP URI. Example formats for the transfer destination include: ◦
tel:+15105550100
<sip:+15105550100@sip.telnyx>.com
For more details, refer to the LiveKit SIP documentation or let me know if you need further assistance.
r
could you provide configuration and python code for this
t
Here is the configuration and Python code to transfer calls from Telnyx Programmable Voice to LiveKit using SIP REFER: Python Code Example
Copy code
import 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:
Copy code
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:
Copy code
@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!