how to take input form user key bord during phone ...
# ask-ai
l
how to take input form user key bord during phone call with agent
t
Thanks for reaching out, Pavan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you take input from a user's keyboard during a phone call with an agent using DTMF (Dual-Tone Multi-Frequency) signals. LiveKit makes this process quite smooth by supporting both receiving and sending DTMF tones during SIP-based phone calls. Here's a simple way to get started: • Receiving DTMF Input: Whenever a user presses a key, LiveKit will emit a
sip_dtmf_received
event. You can listen for this event to capture the input and respond accordingly—for instance, pressing '1' might transfer the call to Billing, while '2' could route it to Technical Support. • Sending DTMF from the Agent: If an agent needs to send DTMF tones, they can use the
publish_dtmf
method. This is especially handy when navigating external IVR systems on behalf of the user. To get everything set up, here’s what you’ll want to do: 1. Configure a SIP trunk with a provider such as Twilio and connect it to your LiveKit instance. 2. Set up your environment variables like API keys and phone numbers in your configuration. 3. Use event handlers to process the DTMF input and take the right actions based on user input. If you'd like to dive into more detailed examples and step-by-step configuration, the LiveKit DTMF documentation is a fantastic resource to guide you through. Sources: Company directory phone assistant | LiveKit Docs | Handling DTMF | LiveKit Docs | sip-refer/README.mdoc | README.md
l
provide code in python
t
I'd love to walk you through an example of handling DTMF input in Python using LiveKit. Let's dive in together! Receiving DTMF Input
Copy code
from livekit import rtc
import asyncio

@room.on("sip_dtmf_received")
def handle_dtmf(dtmf_event: rtc.SipDTMF):
    digit = dtmf_event.digit
    print(f"DTMF received - Digit: '{digit}'")

    # Handle input based on pressed digit
    if digit == "1":
        asyncio.create_task(say("Transferring you to Billing."))
    elif digit == "2":
        asyncio.create_task(say("Transferring you to Technical Support."))
    else:
        asyncio.create_task(say("Invalid option, please try again."))
Sending DTMF Tones
Copy code
import time

last_dtmf_press = 0

async def send_dtmf_code(code: int):
    global last_dtmf_press
    current_time = time.time()
    if current_time - last_dtmf_press < 3:  # 3-second cooldown
        print("DTMF code rejected due to cooldown")
        return
    last_dtmf_press = current_time

    await ctx.room.local_participant.publish_dtmf(
        code=code,
        digit=str(code)
    )
Full Example
Copy code
def start_agent(self, participant: rtc.Participant):
    self.model = openai.realtime.RealtimeModel(
        instructions=(
            "You are a friendly assistant. "
            "Please inform users they can:\n"
            "- Press 1 for Billing\n"
            "- Press 2 for Technical Support\n"
            "- Press 3 for Customer Service"
        ),
        modalities=["audio", "text"],
        voice="sage"
    )
    self.assistant = MultimodalAgent(model=self.model)
    self.assistant.start(self.context.room, participant)
    greeting = "Hi, thanks for calling. Press 1 for Billing, 2 for Support, or 3 for Customer Service."
    asyncio.create_task(self.say(greeting))
Just a quick reminder to ensure your SIP trunk and environment variables are set up properly. If you run into any questions or need a hand with anything else, please don't hesitate to reach out—I'm here to help!