My agent needs to press button 1 to accept an inbo...
# ask-ai
c
My agent needs to press button 1 to accept an inbound call. How can I do that? Provide sample python code.
t
Thanks for reaching out, Yousri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to have your agent press button 1 to accept an inbound call. Below is a concise Python example using the LiveKit agents framework that sends DTMF digit "1" after a SIP participant joins.
Copy code
import asyncio
from livekit import api
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.voice import VoicePipelineAgent
from livekit.plugins import openai, silero

async def entrypoint(ctx: JobContext):
    # Connect and subscribe to audio only
    await ctx.connect(auto_subscribe=api.AutoSubscribe.AUDIO_ONLY)

    # Create agent (STT/TTS/LLM plugins optional)
    agent = VoicePipelineAgent(
        vad=silero.VAD.load(),
        stt=openai.STT(),
        llm=openai.LLM(),
        tts=openai.TTS(),
        ivr_detection=True
    )

    await agent.start(ctx.room)

    # Wait for the inbound SIP caller to join
    participant = await ctx.wait_for_participant()

    # Optional: ensure this is a SIP caller
    if "sip_" not in participant.identity:
        return

    # Small delay to ensure the call is fully established
    await asyncio.sleep(1.0)

    # Send DTMF tone '1' (press button 1)
    await ctx.room.local_participant.publish_dtmf(code=1, digit='1')

    print("DTMF '1' sent to accept the call.")

    # Optionally speak to the caller
    await agent.say("Hello, how can I help you today?", participant=participant)

    await ctx.job_finished.wait()

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Notes: • Use
publish_dtmf(code=1, digit='1')
to send the DTMF for button 1. • Wait briefly after participant join to ensure the SIP call is connected. • Ensure your SIP trunk/provider supports DTMF and your LiveKit inbound trunk is configured. Would you like a shorter snippet that only shows the DTMF send (no agent boilerplate), or help adapting this to your existing agent code? Sources: Handling DTMF | LiveKit docs
c
I'd like to handle this in a function call. Implement it. The function should have the button to be pressed as argument
t
Below is a concise Python implementation that wraps the DTMF send in a function
press_button
. It assumes you're inside an agent
entrypoint
with access to
ctx
(JobContext) and a connected room. The function accepts a single
button
argument (string, e.g., '1', '0', '#', '*').
Copy code
import asyncio
from livekit import api
from livekit.agents import JobContext, WorkerOptions, cli
from livekit.agents.voice import VoicePipelineAgent
from livekit.plugins import openai, silero

async def press_button(ctx: JobContext, button: str, delay: float = 1.0) -> None:
    """
    Send DTMF for `button` from the local participant in ctx.room.
    `button` should be a single character: '0'-'9', '*', or '#'.
    `delay` is seconds to wait before sending (default 1.0).
    """
    # Validate button
    if not isinstance(button, str) or len(button) != 1 or button not in "0123456789*#":
        raise ValueError("button must be one of '0'-'9', '*' or '#'")

    # Ensure room and local participant exist
    room = ctx.room
    if room is None or room.local_participant is None:
        raise RuntimeError("Not connected to a room or local participant missing")

    # Optional delay to ensure SIP call is fully connected
    await asyncio.sleep(delay)

    # DTMF code: use int for digits, common providers accept code for 0-9; for '*' and '#' use their ASCII or map if needed.
    # Here we pass digit as string which LiveKit accepts; code for digits use int(value), for '*' and '#' use 10/11 as placeholders if required.
    if button.isdigit():
        code = int(button)
    else:
        # Use conventional mappings if required by your SIP provider: '*' -> 10, '#' -> 11
        code = 10 if button == '*' else 11

    await room.local_participant.publish_dtmf(code=code, digit=button)

async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=api.AutoSubscribe.AUDIO_ONLY)

    agent = VoicePipelineAgent(
        vad=silero.VAD.load(),
        stt=openai.STT(),
        llm=openai.LLM(),
        tts=openai.TTS(),
        ivr_detection=True
    )
    await agent.start(ctx.room)

    # Wait for inbound participant
    participant = await ctx.wait_for_participant()

    # Ensure SIP participant (optional)
    if "sip_" not in participant.identity:
        return

    # Send button '1' using the function
    await press_button(ctx, '1')

    await agent.say("Call accepted. How can I help?", participant=participant)

    await ctx.job_finished.wait()

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
If you'd like, I can: • Return a minimal standalone snippet that only defines
press_button
and shows usage, or • Adjust the
code
mapping for
*
/
#
to match your SIP provider's expected codes—tell me which provider you use.
c
Simplify the code and description. THe input is always an int
t
Below is a minimal async function that sends a DTMF digit.
button
is an int (0–9). Call it from your agent after the SIP participant has joined.
Copy code
import asyncio
from livekit.agents import JobContext

async def press_button(ctx: JobContext, button: int, delay: float = 1.0) -> None:
    if not (0 <= button <= 9):
        raise ValueError("button must be an int 0-9")
    room = ctx.room
    if room is None or room.local_participant is None:
        raise RuntimeError("not connected to a room")
    await asyncio.sleep(delay)
    await room.local_participant.publish_dtmf(code=button, digit=str(button))
Usage (inside your entrypoint after participant joins):
Copy code
await press_button(ctx, 1)
This sends DTMF for the specified digit after an optional short delay.