When the caller is calling from a local phone or a...
# ask-ai
t
When the caller is calling from a local phone or a hidden phone number, I'd like to collect the caller's mobile phone number before starting the agent session. I want to: 1. Play a mp3 audio file (or use TTS) to ask the caller to input their phone number 2. The caller must then input their phone number and end the dtmf collection by typing
#
3. Once this is done, I want the agent session to start
t
Thanks for reaching out, Pierrick. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you help me understand which system or integration you're using to manage calls and agent sessions? For example, are you working with LiveKit's APIs, a particular telephony provider, or maybe another platform? Knowing this will make it easier to figure out the best way to set up the DTMF-based phone number collection.
t
I'm using a Twilio SIP trunk
t
To collect a caller's phone number using DTMF with a Twilio SIP trunk before starting an agent session in LiveKit, here’s a friendly step-by-step to guide you: 1. Set Up Twilio SIP Trunk: First, make sure your Twilio account is ready by creating a SIP trunk and linking it to your Twilio phone number. This gets everything talking smoothly with LiveKit. 2. Configure LiveKit Inbound Trunk: Next, set up an inbound trunk in LiveKit to accept calls from Twilio. Don’t forget to add the right authentication credentials so it all stays secure. 3. Extract Caller’s Phone Number: When a call comes in, LiveKit will provide the caller’s phone number right in the SIP participant’s attributes—you can grab it using
participant.attributes['sip.phoneNumber']
. 4. Handle DTMF Inputs: Then, add an event listener for DTMF signals to catch the caller’s input. This is super handy for collecting extra info or guiding them through an IVR before you start the agent session. If you want a hand with the implementation or some code examples, just give me a shout—I’m here to help! Sources: ivr-nagivator/agent.py | agent.py | src/setup-livekit.ts | SIP participant | LiveKit Docs
t
Yep, this I know. What I wanna know now is when I have collected
participant.attributes['sip.phoneNumber']
but this number is hidden or local (ie not mobile), I want to ask the user to input their mobile phone number via DTMF and finish their input by typing
#
. Then I will parse the phone number and correct the input if necessary
t
To help the user enter their mobile phone number using DTMF and make sure it's valid, here’s a simple way to do it: 1. Play a Prompt: Use a friendly TTS message or a pre-recorded audio to ask the user to enter their mobile phone number followed by
#
. 2. Capture DTMF Input: Listen for the DTMF tones and gather the digits until you detect the
#
. 3. Validate the Input: Check the input against a regular expression to confirm it looks like a valid phone number. For example, the pattern
^\+?[1-9]\d{1,14}$
works well for international numbers. 4. Provide Feedback: If the number doesn’t look right, gently ask the user to try again by replaying the prompt. If it’s good, you can move forward with your app’s next steps. If you’d like, I’m here to help with code examples or any other details you need!
t
Give me a code example
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
r
To achieve the functionality you described, you can implement a DTMF collection mechanism in your agent that plays a prompt asking the caller to input their phone number. After collecting the input, you can start the agent session. Below is an example of how you can implement this using the LiveKit Python API. Example Code
Copy code
import json
import asyncio
from livekit import api
from livekit import rtc
from your_tts_library import TTS  # Replace with your TTS library
from your_dtmf_library import DTMFCollector  # Replace with your DTMF collection logic

async def collect_phone_number(ctx):
    # Play a prompt asking for the phone number
    await ctx.room.local_participant.publish_audio("path/to/your/prompt.mp3")  # Play an MP3 file
    # Alternatively, use TTS
    # await TTS.speak("Please enter your phone number followed by the pound key.")

    dtmf_collector = DTMFCollector()  # Initialize your DTMF collector
    phone_number = ""

    while True:
        code = await dtmf_collector.wait_for_dtmf()  # Wait for DTMF input
        if code == "#":  # End input on #
            break
        elif code.isdigit():  # Collect digits
            phone_number += code

    return phone_number

async def main(ctx):
    # Collect the phone number from the caller
    phone_number = await collect_phone_number(ctx)
    print(f"Collected phone number: {phone_number}")

    # Now start the agent session
    agent = OutboundCaller(
        name="Agent Name",
        appointment_time="next Tuesday at 3pm",
        dial_info={"phone_number": phone_number},
    )

    session = AgentSession(
        turn_detection=EnglishModel(),
        vad=silero.VAD.load(),
        stt=deepgram.STT(),
        tts=cartesia.TTS(),
        llm=openai.LLM(model="gpt-4o"),
    )

    # Start the session
    await session.start()

# Assuming you have a context (ctx) available
# asyncio.run(main(ctx))
Explanation 1. Prompting for Input: The code plays an audio prompt asking the caller to input their phone number. You can use either an MP3 file or TTS to generate the prompt. 2. DTMF Collection: The
DTMFCollector
class (which you would need to implement) listens for DTMF tones. It collects digits until the caller presses
#
, which indicates the end of input. 3. Starting the Agent Session: After collecting the phone number, the agent session is started with the collected phone number. Notes • Make sure to replace the placeholders for TTS and DTMF collection with your actual implementations. • Ensure that your audio prompt file path is correct. • This example assumes you have a context (
ctx
) available to work with. This approach allows you to collect the caller's phone number before starting the agent session, as you requested. Referenceslivekit-examples/outbound-caller-python/agent.py#L169-L226livekit-examples/outbound-caller-python/agent.py#L169-L226livekit-examples/agent-demos/ivr-nagivator/agent.py#L57-L111livekit-examples/agent-demos/ivr-nagivator/agent.py#L57-L111livekit/livekit-omni/livekit-examples/agent-demos/ivr-nagivator/agent.py#L57-L111
t
Awesome, thanks for the very detailed answer!