how to detect for outbound dial using create sip p...
# ask-ai
n
how to detect for outbound dial using create sip participant in the livekit agents, the user has picked up the phone call? right now I can’t detect when the user has picked up the phone
t
Thanks for reaching out, Tareq. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To detect when a user has picked up the phone during an outbound dial using
create_sip_participant
in LiveKit agents, you can use one of the following approaches: 1. Using the
wait_until_answered
Parameter
Set
wait_until_answered=True
in your
create_sip_participant
request. This will block the method until the call is answered:
Copy code
try:
    await ctx.api.sip.create_sip_participant(api.CreateSIPParticipantRequest(
        room_name=ctx.room.name,
        sip_trunk_id='ST_xxxx',
        sip_call_to=phone_number,
        participant_identity=sip_participant_identity,
        # This will wait until the call is answered before returning
        wait_until_answered=True,
    ))
    
    print("Call picked up successfully")
except api.TwirpError as e:
    print(f"Error creating SIP participant: {e.message}")
2. Monitoring Call Status with Polling Alternatively, you can monitor the dialing status by checking the
sip.callStatus
attribute on the participant:
Copy code
# Create SIP participant (returns immediately)
await ctx.api.sip.create_sip_participant(api.CreateSIPParticipantRequest(
    room_name=ctx.room.name,
    sip_trunk_id=outbound_trunk_id,
    sip_call_to=phone_number,
    participant_identity=user_identity,
))

# Wait for participant to be created
participant = await ctx.wait_for_participant(identity=user_identity)

# Monitor dialing status
start_time = perf_counter()
while perf_counter() - start_time < 30:
    call_status = participant.attributes.get("sip.callStatus")
    if call_status == "active":
        <http://logger.info|logger.info>("User has picked up")
        return
    elif call_status == "automation":
        # DTMF dialing in progress
        pass
    elif call_status == "hangup":
        <http://logger.info|logger.info>("User hung up, exiting job")
        break
    await asyncio.sleep(0.1)

<http://logger.info|logger.info>("Session timed out, exiting job")
Understanding Call Status Values • `active`: Participant is connected and the call is active (user picked up). • `automation`: For outbound calls using DTMF, indicates the call connected but is still dialing DTMF numbers. • `dialing`: Call is dialing and waiting to be picked up. • `hangup`: Call has been ended by a participant. • `ringing`: Inbound call is ringing for the caller. The
wait_until_answered=True
approach is simpler, while the polling approach gives you more control over the waiting process and allows you to handle different call states as needed. Sources: Agents telephony integration | LiveKit Docs | Making calls using SIP | LiveKit Docs | SIP participant | LiveKit Docs | Make outbound calls | LiveKit Docs