Provided with a ctx.room, how do I find all partic...
# ask-ai
c
Provided with a ctx.room, how do I find all participants that are not the agent?
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you find all participants in a room who are not the agent! You can easily do this by using the
remoteParticipants
property of the
room
object. This collection includes everyone connected to the room except the agent, since the agent is represented separately by
localParticipant
. Let me share some examples in Python and Node.js to guide you through it: Python:
Copy code
# Loop through all participants except the agent
for participant in ctx.room.remote_participants.values():
    print(participant.identity)
Node.js:
Copy code
// Loop through all participants except the agent
for (const rp of ctx.room.remoteParticipants.values()) {
    console.log(rp.identity);
}
This way, you’re sure to access only the remote participants (those who aren’t the agent) in the room. If you have any more questions or need further clarification, feel free to ask—I’m here to help! Sources: Docs::Agents | Inside a session | LiveKit Docs | agents/src/job.ts
c
And how do I retrieve a participant's phone number?
t
Let's walk through how you can retrieve a participant's phone number, which varies depending on the participant type and your setup: • SIP Participants: For SIP participants, you can find the phone number in the
attributes["sip.phoneNumber"]
field, as long as the
hide_phone_number
setting isn’t turned on. This field shows the caller’s phone number for inbound calls or the number dialed for outbound calls. • Agent-Initiated Outbound Calls: When an agent starts an outbound call, the phone number is usually stored in
ctx.job.metadata
. You can get it like this:
Copy code
dial_info = json.loads(ctx.job.metadata)
phone_number = dial_info["phone_number"]
• Phone Number Management: If you’re managing assigned numbers, the
PhoneNumberService
API is a handy tool to list or get details about numbers linked to your account. Just a heads-up: non-SIP participants, such as web or mobile users, won’t have a phone number attribute unless you add it manually as custom metadata. Also, if
hide_phone_number
is enabled, the number won’t be visible. Sources: Docs::Telephony | SIP participant | LiveKit Docs | Docs::Telephony | livekit-protocol/src/livekit.rs | Agents telephony integration | LiveKit Docs
c
ctx.room.remote_participants.values(): print(participant.identity) There are no remote participants at the beginning of the call, presumably, because await ctx.connect() was not yet called. Workarounds?
t
Hi there! Just a quick heads-up: the
ctx.room.remote_participants
dictionary will be empty before you call
ctx.connect()
, since the agent hasn’t connected to the room yet. No worries though! Here are a few friendly steps to help you handle this smoothly: 1. Start by calling `ctx.connect()`: This will establish the connection to the room. Once connected, you can easily check for any participants already in the room: 2. Wait for participants to join: If you want to pause your code until someone joins, you can use
await ctx.wait_for_participant()
like this: 3. Set up a listener for participant events: This way, you’ll be notified whenever a new participant connects, making it easy to respond right away: By following these steps, you’ll be all set to handle both existing and new participants smoothly once your connection is up and running. If you have any questions or need a hand, I’m here to help! Sources: Docs::Agents | examples/other/transcription/multi-user-transcriber.py | complex-agents/drive-thru/tools/management_tools.py