We have some users who we want to transfer to a hu...
# telephony
c
We have some users who we want to transfer to a human agent immediately, instead of sending them to our LiveKit agent. I tried setting that up by creating a
TransferSIPParticipantRequest
after
ctx.connect
and
ctx.wait_for_participant
, but getting: `livekit.api.twirp_client.TwirpError: TwirpError(code=failed_precondition, message=twirp error unknown: can't transfer non established call, status=412)`Any ideas how to fix this? Not sure how to wait for the call to be "established". Code:
Copy code
async def entrypoint(ctx: JobContext):
    logger.info(f"connecting to room {ctx.room.name}")

    await ctx.connect()
    user_participant = await ctx.wait_for_participant()
    logger.info(
        f"participant joined, participant={user_participant}"
    )


    session_data, init_job_metadata = await get_init_userdata(
        ctx, user_participant
    )

    # Cold transfer if needed
    if session_data.transfer_on_callback:
        logger.info("transfer_on_callback is true, initiating cold transfer")
        async with api.LiveKitAPI() as livekit_api:
            transfer_to = "tel:" + env("TRANSFER_PHONE_NUMBER")

            transfer_request = TransferSIPParticipantRequest(
                participant_identity=user_participant.identity,
                room_name=ctx.room.name,
                transfer_to=transfer_to,
                play_dialtone=False,
            )
            logger.debug(f"Transfer request: {transfer_request}")

            await livekit_api.sip.transfer_sip_participant(transfer_request)
            logger.info(
                f"Successfully transferred participant {user_participant.identity} to {transfer_to}"
            )
        return
r
Try to wait for the SIP participant's call status to become
active
before attempting the transfer. sip_lifecycle.py:183-210 The
sip.callStatus
attribute indicates the current state of the call, and it needs to be
active
(not
dialing
or
ringing
) before a transfer can succeed.
👍 1
c
Looks like it's staying in
ringing
, I'm guessing I need to "answer" the call somehow. Is there a way to do that without setting up a full AgentSession?
r
You need SIP server waits for an audio track to be published to answer:
Copy code
async def entrypoint(ctx: JobContext):  
    await ctx.connect()  
    user_participant = await ctx.wait_for_participant()  
      
    # Answer the call by publishing a track  
    source = rtc.AudioSource(24000, 1)  
    track = rtc.LocalAudioTrack.create_audio_track("audio", source)  
    await ctx.room.local_participant.publish_track(track)  
      
    # Wait for call status to become active  
    while user_participant.attributes.get('sip.callStatus') != 'active':  
        await asyncio.sleep(0.1)  
      
    # Now transfer  
    await livekit_api.sip.transfer_sip_participant(transfer_request)
c
That works, thanks!