creamy-window-79763
12/03/2025, 2:16 PMtall-airline-16756
12/03/2025, 2:16 PMtall-airline-16756
12/03/2025, 2:16 PMimport 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 docscreamy-window-79763
12/03/2025, 2:37 PMtall-airline-16756
12/03/2025, 2:37 PMpress_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', '#', '*').
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.creamy-window-79763
12/03/2025, 2:42 PMtall-airline-16756
12/03/2025, 2:43 PMbutton is an int (0–9). Call it from your agent after the SIP participant has joined.
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):
await press_button(ctx, 1)
This sends DTMF for the specified digit after an optional short delay.