How can I detect DTFM in a conversation?
# ask-ai
p
How can I detect DTFM in a conversation?
t
Thanks for reaching out, Mario. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Detecting DTMF (Dual-Tone Multi-Frequency) tones in a conversation using LiveKit is quite straightforward, and I’m here to help you through it! You can use the
sip_dtmf_received
event, which gets triggered whenever a SIP participant sends DTMF tones, like when they press keys on their phone keypad. Here’s a simple way to get you started: 1. Set up an event listener on the
rtc.Room
object for the
sip_dtmf_received
event. This event will give you useful info about the digit pressed, who sent it, and the numeric DTMF code. 2. Make sure your agent connects to the LiveKit room with audio enabled. Using
AutoSubscribe.AUDIO_ONLY
helps automatically receive audio from participants. 3. Then, you can define your own logic to handle the detected DTMF tones—maybe routing calls or triggering actions based on what input you get. Here’s a quick example in Python to illustrate:
Copy code
@room.on("sip_dtmf_received")
def handle_dtmf(dtmf_event):
    digit = dtmf_event.digit
    identity = dtmf_event.participant.identity
    print(f"DTMF received: {digit} from {identity}")
If you’d like, I’m happy to walk you through any part of this or help with more specific implementation details. Just let me know! Sources: ivr-nagivator/agent.py | livekit-rtc/livekit/rtc/room.py | Company directory phone assistant | LiveKit Docs | agent.py
p
is there a way to grab the room from an event callback or from the agent session?
t
I'm happy to help you access the room from an event callback or agent session in LiveKit! Here are a few ways you can do this: • From the Job Context: The
JobContext
gives you a handy
room
property for direct access. For example, in Python:
Copy code
from livekit.agents import JobContext

async def entrypoint(ctx: JobContext):
    room = ctx.room
    await session.start(agent=agent, room=ctx.room)
• From Event Callbacks: You can also grab the current job context inside event callbacks to get the room. Here's how:
Copy code
@ctx.room.local_participant.register_rpc_method("start_turn")
async def start_turn(data):
    job_ctx = get_job_context()
    room = job_ctx.room
• From Agent Sessions: When you start an agent session, you usually pass the room as a parameter like this:
Copy code
await session.start(agent=MyAgent(), room=ctx.room)
If any part of this feels unclear or if you'd like more examples, please don’t hesitate to ask—I’m here to help! Sources: examples/voice_agents/push_to_talk.py | agents/src/job.ts | events/basic_event.py