wide-dress-57902
08/08/2025, 11:26 PMtall-airline-16756
08/08/2025, 11:26 PMtall-airline-16756
08/08/2025, 11:26 PMon_enter method of the agent only runs when the participant is actually active in the call (and not just when the phone is ringing), you can check the participant's sip.callStatus attribute to see what's going on with the call.
Here’s a simple way to do it:
1. Keep an eye on Call Status: Listen for the participant_attributes_changed event to catch any updates to the SIP participant's sip.callStatus.
2. Check the Status: Before triggering on_enter, make sure the call status is active.
3. Run on_enter only when it counts: Execute the on_enter method only if the call is truly in the active state.
Here’s an example to illustrate this:
class MyAgent(Agent):
async def on_enter(self):
# Your custom logic here
self.session.generate_reply()
async def async_on_participant_attributes_changed(self, 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 == 'active':
# Trigger on_enter only when the call is active
await self.on_enter()
Here’s what’s happening:
• Listening for changes: The async_on_participant_attributes_changed method gets called whenever the participant’s attributes update. This is where you check if sip.callStatus has switched to active.
• Confirming the call state: By checking call_status == 'active', you make sure on_enter only fires when the participant is really connected, not just ringing or dialing.
• Triggering on_enter: Once you know the call is active, you call on_enter to run any setup or greeting you want.
This way, the on_enter method runs only when it truly matters—when the call is active—so you avoid unnecessary triggers during ringing or other states.
Sources: telephony/warm_handoff.py | Pipeline nodes and hooks | LiveKit Docswide-dress-57902
08/08/2025, 11:28 PMasync_on_participant_attributes_changed function being called?tall-airline-16756
08/08/2025, 11:29 PMasync_on_participant_attributes_changed function gets called whenever the participant_attributes_changed event happens. This event pops up anytime a participant’s attributes get updated.
To give you a clearer picture, this function runs asynchronously as part of setting up the event handler. For instance, if you peek inside the telephony/sip_lifecycle.py file in the livekit-examples/python-agents-examples repo, you’ll see the event handler set up like this:
room.on("participant_attributes_changed", on_participant_attributes_changed_handler)
And inside that handler, the function is called with:
asyncio.create_task(async_on_participant_attributes_changed(changed_attributes, participant))
This way, every time the event happens, your function kicks in, letting you handle any changes in participant attributes—like when sip.callStatus gets updated. Pretty handy, right?