how do this work : def on_participant_attributes_c...
# ask-ai
v
how do this work : def on_participant_attributes_changed(attributes): print(f”inside on_participant_attributes_changed and {attributes}“) call_status = attributes.get(“sip.callStatus”) if call_status == “active”: print(‘user has picked call’) logger.info(“User has picked up the call”) # Create a copy of the chat context and add the message new_chat_ctx = agent.chat_ctx.copy() new_chat_ctx.add_message(role=“user”, content=[“hello”]) asyncio.create_task(agent.update_chat_ctx(new_chat_ctx))
t
Thanks for reaching out, Ravi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
on_participant_attributes_changed
function is a helpful event handler in LiveKit that kicks in whenever a participant's attributes get updated in a room. It’s a great way to stay in the loop and respond to changes in a participant’s key-value state, which everyone in the room can see. Here’s a friendly breakdown of how it works: • The handler takes in two parameters:
changed_attributes
(a dictionary showing which keys and values were updated) and
participant
(the
Participant
object whose attributes changed). • You update attributes using the
set_attributes
method, and these changes automatically sync across all participants. • If you want to delete an attribute, just set its value to an empty string—it’s that simple! • Keep in mind, attributes aren’t meant for very frequent updates (like more than once every few seconds). For those, using data packets is a better choice. • Also, participants need the
canUpdateOwnMetadata
permission in their access token to update their own attributes. Here’s a gentle example in Python to illustrate:
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes: dict[str, str], participant: rtc.Participant):
    logging.info("participant attributes changed: %s %s", participant.attributes, changed_attributes)
This will log both the participant’s full current attributes and the specific changes made. If you have any questions or want me to walk you through anything else, just let me know—I’m here to help! Sources: livekit-rtc/livekit/rtc/room.py | Participant attributes and metadata | LiveKit Docs