i have an agent called upsell_agent, when i create...
# ask-ai
c
i have an agent called upsell_agent, when i create an outbound call, how to make that specific agent connect with the person
t
Thanks for reaching out, ZaidKaraymeh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
""" Hey there! To make sure your agent, "upsell_agent," connects smoothly with the person during an outbound call, just follow these simple steps: 1. Create an Outbound Trunk: First, set up your SIP provider's outbound trunk using the LiveKit CLI. You'll create a JSON file with your provider's details and register it with LiveKit. Here's an example command to get you started:
Copy code
lk sip outbound create outbound-trunk.json
2. Create the Agent: Next, use the LiveKit CLI to create the "upsell_agent" specifically for outbound calls. Here's how:
Copy code
lk app create --template=outbound-caller-python
Just make sure the agent is linked with the outbound trunk ID you set up. 3. Dispatch the Agent for Outbound Calls: Finally, assign the "upsell_agent" to the outbound call using this command (swap out
+15105550100
with the phone number you want to dial):
Copy code
lk dispatch create \
   --new-room \
   --agent-name upsell_agent \
   --metadata '+15105550100'
This will create a new room, connect "upsell_agent" to the call, and dial the number. Once the person answers, your agent will jump right in and handle the conversation. If you have any questions or need a hand with anything else, just let me know—I’m here to help! """ Sources: Making calls using SIP | LiveKit Docs | Making calls using SIP | LiveKit Docs | agent.py
c
So i create an outbound trunk, and i have an agent running called "upsell_agent", i have a process which initiates an outbound call like so, how will the agent join the room with the person? import asyncio from livekit import api from livekit.protocol.sip import CreateSIPParticipantRequest import logging logger = logging.getLogger('outbound') async def main(): livekit_api = api.LiveKitAPI() request = CreateSIPParticipantRequest( sip_trunk_id="ST_yxT8oGHqoaAL", sip_call_to="221223", # number you want to dial participant_identity="sip-test", participant_name="Test Caller", krisp_enabled=True, wait_until_answered=True, ) try: participant = await livekit_api.sip.create_sip_participant(request) print(f"Call initiated: {participant}") except Exception as e: print(f"Error creating SIP participant: {e}") print(f"error creating SIP participant: {e.message}, " f"SIP status: {e.metadata.get('sip_status_code')} " f"{e.metadata.get('sip_status')}") finally: await livekit_api.aclose() asyncio.run(main())
t
To make sure your "upsell_agent" joins the room with the person during an outbound call, here’s a simple step-by-step guide: 1. Create the SIP Participant: Start by using the
CreateSIPParticipant
API to kick off the outbound call. Just include the room name where you want the agent to join, the SIP trunk ID, and the phone number you’re calling. Here’s an example:
Copy code
request = CreateSIPParticipantRequest(
    room_name="room_name_here",
    sip_trunk_id="ST_yxT8oGHqoaAL",
    sip_call_to="221223",
    participant_identity="sip-test",
    participant_name="Test Caller",
    krisp_enabled=True,
    wait_until_answered=True,
)
2. Wait for the Participant to Join: After you create the SIP participant, wait for them to actually join the room:
Copy code
participant = await ctx.wait_for_participant(identity="sip-test")
3. Start the Agent Session: Once the participant is in, you can start the agent session with the room and participant. Like this:
Copy code
session_started = asyncio.create_task(
    session.start(
        agent=upsell_agent,
        room=ctx.room,
        room_input_options=RoomInputOptions(
            noise_cancellation=noise_cancellation.BVCTelephony(),
        ),
    )
)
4. Ensure Agent Interaction: The agent will automatically jump in and interact with the participant once the session starts. Just double-check that the agent is set up to handle the call flow, including any greetings or other interactions needed. If you have any questions or want me to walk you through any part of this, just let me know—I’m here to help! Sources: Agents telephony integration | LiveKit Docs | agent.py
c
note that i want to create new room for each outbound call, how to do it
t
""" To create a new room for each outbound call, just use the
--new-room
flag when you dispatch the call. Here’s a quick guide to help you out: 1. Use the Dispatch Command: Run this command to set up a new room for every outbound call:
Copy code
lk dispatch create --new-room --agent-name upsell_agent --metadata '+15105550100'
Just swap out
+15105550100
with the phone number you want to call. 2. Make Sure Each Room is Unique: Every time you run this command, a fresh room is created, so each call has its own private space. If you have any questions or need a hand with anything else, feel free to ask! """
c
how to do it progromatically with python, like make the call and dispatch both of them
t
To programmatically create a new room and dispatch an agent for an outbound call using Python, here’s a handy example to get you started:
Copy code
python import os import asyncio from livekit import api async def create_outbound_call(phone_number, agent_name="upsell_agent"): # Initialize LiveKit API lkapi = api.LiveKitAPI() try: # Create a unique room name room_name = f"outbound-call-{int(asyncio.get_event_loop().time())}" # Dispatch the agent to the new room dispatch = await lkapi.agent_dispatch.create_dispatch( api.CreateAgentDispatchRequest( agent_name=agent_name, room=room_name, metadata=phone_number # Add metadata if needed ) ) print(f"Created dispatch: {dispatch}") # Create SIP participant for the outbound call sip_participant = await lkapi.sip.create_sip_participant( api.CreateSIPParticipantRequest( room_name=room_name, sip_trunk_id="ST_yxT8oGHqoaAL", # Replace with your SIP trunk ID sip_call_to=phone_number, participant_identity="outbound-agent", ) ) print(f"Created SIP participant: {sip_participant}") except Exception as e: print(f"Oops, something went wrong: {e}") finally: await lkapi.aclose() # Example usage async def main(): await create_outbound_call("+1234567890") # Replace with the target phone number if __name__ == "__main__": asyncio.run(main())
### Key Steps: 1. Room Creation: We generate a unique room name for each call to keep things organized. 2. Agent Dispatch: The agent (like "upsell_agent") is sent over to the new room. 3. SIP Participant: This part sets up a SIP participant to kick off the outbound call. Just swap out
ST_yxT8oGHqoaAL
with your actual SIP trunk ID and
+1234567890
with the phone number you want to call. If you hit any bumps or have questions, feel free to ask—I’m here to help!
c
fix this eror im getting Error during outbound call: bad argument type for built-in operation import asyncio from livekit import api SIP_TRUNK_ID = "ST_yxT8oGHqoaAL" # replace with your SIP trunk ID AGENT_NAME = "upsell_agent" # your agent name async def create_outbound_call(phone_number: str): lkapi = api.LiveKitAPI() try: # 1. Create a unique room for this outbound call room_name = f"outbound-call-{int(asyncio.get_event_loop().time())}" # 2. Dispatch your agent into the new room dispatch = await lkapi.agent_dispatch.create_dispatch( api.CreateAgentDispatchRequest( agent_name=AGENT_NAME, room=room_name, metadata={"call_to": phone_number}, ) ) print(f"Agent dispatched: {dispatch}") # 3. Create a SIP participant that dials the outbound number into that room sip_participant = await lkapi.sip.create_sip_participant( api.CreateSIPParticipantRequest( room_name=room_name, sip_trunk_id=SIP_TRUNK_ID, sip_call_to=phone_number, participant_identity="callee", participant_name="Outbound Callee", # wait_until_answered=True, # krisp_enabled=True, ) ) print(f"SIP participant created: {sip_participant}") except Exception as e: print(f"Error during outbound call: {e}") finally: await lkapi.aclose() # Example usage async def main(): await create_outbound_call("36533336") # replace with target number if name == "__main__": asyncio.run(main())
t
The 'bad argument type for built-in operation' error usually pops up when there’s a mismatch in the types of arguments you’re passing to the
create_sip_participant
method. No worries though, here’s a simple way to fix it: 1. Double-check Argument Types: Make sure every argument you pass to
create_sip_participant
is exactly the type it expects. For instance: -
sip_trunk_id
should be a string that starts with "ST_". -
sip_call_to
should be a valid phone number in string format. 2. Verify Your SIP Trunk ID: Take a moment to confirm that your
SIP_TRUNK_ID
is formatted correctly and starts with "ST_". You can easily check this with the LiveKit CLI by running:
Copy code
lk sip outbound list
3. Update Your Code: Make sure your code follows the right pattern. Here’s an example:
Copy code
sip_participant = await lkapi.sip.create_sip_participant(
    api.CreateSIPParticipantRequest(
        room_name=room_name,
        sip_trunk_id=SIP_TRUNK_ID,  # This should be a valid string
        sip_call_to=phone_number,   # This should be a valid phone number string
        participant_identity="callee",
        participant_name="Outbound Callee",
    )
)
4. Catch Exceptions with Care: It’s helpful to add some detailed exception handling to catch any specific errors:
Copy code
except api.TwirpError as e:
    print(f"Error creating SIP participant: {e.message}, "
          f"SIP status: {e.metadata.get('sip_status_code')} "
          f"{e.metadata.get('sip_status')}")
If you’re still running into trouble or want me to walk through anything else, just let me know—I’m here to help!