``` @ctx.room.on("participant_connected") d...
# agents
a
Copy code
@ctx.room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
why this event does not always trigger?
r
What do you mean. It does.
a
i dont see any print statements. @ctx.room.on("participant_connected") def on_participant_connected(participant: rtc.RemoteParticipant): print("=======================================") print(f"new participant connected {participant}") on web it doesnt in dev mode
r
How are you testing it?
a
by web and telephony.
r
So you make a call and the agent is dispatched into the room with the call. Then later you have a browser join the room and you do not see the event?
a
the disconnect is working fine but not the
participant_connected
r
I use it all the time and has been fine for me.
a
i just have 1 agent and 1 participant
r
how do you try and generate the participant connected in that case?
a
idk actually i want to log the call time when im connected to the agent
r
what does that have to do with
participant_connected
?
a
i thought i will log the time. anyways i want to know how can i know that the user joined coming from web or telephony?
i want to have the number from which the user joins
please help.
r
look at the participant to see what kind it is
a
thats why im using it but its not tiggering
Copy code
from dotenv import load_dotenv

from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions,CloseEvent
from livekit.plugins import (
    openai,
)
from livekit import rtc

load_dotenv()


class Assistant(Agent):
    def __init__(self) -> None:
        super().__init__(instructions="You are a helpful voice AI assistant. Always speak in english language no matter what.")


async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()

  
    
    session = AgentSession(
        llm=openai.realtime.RealtimeModel(
            voice="coral"
        )
    )

    await session.start(
        room=ctx.room,
        agent=Assistant(),
  
    )
    
    @ctx.room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
        print("=======================================")
        print(f"new participant connected {participant}")
 
    @ctx.room.on("participant_disconnected")
    def on_participant_disconnected(participant: rtc.RemoteParticipant):
        print("=======================================")
        print(f"new participant disconnected {participant}")
 




if __name__ == "__main__":
    agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint,agent_name="saudia-agent"))
please check.
r
I don't see where you are doing it. I only see it in the events there.
a
the participant is not being printed. sometimes it does.
r
you are not printing it
a
can u tell if this is correct code?
r
There is no code there to detect participant kind
so not correct
a
i will see
r
This only gets called if a participant joins a room. Not if they are already in there
ctx.room.on("participant_connected")
a
suppose. im just the only one wants to speak to agent. will it trigger?
r
Agent only listens to a single primary particiant
a
in this code. when i connect from web or telephony. this
@room.on("participant_connected")
doesnt work
Copy code
from dotenv import load_dotenv

from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions, CloseEvent
from livekit.plugins import (
    openai,
)
from livekit import rtc

load_dotenv()


class Assistant(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="You are a helpful voice AI assistant. Always speak in english language no matter what."
        )


async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()
    room = ctx.room

    @room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
        print("participant connected: %s %s", participant)

    session = AgentSession(llm=openai.realtime.RealtimeModel(voice="coral"))

    await session.start(
        room=ctx.room,
        agent=Assistant(),
    )

    @ctx.room.on("participant_disconnected")
    def on_participant_disconnected(participant: rtc.RemoteParticipant):
        print("=======================================")
        print(f"new participant disconnected {participant}")


if __name__ == "__main__":
    agents.cli.run_app(
        agents.WorkerOptions(entrypoint_fnc=entrypoint, agent_name="saudia-agent")
    )
but the disconnect works everytime
r
participant_connected
works every time too. In your use case you do not have a participant connecting.
a
the example u sent me is not of entrypoint function. i want it in entrypoint function.
😀 1
i want to use the ctx.room.on method
Copy code
async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()

    @ctx.room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
        print("participant connected: %s %s", participant)
r
not sure how many ways I can tell you that won't work...
a
ok sorry. but it worked sometimes. and then why the ctx.room("disconnect") works
r
Get the participants in the room and see if the one you want is there.
Because that is when a participant leaves
a
i will try. anyways can u tell me that do we have to use the ctx.connect() in the top or at the bottom . inside the entrypoint function. does it matter?
r
If you have an agent session you won't need that.
What is your use case. What are you trying to do?
You should do something like this.
a
from the telephony . the participant connected event is working. but from web its not.
Copy code
async def entrypoint(ctx: agents.JobContext):
    print(ctx.room)
    @ctx.room.on("participant_connected")
    def on_participant_connected(participant: rtc.RemoteParticipant):
        print("participant connected==========================: %s %s", participant)
    @ctx.room.on("participant_disconnected")
    def on_participant_disconnected(participant: rtc.RemoteParticipant):
        print(f"new participant disconnected==========================: {participant}")