How can I end a call in on_user_input_transcribed ...
# ask-ai
n
How can I end a call in on_user_input_transcribed event ?
t
Thanks for reaching out, Niv. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To gracefully end a call during the
on_user_input_transcribed
event, you can detect the user’s intent from the transcribed input and then trigger a function tool to delete the room. This way, the call will end smoothly for everyone involved. Here’s a simple way to set this up: 1. Create a
@function_tool
that calls the
delete_room
API. 2. If you'd like, have the agent say a closing message and wait for it to finish playing using
ctx.wait_for_playout()
before deleting the room. 3. Register the
on_user_input_transcribed
event handler to listen for the transcribed user input. 4. Inside the handler, trigger the function tool whenever you detect user intent like "goodbye" or "hang up". Here’s an example to guide you:
Copy code
from livekit import api
from livekit.agents import get_job_context, JobContext
from livekit.agents.voice import RunContext, AgentContext
from livekit.agents import function_tool, UserInputTranscribedEvent

@function_tool
async def end_call(self, ctx: RunContext):
    await ctx.wait_for_playout()
    hangup_ctx = get_job_context()
    if hangup_ctx is None:
        return
    await hangup_ctx.api.room.delete_room(
        api.DeleteRoomRequest(room=hangup_ctx.room.name)
    )

def setup_call_ending(agent_session: AgentSession, job_ctx: JobContext):
    @job_ctx.room.on("user_input_transcribed")
    def on_user_input_transcribed(event: UserInputTranscribedEvent):
        transcript = event.transcript.lower()
        if any(phrase in transcript for phrase in ["goodbye", "hang up", "end call", "bye"]):
            asyncio.create_task(
                agent_session.execute_function("end_call")
            )
This method helps ensure everyone’s call ends cleanly by using the
delete_room
API. If you have any questions or want to explore this further, I’m here to help! Sources: Agents telephony integration | LiveKit Docs | Events and error handling | LiveKit Docs | Events and error handling | LiveKit Docs | Agents telephony integration | LiveKit Docs