how to hangup a call? i have integrated our teleph...
# ask-ai
p
how to hangup a call? i have integrated our telephony system already.
t
Thanks for reaching out, Bryner. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To hang up a call in LiveKit, you can use one of the following methods: 1. End the call for all participants: Use the
hangup_call()
function to delete the entire room, which will end the call for everyone. Here is an example:
Copy code
async def hangup_call():
    ctx = get_job_context()
    if ctx is None:
        # Not running in a job context
        return

    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )
1. Remove a specific participant: Use the
hangup()
method to remove a specific participant from the room. Here is an example:
Copy code
async def hangup(self):
    try:
        await self.api.room.remove_participant(api.RoomParticipantIdentity(
            room=self.room.name,
            identity=self.participant.identity,
        ))
    except Exception as e:
        # it's possible that the user has already hung up, this error can be ignored
        <http://logger.info|logger.info>(f"received error while ending call: {e}")
The first method ends the call for everyone, while the second method allows you to remove a specific participant. Let me know if you need further assistance! Sources: Agents telephony integration | LiveKit Docs | Making calls using SIP | LiveKit Docs
p
how can i call the hangup_call function if my agent says goodbye or anything that ends the call?
t
Hey there! Want to smoothly end a call with your agent? I've got a friendly solution that makes sure the agent can say a proper goodbye before hanging up. Let me walk you through a simple way to do this.
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """Called when the user wants to end the call"""
    # let the agent finish speaking
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    await hangup_call()
And here's how you can handle the actual call hangup:
Copy code
async def hangup_call():
    ctx = get_job_context()
    if ctx is None:
        # Not running in a job context
        return

    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )
This approach makes sure your agent gets to say goodbye properly before the call ends. Pretty neat, right? If you need any more help or have questions, I'm totally here for you! Sources: Agents telephony integration | LiveKit Docs
p
how will i trigger this or will the llm decide to trigger the hangup?
t
Hey there! So, you're wondering about the different ways to wrap up a call with the
hangup_call
function? I've got you covered! Let me break it down in a friendly way: 1. Manual Trigger: Think of this like having a big "end call" button. You can directly call the
hangup_call
function from your agent. Here's a quick example that might help:
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """Called when the user wants to end the call"""
    # let the agent finish speaking
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    await hangup_call()
1. Automated Trigger: Imagine your system being smart enough to know when it's time to say goodbye! This could mean hanging up after detecting a farewell message or meeting a specific condition. 2. API Integration: If you're more of a direct approach person, you can use the API to remove participants or delete the room - just like we discussed earlier. Need more help figuring this out? I'm right here to help you navigate through it! Let me know what questions you have. Sources: Agents telephony integration | LiveKit Docs | Making calls using SIP | LiveKit Docs