How to get call status in case a call is missed or...
# ask-ai
b
How to get call status in case a call is missed or declined using twilio sip
t
Thanks for reaching out, G. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get the call status for missed or declined calls using Twilio SIP with LiveKit, you can monitor the
sip.callStatus
participant attribute and listen for participant events. Here’s how you can track call status: Monitoring Call StatusYou can check the call status by accessing the
sip.callStatus
attribute on SIP participants. The available call status values include: • `active`: Participant is connected and the call is active • `dialing`: Call is dialing and waiting to be picked up • `ringing`: Inbound call is ringing for the caller • `hangup`: Call has been ended by a participant • `automation`: For outbound calls using DTMF, call connected but still dialing DTMF numbers Implementation Approaches 1. Polling Call Status
Copy code
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 == "hangup":
        <http://logger.info|logger.info>("user hung up, exiting job")
        break
    await asyncio.sleep(0.1)
2. Event-Based Monitoring You can listen to participant events to track call status changes: ◦ If the
sip.callStatus
participant attribute is updated to
active
, the call has connected. ◦ If the call fails, the participant is disconnected and leaves the room. 1. Comprehensive Event Handling For more detailed monitoring, you can set up event handlers to track attribute changes:
Copy code
async def async_on_participant_attributes_changed(changed_attributes: dict, participant: rtc.Participant):
    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        if 'sip.callStatus' in changed_attributes:
            call_status = changed_attributes['sip.callStatus']
            if call_status == 'hangup':
                <http://logger.info|logger.info>("Call has been ended by a participant")
Twilio-Specific InformationFor Twilio SIP trunks, additional attributes are available: • `sip.twilio.accountSid`: Twilio account SID • `sip.twilio.callSid`: Twilio call SID These attributes can help you correlate LiveKit call events with Twilio’s call records for more detailed analysis. LimitationsWhile the above methods allow you to track call status changes, distinguishing between "missed" and "declined" calls specifically is not explicitly detailed in the available SIP attributes. The
hangup
status indicates that the call has ended, but further differentiation may require additional logic or integration with Twilio’s call logs. Sources: SIP participant | LiveKit Docs | Making calls using SIP | LiveKit Docs | Making outbound calls | LiveKit Docs | telephony/sip_lifecycle.py | SIP participant | LiveKit Docs