how to use hang up call, wait maximum 15 s then au...
# ask-ai
c
how to use hang up call, wait maximum 15 s then auto hang up
t
Thanks for reaching out, Hai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Just to make sure I understand correctly, are you hoping to implement this feature using LiveKit's SDKs or their APIs? Also, when you mention ending a call automatically, do you mean for everyone in the room or just for a particular participant? I'm here to help you through this!
c
everyone in the call, use livekit sdk
t
Hi there! To help you automatically hang up a call for all participants after 15 seconds using the LiveKit SDK, you can make use of the
delete_room
API. This handy API will end the room and disconnect everyone at once, making it super convenient. Here’s a Python example to guide you through it:
Copy code
import asyncio
from livekit import api
from livekit.agents import get_job_context, JobContext

async def hangup_after_15_seconds():
    # Wait for 15 seconds
    await asyncio.sleep(15)

    # Get the current job context
    ctx = get_job_context()
    if ctx is None:
        print("No job context available.")
        return

    # Initialize the LiveKit API client
    api_client = api.LiveKitAPI()

    try:
        # Delete the room to hang up for all participants
        await api_client.room.delete_room(
            api.DeleteRoomRequest(
                room=ctx.room.name,
            )
        )
        print(f"Room {ctx.room.name} has been deleted.")
    except Exception as e:
        print(f"Failed to delete room: {e}")
    finally:
        api_client.close()  # Clean up the API client

async def entrypoint(ctx: JobContext):
    # Connect the agent to the room
    await ctx.connect(auto_subscribe=api.AutoSubscribe.AUDIO_ONLY)

    # Start the 15-second countdown to hang up
    asyncio.create_task(hangup_after_15_seconds())

    print("Call in progress. Will end in 15 seconds for all participants.")
Just a quick reminder to make sure your environment has these credentials set up: • `LIVEKIT_URL`: Your LiveKit server WebSocket URL • `LIVEKIT_API_KEY`: API key with room management permissions • `LIVEKIT_API_SECRET`: Corresponding API secret If you'd like, I’d be more than happy to help you adapt this example for another programming language or a different use case. Just let me know! Sources: Inside a session | LiveKit Docs | Inside a session | LiveKit Docs | SIP Integration | livekit/livekit_composite | DeepWiki | Connecting to LiveKit | LiveKit Docs | Remote method calls (RPC) | LiveKit Docs
c
i want to use api.LiveKitAPI()
t
I'm glad you’re exploring the Python example! It already uses
api.LiveKitAPI()
to help you implement the auto hang-up feature smoothly. Here’s a quick rundown of the steps: 1. Wait for 15 seconds using
asyncio.sleep(15)
. 2. Initialize the LiveKit API client with
api.LiveKitAPI()
. 3. Call the
delete_room
method using the room name to disconnect all participants. Just a friendly reminder to double-check that your environment variables (
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
) are set up correctly so the API client works seamlessly. If you’d like, I’m here to help with any part of the setup or to clarify anything further—just let me know!
c
i mean i dont want to use api.livekitapi
t
I’m happy to help you with implementing auto hang-up for all participants in LiveKit without needing to use
api.LiveKitAPI
. You can achieve this by using the
ctx.api.room.delete_room()
method available through the agent's job context. This is a convenient way to end the room session and disconnect everyone without having to set up an API client manually. Here’s a simple example for you to try:
Copy code
from livekit import api
from livekit.agents import get_job_context

async def auto_hangup_all():
    # Get current job context
    ctx = get_job_context()
    if ctx is None:
        return  # Not running inside an agent job

    # Delete the room to disconnect all participants
    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )
This approach makes sure everyone is disconnected right away, using the built-in API client that’s already part of the agent runtime. So, you won’t need any extra credentials or setup. If you’d like, I’m here to help you integrate this smoothly into your workflow or answer any questions you might have! Sources: Connecting to LiveKit | LiveKit Docs | examples/warm-transfer/warm_transfer.py | Agents telephony integration | LiveKit Docs | Agents telephony integration | LiveKit Docs | Job lifecycle | LiveKit Docs
c
wait for 15s when client not replying?
t
I'd be happy to help you set up a 15-second timeout for client inactivity before auto hang-up in LiveKit. You can achieve this by using the
user_away_timeout
parameter in the
AgentSession
class. This parameter helps detect when a user has been inactive for 15 seconds and triggers a
user_state_changed
event. From there, you can handle this event to disconnect the room smoothly. Here’s a simple example to guide you:
Copy code
from livekit.agents import AgentSession, JobContext

async def entrypoint(ctx: JobContext):
    session = AgentSession(user_away_timeout=15.0)

    @session.on("user_state_changed")
    def _user_state_changed(event):
        if event.new_state == "away":
            # Auto hang-up after inactivity
            ctx.room.disconnect()

    await session.start()
This setup ensures the room disconnects after 15 seconds of inactivity. If you'd like, you can also notify the user before disconnecting by sending a friendly message or prompt. Would you like me to help you with adding user notifications or any other customization? I'm here to support you! Sources: agent_extensions/README.md | Connecting to LiveKit | LiveKit Docs | Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Agent Testing & Evaluation | livekit/livekit_composite | DeepWiki