How can I track when the call is completed to sent...
# ask-ai
f
How can I track when the call is completed to sent an SMS message after the call?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To keep track of when a call finishes and send an SMS right after, here are a few ways you can do it: • Use the OnSessionEnd SIP Handler: This handy callback kicks in when a SIP call ends. It gives you useful details like the call’s end time and who was on the call, which you can then use to send an SMS through services like Twilio or AWS SNS. • Keep an eye on sip.callStatus: Whether it’s a SIP call or not, you can watch the
sip.callStatus
of the participants. When it switches to
hangup
, that’s your cue to send the SMS. • AI Agent end_call Function: If you’re working with AI agents, you can set up a custom
end_call
function to send an SMS once the AI decides the conversation is wrapped up. Would you like me to walk you through the steps or share some code examples for any of these? Just let me know! Sources: livekit-protocol/livekit/protocol/sip.pyi | pkg/sip/inbound.go | SIP participant | LiveKit Docs | Making calls using SIP | LiveKit Docs | Community & Support | livekit/livekit_composite | DeepWiki
f
can you provide an example for AI Agent end_call Function?
t
Here’s a friendly example of how you can implement the AI Agent end_call function to send an SMS right after the call ends:
Copy code
@llm.ai_callable()
def end_call(self):
    """Called when the user wants to end the call"""
    logger.info("User requested to end the call")

    # End the call by removing the participant
    await self.hangup()

    # Send an SMS after the call ends
    await self.send_sms(
        phone="+1234567890",  # Just swap this out with the recipient's phone number
        message="Thank you for your call. If you have further questions, feel free to reach out!"
    )
Here are the steps to get this going: 1. Make sure your AI agent knows when the user wants to end the call. 2. Swap the
phone
parameter with the right phone number, which you can grab from participant info or your database. 3. Set up the
send_sms
method to work with an SMS service like Twilio or AWS SNS. If you want, I can also share an example of the
send_sms
method for Twilio integration—just let me know!
f
can I define the end of call using room.on or session.on callbacks?
t
Hey there! You can easily define the end of a call using
room.on
or
session.on
callbacks in LiveKit. Here are some key events that might help you out: • `room.on("participant_disconnected")`: This one fires when a participant leaves the room. It’s great for spotting when everyone’s gone and cleaning things up. • `session.on("close")`: This triggers when an agent’s session ends, which usually means the call is over. Perfect for tidying up, like deleting the room. Here’s a quick example for `room.on("participant_disconnected")`:
Copy code
def _on_participant_disconnected(_: rtc.Participant):
    if len(ctx.room.remote_participants) == 0:
        print("All participants have left the room.")

ctx.room.on("participant_disconnected", _on_participant_disconnected)
And here’s how you might use `session.on("close")`:
Copy code
@session.on("close")
def on_close(ev):
    print(f"Session closed, reason: {ev.reason}")
    ctx.delete_room()  # Optionally delete the room
If you want, I’m here to help you set these up or answer any questions you have! Sources: livekit-rtc/livekit/rtc/room.py | examples/voice_agents/realtime_joke_teller.py | Agents telephony integration | LiveKit Docs | complex-agents/ivr-agent/ivr-agent-frontend/src/App.tsx
f
How can I distinguish if user newer joined the call or if it was some conversation?
t
Hey there! To figure out if a user never joined a call or if there was actually a conversation, here are a couple of easy ways you can check: • Keep an eye on `sip.callStatus`: ◦ If it stays at
incoming
, that means the user didn’t join the call. ◦ If it switches to
active
, then the user joined and there was a conversation. • Look at Participant Events: ◦ The
PARTICIPANT_JOINED
event tells you the user entered the room. ◦ The
PARTICIPANT_ACTIVE
event shows the user actually engaged in the call. ◦ If the user leaves before hitting the
ACTIVE
state, it’s likely no meaningful conversation took place. Need any more details or some examples on how to set this up? Just let me know—I’m happy to help! Sources: Voice Assistants | livekit/livekit_composite | DeepWiki | Voice Assistants | livekit/livekit_composite | DeepWiki | livekit/livekit_sip.pb.go | livekit-protocol/src/livekit.rs | Making outbound calls | LiveKit Docs
f
How do I look at participant events?
t
If you want to keep an eye on participant events in LiveKit, here are a few friendly ways to do it: • Direct Event Listeners on Participant Objects: You can attach event listeners to each participant to catch changes like
TrackMuted
,
IsSpeakingChanged
, or
ConnectionQualityChanged
. Here’s a simple JavaScript example:
Copy code
participant.on(ParticipantEvent.TrackMuted, (publication) => {
  console.log('Track muted:', publication.trackSid);
});
• Room-Level Events: Another way is to use the
Room
object to watch all participants at once. Here’s how you might do it in Python:
Copy code
@room.on("track_published")
def on_track_published(publication, participant):
    print(f"Track published by {participant.identity}")
• Server-Side Monitoring: And if you’re working on the backend, webhooks are great for getting events like
participant_joined
and
participant_left
. If you’d like, I can share more details or examples for any of these methods—just let me know! Sources: Handling events | LiveKit Docs | examples/demo/demo.ts | src/useParticipant.ts | examples/basic_room.py | lib/src/context/participant_context.dart
f
To sum up, what are the options to sent SMS after the phone call only if there were real conversation with the user?
t
Hey there! If you want to send an SMS after a phone call—but only when there was a real conversation with the user—here are some friendly options you can try out: 1. Keep an eye on `sip.callStatus`: ◦ When the status switches to
active
, it means the user joined and had a chat. You can then go ahead and trigger the SMS once the call wraps up. 1. Use Participant Events: ◦ Watch for the
PARTICIPANT_ACTIVE
event to make sure the user actually took part in the call. If you see this, you can send the SMS after the
PARTICIPANT_LEFT
event. 1. OnSessionEnd Callback: ◦ For SIP calls, try using the
OnSessionEnd
handler to peek at call details like
ended_at
and
disconnect_reason
. Pair this with
sip.callStatus
to double-check a conversation happened before sending your SMS. If you'd like, I can walk you through how to set up any of these methods—just let me know!
f
does sip.callStatus stays active till the end of the session?
t
Just a heads-up: the
sip.callStatus
attribute doesn’t stay
active
for the entire session. It actually updates to show the current state of the SIP call as things happen. For example: • When the call connects, the status switches to
active
. • Once the call ends, it changes to
hangup
. So, by keeping an eye on
sip.callStatus
, you’ll know right when the call moves to
hangup
, meaning the session is over. Sources: SIP participant | LiveKit Docs | telephony/sip_lifecycle.py