Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Transfer SIP Calls to Another Number in voice-pipeline-agent-python? #1139

Closed
ericyue opened this issue Nov 27, 2024 · 3 comments
Closed
Labels
question Further information is requested

Comments

@ericyue
Copy link

ericyue commented Nov 27, 2024

Hello,

I am currently working on a simple demo using voice-pipeline-agent-python and have successfully configured the SIP settings as required.
However, I am looking to implement a feature where, after a user dials a number and enters the logic of the VoicePipelineAgent, they can trigger a signal that redirects the call to another number for manual response.

Here's what I have so far, and I'm wondering how I can proceed to achieve the call transfer functionality:

The user dials a number and is connected to the VoicePipelineAgent.

At some point during the interaction, the user triggers a specific signal.
Upon receiving this signal, I want the call to be transferred to another predefined number for human manual handling.

I have reviewed the documentation, but I'm not sure how to implement this feature within the voice-pipeline-agent-python framework. Could someone please guide me on the steps or provide an example of how to achieve this?

Thank you in advance for your help!

@ericyue ericyue added the question Further information is requested label Nov 27, 2024
@narendra-bluebash
Copy link

I am also being faced same problem plz solve this as soon as possible

@davidzhao
Copy link
Member

Like in the cold transfer docs, you could do the following

# assuming ctx is JobContext, participant is the user (i.e. ctx.wait_for_participant())

livekit_api = api.LiveKitAPI()

transfer_to = '+14155550100'

# Create transfer request
transfer_request = TransferSIPParticipantRequest(
    # indicate the participant to transfer out
    participant_identity=participant.identity,
    # name of the current room
    room_name=ctx.room.name,
    transfer_to=transfer_to,
)
logger.debug(f"Transfer request: {transfer_request}")

# Transfer caller
await livekit_api.sip.transfer_sip_participant(transfer_request)

@shwetd19
Copy link
Contributor

shwetd19 commented Dec 27, 2024

Hello,

I am attempting to handle call transfers using LiveKit in a voice assistant. The assistant listens for user responses (yes or no) and initiates a transfer to the billing department when the response is affirmative. Despite following the documentation and implementing the SIP transfer process, the call transfer does not happen.

Previously, I was able to handle call transfers successfully using DTMF handling, where the assistant listens for keypad inputs (e.g., pressing a specific number) and responds accordingly. However, the same process does not seem to work with voice-triggered responses.

Expected Behavior

The call should be transferred to the billing department's phone number provided in the .env.local file as BILLING_PHONE_NUMBER.

Observed Behavior

The assistant verbally confirms the transfer, but the actual SIP transfer does not take place. The call remains in the current room, and no error logs indicate why the transfer failed.


Code Snippets

1. Handling Call Transfer
Below is the code snippet for initiating the SIP call transfer:

async def handle_transfer(room_name: str, participant_identity: str, assistant: VoiceAssistant) -> None:
    try:
        transfer_number = os.getenv('BILLING_PHONE_NUMBER')
        if not transfer_number:
            raise ValueError("Billing phone number not configured")

        if not transfer_number.startswith('+'):
            transfer_number = f"+{transfer_number}"

        await assistant.say("Transferring you to our billing department. Please hold.", allow_interruptions=False)

        livekit_api = api.LiveKitAPI(
            url=os.getenv('LIVEKIT_URL'),
            api_key=os.getenv('LIVEKIT_API_KEY'),
            api_secret=os.getenv('LIVEKIT_API_SECRET')
        )

        transfer_uri = f"tel:{transfer_number}"
        transfer_request = proto_sip.TransferSIPParticipantRequest(
            room_name=room_name,
            participant_identity=participant_identity,
            transfer_to=transfer_uri,
            play_dialtone=False
        )

        logger.info(f"Initiating transfer for participant {participant_identity} to {transfer_uri}")
        await livekit_api.sip.transfer_sip_participant(transfer_request)
        await asyncio.sleep(2)
        await assistant.cleanup()
        logger.info("Transfer completed, exiting process")
        os._exit(0)
    except Exception as e:
        logger.error(f"Transfer failed: {e}", exc_info=True)
        await assistant.say("I apologize, but I couldn't transfer your call. Please try again later.", allow_interruptions=True)

2. Handling yes/no Responses
Below is the implementation for handling user responses and deciding whether to transfer the call:

@assistant.on_message
async def handle_message(message: str):
    message = message.lower().strip()
    
    if message in ["yes", "yeah", "sure", "okay", "correct", "yep"]:
        participants = list(ctx.room.participants.values())
        if not participants:
            logger.error("No participants found in room")
            await assistant.say("I'm sorry, but I cannot process the transfer at this moment.", allow_interruptions=True)
            return

        participant = participants[0]
        logger.info(f"Attempting transfer for participant: {participant.identity}")
        await handle_transfer(ctx.room.name, participant.identity, assistant)
        
    elif message in ["no", "nope", "not now", "nah"]:
        await assistant.say("Alright, is there something else I can help you with?", allow_interruptions=True)
    else:
        await assistant.say("Would you like me to transfer you to our billing department? Please say 'yes' or 'no'.", allow_interruptions=True)

What I Tried

  • Attempted debugging the transfer_sip_participant call, but found no relevant error logs.

Questions

  1. Is the SIP transfer feature supported for this use case in LiveKit?
  2. Are there additional configurations or permissions required for SIP transfers to work?
  3. Could there be a potential issue with the LiveKit SDK or API?
  4. Why does the transfer work as expected when using DTMF handling (listening for keypad inputs) but fails when using yes/no voice responses? Are there additional steps or considerations required for voice-based triggers?

Environment

  • LiveKit SDK Version: (Please specify)
  • Python Version: (e.g., 3.10)
  • OS: (e.g., Ubuntu 22.04)

Any insights or guidance would be greatly appreciated! Let me know if you need additional logs or context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants