How do send a message when user status is away?
# ask-ai
f
How do send a message when user status is away?
t
Thanks for reaching out, Fred. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to send a message when a user's status switches to "away" in LiveKit, you can easily do this by using the
user_state_changed
event handler in your agent session. Here’s a quick rundown: 1. Set Up User Away Detection: You can customize how long it takes to detect when someone goes away by adjusting the
user_away_timeout
parameter in your
AgentSession
. 2. Add the Event Handler: Just create an event handler that listens for the
user_state_changed
event. When the user’s status flips to "away," you can kick off a task to send them a message. 3. Send Friendly Messages: Set up a task that sends a few polite check-in messages to see if the user is still around. For example, you might send up to three gentle reminders and then close the session if there’s no reply. If you want me to share some detailed code examples or help you out further, just give me a shout! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | Events and error handling | LiveKit Docs | Agents Core Architecture | livekit/livekit_composite | DeepWiki
f
Yes, can you give me code examples
t
Here’s a complete code example to help you handle sending messages when a user’s status switches to "away":
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(
        vad=silero.VAD.load(),
        llm=openai.LLM(model="gpt-4o-mini"),
        stt=deepgram.STT(),
        tts=cartesia.TTS(),
        user_away_timeout=12.5,  # Custom timeout in seconds
    )

    inactivity_task: asyncio.Task | None = None

    async def user_presence_task():
        # We'll try pinging the user 3 times; if there's no response, we'll close the session gently
        for _ in range(3):
            await session.generate_reply(
                instructions=(
                    "The user has been inactive. Politely check if the user is still present."
                )
            )
            await asyncio.sleep(10)

        await asyncio.shield(session.aclose())
        ctx.delete_room()

    @session.on("user_state_changed")
    def _user_state_changed(ev: UserStateChangedEvent):
        nonlocal inactivity_task
        if ev.new_state == "away":
            inactivity_task = asyncio.create_task(user_presence_task())
            return

        # If the user comes back, we’ll cancel the inactivity check
        if inactivity_task is not None:
            inactivity_task.cancel()

    await session.start(agent=Agent(instructions="You are a helpful assistant."), room=ctx.room)
Here’s a quick rundown: • Custom Timeout: The
user_away_timeout
sets how long we wait before considering the user inactive (like 12.5 seconds). • Event Handling: When the user’s status changes to "away," the
user_state_changed
event kicks off the
user_presence_task
. • Message Sending: That task sends up to three polite check-in messages before closing the session if there’s no response. If you want me to explain anything further or tweak the code, just let me know—I’m happy to help!