how do i hangup a sip call? i am using telnyx for ...
# ask-ai
k
how do i hangup a sip call? i am using telnyx for inbound calling with livekit
t
Thanks for reaching out, Saurav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To hang up a SIP call using Telnyx for inbound calling with LiveKit, you have the following options: • Automatic SIP BYE Handling: When the caller hangs up or Telnyx sends a SIP BYE request, LiveKit automatically processes the termination, disconnects the call, and updates the call status. • Programmatic Call Termination: You can terminate calls programmatically using LiveKit's participant management APIs. For example, you can remove the SIP participant from the room to end the call. Additionally, LiveKit provides internal methods like
Close()
and
closeWithHangup()
for call termination. If you need further assistance with implementing these methods, let me know! Sources: SIP overview | LiveKit Docs | Create and configure Telnyx SIP trunk | LiveKit Docs | Accepting incoming calls | LiveKit Docs | pkg/sip/inbound.go | Agents telephony integration | LiveKit Docs
k
tell me more about closeWithHangup method
t
The
closeWithHangup
method in LiveKit is all about ending inbound SIP calls when you decide to hang up. Here’s a quick rundown of what it does: • State Update: It updates the call state by setting the
DisconnectReason
to
CLIENT_INITIATED
, so everyone knows the client ended the call. • Call Termination: It calls the internal
close
method to wrap things up nicely—this includes sending SIP BYE messages, freeing up media resources, and removing the participant from the room. • Resource Cleanup: It makes sure all resources like media ports and room data are tidied up properly. • Usage: You’d typically use this method when hanging up during an active call or closing the LiveKit room yourself. If you want me to walk you through it or help with implementing this method, just let me know—I’m here to help! Sources: pkg/sip/inbound.go
k
but i am using livekit python sdk and there is no such method as closeWithHangup in this package
t
Hey there! Just a quick heads-up: in the LiveKit Python SDK, the
closeWithHangup
method isn’t available. No worries though! You can handle SIP call hangups using these approaches: • Using the
end_call
Method:
This one removes the participant from the room and ends the call. Here’s a little example:
Copy code
from livekit import api

class CallActions:
    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:
            <http://logger.info|logger.info>(f"Error while ending call: {e}")

    async def end_call(self):
        <http://logger.info|logger.info>(f"Ending the call for {self.participant.identity}")
        await self.hangup()
Deleting the Room: If you want to end the call for everyone in the room, you can delete the room like this:
Copy code
from livekit import api

class CallActions:
    async def end_call(self):
        <http://logger.info|logger.info>(f"Ending the call for {self.participant.identity}")
        await context.session.generate_reply(
            instructions="Thank you for your time. I'll be ending this call now. Goodbye!"
        )
        job_ctx = get_job_context()
        await job_ctx.api.room.delete_room(
            api.DeleteRoomRequest(room=job_ctx.room.name)
        )
If you’d like a hand implementing these or have any other questions, just let me know—I’m here to help! Sources: Making calls using SIP | LiveKit Docs | Agents telephony integration | LiveKit Docs | telephony/sip_lifecycle.py
m
I have tested that. Does not work. It even doesn't work, when I kill a room with the livekit cli => Killing room doesn't terminate the sip call. No BY is sent to caller.
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
k
@miniature-yacht-61157 this code worked for me
Copy code
async def _end_call(self, reason: str = "", msg: str = ""):
        """
        End the current call.
        """
        if msg:
            await self.session.say(text=msg, allow_interruptions=False)
            time_to_wait = get_time_to_wait_for_playout(msg)
            await asyncio.sleep(time_to_wait)
        if reason:
            <http://logger.info|logger.info>(f"Ending call for reason: {reason}")
        job_ctx = agents.get_job_context()
        await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
        return "Call has been ended."