How can I get the on_enter method of the agent to ...
# ask-ai
w
How can I get the on_enter method of the agent to start, only when I can check that the participant is active in the call, and the phone is not ringing still?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make sure the
on_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:
Copy code
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 Docs
w
ok, so where is the
async_on_participant_attributes_changed
function being called?
t
Hey there! So, the
async_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:
Copy code
room.on("participant_attributes_changed", on_participant_attributes_changed_handler)
And inside that handler, the function is called with:
Copy code
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?