response = await livekit_api.sip.create_sip_partic...
# ask-ai
m
response = await livekit_api.sip.create_sip_participant( api.CreateSIPParticipantRequest( sip_trunk_id=sip_trunk_id, sip_call_to=phone_number, room_name=room_name, participant_identity=identity, participant_name="Human Agent", krisp_enabled=True ) ) how to check my sip_participant is connected or not
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can check whether your SIP participant is connected in LiveKit by monitoring the sip.callStatus participant attribute. This attribute reflects the current SIP connection state: • `active`: The SIP participant is connected and call is established. • `dialing`: Outbound call is dialing and awaiting pickup. • `ringing`: Inbound call is ringing for the caller. • `hangup`: Call has ended. After calling
create_sip_participant
, you should listen for participant events and attribute changes. The status changes to
active
when the SIP call is connected. Here’s a Python example (similar approaches are available for other SDKs):
Copy code
def on_participant_attributes_changed(changed_attributes, participant):
    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        if 'sip.callStatus' in changed_attributes:
            if changed_attributes['sip.callStatus'] == 'active':
                print('SIP participant is now connected')
Listen to the
participant_attributes_changed
event and check for
sip.callStatus == 'active'
to confirm the SIP connection. Additional SIP-specific attributes such as
sip.callID
and
sip.phoneNumber
are also available on the participant object for detailed monitoring. For more complete code samples and details on SIP participant monitoring, refer to the LiveKit participant event and attribute documentation. Sources: Making outbound calls | LiveKit Docs | SIP participant | LiveKit Docs | src/room/Room.ts | Core API primitives | LiveKit Docs | Making calls using SIP | LiveKit Docs
m
Can you write a code with my code to do some function
t
Hey there! I'd love to help you figure out the next steps after your SIP participant connects. What's the main thing you're hoping to do? Maybe send a quick message, update some records, start recording, or something totally unique? Let me know what you're thinking, and we'll work through it together!
m
await livekit_api.room.remove_participant( api.RoomParticipantIdentity(room=self.job_context.room.name, identity=local_participant) ) I want to execute this when sip participant connected
t
Hey there! Let me walk you through a neat way to handle removing a participant when a SIP connection is successful. Think of it like a quick handshake between your code and the room connection.
Copy code
def on_participant_attributes_changed(changed_attributes, participant):
    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        if 'sip.callStatus' in changed_attributes:
            if changed_attributes['sip.callStatus'] == 'active':
                # SIP participant has connected
                # Remove the local participant as requested
                await livekit_api.room.remove_participant(
                    api.RoomParticipantIdentity(
                        room=your_room_name,  # Use your actual room name here
                        identity=local_participant  # Replace with the identity you want to remove
                    )
                )
Quick heads up: Depending on how your event framework works, you might need to get a bit creative with async functions. No worries, though – you can easily schedule the coroutine if needed!
Copy code
def on_participant_attributes_changed(changed_attributes, participant):
    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        if 'sip.callStatus' in changed_attributes:
            if changed_attributes['sip.callStatus'] == 'active':
                asyncio.create_task(livekit_api.room.remove_participant(
                    api.RoomParticipantIdentity(
                        room=your_room_name,
                        identity=local_participant
                    )
                ))
And just like that, you'll smoothly remove the specified local participant the moment the SIP participant connects. Easy peasy!
m
how to get changed attributes
t
Hey there! Let's talk about handling participant attribute changes in the Python SDK. It's pretty straightforward and super helpful. When something changes with a participant's attributes, you'll get a neat event handler that makes tracking those updates a breeze.
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes: dict[str, str], participant: rtc.Participant):
    print("Changed attributes:", changed_attributes)
    print("Current participant attributes:", participant.attributes)
Quick Tips:
changed_attributes
is like a snapshot of just what's different - only the attributes that recently changed. • Want the full picture?
participant.attributes
gives you all the current details about that participant. • Pro tip: If an attribute gets deleted, it'll show up as an empty string in
changed_attributes
. This handy event handler is your friend for keeping track of who's doing what and making sure you've got all the info you need. Pretty cool, right? Sources: Participant attributes and metadata | LiveKit Docs | livekit-rtc/livekit/rtc/room.py | callback.go | livekit-android-sdk/src/main/java/io/livekit/android/events/ParticipantEven... | Job lifecycle | LiveKit Docs
m
where to import room attribute
t
Hey there! Let me walk you through how to work with the Room object in the LiveKit Python SDK. It's super easy to get started!
Copy code
from livekit import rtc

room = rtc.Room()
Now comes the fun part - you can set up event listeners to catch all the cool things happening in your room. For example, tracking participant attribute changes is a breeze:
Copy code
@room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attributes: dict[str, str], participant: rtc.Participant):
    print("Changed attributes:", changed_attributes)
If you're working with LiveKit Agents, it gets even simpler - the
room
object is right there waiting for you as
ctx.room
in your JobContext:
Copy code
async def entrypoint(ctx):
    room = ctx.room
With this setup, you'll be able to manage all your room and participant events like a pro. Trust me, it's going to make your development process so much smoother! Sources: livekit-protocol/livekit/protocol/room.py | Inside a session | LiveKit Docs