how to get the call start time, end time and call ...
# ask-ai
p
how to get the call start time, end time and call duration from the telephony call
t
Thanks for reaching out, Avinash. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
p
Also give me an example for this
t
If you're looking to find out when a telephony call started, ended, and how long it lasted in LiveKit, the
SIPCallInfo
object is your go-to. Here’s what it includes: • started_at_ns: The exact time (in nanoseconds) when the SIP call kicked off. • ended_at_ns: The exact time (in nanoseconds) when the SIP call wrapped up. To figure out the call duration, just subtract
started_at_ns
from
ended_at_ns
like this:
Copy code
duration_ns = sip_call_info.ended_at_ns - sip_call_info.started_at_ns
This gives you the duration in nanoseconds, which you can easily convert to seconds or milliseconds depending on what you need. You can get this info through a couple of ways: • Webhook events: Keep an eye on SIP call-related webhooks such as
SIP_CALL_STARTED
and
SIP_CALL_ENDED
. They’ll include the
SIPCallInfo
object right in the payload. • Server-side SDKs: Use LiveKit’s server SDKs (like Python, Go, or Rust) to fetch or get updates about the call status. If you want me to walk you through an example in a specific SDK or have any other questions, just give me a shout—I’m here to help! Sources: Sources/LiveKit/Protos/livekit_models.pb.swift | Agents telephony integration | LiveKit Docs | livekit/livekit_sip.pb.go | livekit-protocol/livekit/protocol/sip.py | livekit-protocol/src/livekit.rs
p
Yeah python example would help
t
Here’s a friendly example in Python to help you grab the call start time, end time, and figure out the call duration using the
SIPCallInfo
object:
Copy code
# Assuming you have the SIPCallInfo object from a webhook or SDK
sip_call_info = get_sip_call_info()  # Just swap this out with how you actually get your SIPCallInfo

# Pull out start and end times in nanoseconds
started_at_ns = sip_call_info.started_at_ns
ended_at_ns = sip_call_info.ended_at_ns

# Work out the call duration in nanoseconds
duration_ns = ended_at_ns - started_at_ns

# Turn that duration into seconds
duration_seconds = duration_ns / 1_000_000_000

print(f"Call started at: {started_at_ns} ns")
print(f"Call ended at: {ended_at_ns} ns")
print(f"Call duration: {duration_seconds} seconds")
Just replace
get_sip_call_info()
with whatever method you use to fetch your
SIPCallInfo
object, whether that’s from a webhook payload or an SDK call. If you want me to walk you through anything else, just let me know—I’m here to help!
p
sip_call_info = get_sip_call_info() # Just swap I want to know this how to get this sip call info object
t
To get your hands on the
SIPCallInfo
object, you’ve got a couple of easy options: • Webhook Events: You can subscribe to SIP call-related webhooks like
SIP_CALL_STARTED
and
SIP_CALL_ENDED
. These webhooks come with the
SIPCallInfo
object right in their payload, so you can simply parse it to grab the info you need. • Server-Side SDKs: Another way is to use LiveKit’s server SDKs (like the Python SDK) to listen for events or fetch call details. The SDK has handy methods to pull SIP call metadata, including the
SIPCallInfo
object. If you’re going the webhook route, just make sure your webhook endpoint is set up correctly to receive events from LiveKit. And hey, if you want a hand setting up webhooks or working with the SDK, just give me a shout—I’m here to help!
p
Ok I want to subscribe to SIP_CALL_ENDED event give me an example for this
t
Hey there! To subscribe to the
SIP_CALL_ENDED
event, you’ll want to set up a webhook endpoint where LiveKit can send those event notifications. Here’s a simple example using Python and Flask to help you get started:
Copy code
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def handle_webhook():
    # Grab the incoming JSON payload
    event = request.json

    # Check if the event is SIP_CALL_ENDED
    if event.get('event') == 'SIP_CALL_ENDED':
        sip_call_info = event.get('sip_call')
        if sip_call_info:
            started_at_ns = sip_call_info.get('started_at_ns')
            ended_at_ns = sip_call_info.get('ended_at_ns')

            # Calculate how long the call lasted
            duration_ns = ended_at_ns - started_at_ns
            duration_seconds = duration_ns / 1_000_000_000

            print(f"Call started at: {started_at_ns} ns")
            print(f"Call ended at: {ended_at_ns} ns")
            print(f"Call duration: {duration_seconds} seconds")

    return jsonify({"status": "received"})

if __name__ == '__main__':
    app.run(port=5000)
Here’s how to get this up and running: 1. Deploy this Flask app to a server that LiveKit can reach (think AWS, Heroku, or your own setup). 2. Point your LiveKit project to send webhooks to your endpoint (like
<YOUR_WEBHOOK_URL>
). 3. Make sure your server can handle HTTPS requests if that’s needed. If you hit any bumps or want a hand with deployment or setting up the webhook, just give me a shout—I’m here to help!
f
How to subscribe to SIP_CALL_ENDED event
t
To help you subscribe to the
SIP_CALL_ENDED
event, you’ll want to set up webhooks in your LiveKit project. Don’t worry, I’ll guide you through the steps: 1. Set up a Webhook Endpoint: First, create a server endpoint (using Flask, Express, or any framework you prefer) that can receive webhook events. Just make sure this endpoint is publicly accessible. 2. Configure Webhooks in LiveKit: Next, head over to your LiveKit project settings and add your webhook endpoint URL (like
<YOUR_WEBHOOK_URL>
) in the webhook configuration section. 3. Handle the Event: Finally, in your webhook handler, watch for the
SIP_CALL_ENDED
event in the incoming data. Here’s a quick example:
Copy code
# Example payload handling in Python
if event.get('event') == 'SIP_CALL_ENDED':
    sip_call_info = event.get('sip_call')
    # Process the SIPCallInfo object as needed
Once you’ve got this set up, LiveKit will notify your endpoint whenever a SIP call ends. If you have any questions or need a hand with any step, feel free to reach out—I’m here to help!