Prompt
How do I build an IVR phone menu with a voice API?
Latest observation
To build an IVR phone menu with a voice API, you usually combine:
- A phone number
- A webhook/server
- Call control logic
- Speech or keypad input handling
- Routing to an agent, voicemail, or another menu
Basic IVR flow
Example menu:
- “Press 1 for sales”
- “Press 2 for support”
- “Press 3 to leave a message”
The voice API connects the call to your webhook. Your server responds with call instructions such as:
- play audio / text-to-speech
- gather DTMF digits
- listen for speech
- transfer the call
- record voicemail
Typical architecture
Caller → Phone number → Voice API → Your webhook → IVR response
Your webhook decides what happens next based on:
- which menu level the caller is on
- what digit they pressed
- what they said
- business hours, caller ID, language, etc.
Steps to build it
1. Buy or configure a phone number
In your voice provider dashboard:
- purchase a number
- enable voice
- set the incoming call webhook URL
2. Create a webhook endpoint
This is usually a small HTTP server that returns instructions in the provider’s XML/JSON format.
You’ll need endpoints like:
/voice/incoming/voice/menu/voice/transfer/voice/voicemail
3. Return a greeting and menu
For an incoming call, your webhook responds with something like:
- “Thanks for calling Acme Corp.”
- “Press 1 for sales.”
- “Press 2 for support.”
- “Press 3 to leave a voicemail.”
4. Collect input
Most voice APIs support either:
- DTMF: keypad digits
- Speech recognition: caller says “sales” or “support”
5. Route based on input
Your app maps the input to actions:
1→ connect to sales queue2→ connect to support queue3→ record voicemail- invalid input → repeat menu or send to operator
6. Add error handling
Plan for:
- no input
- bad input
- disconnected calls
- busy agents
- after-hours routing
Example IVR logic
Main menu
- 1: Sales
- 2: Support
- 3: Billing
- 0: Operator
Support submenu
- 1: technical issue
- 2: account issue
- 3: password reset
After-hours
- Record voicemail or send SMS with a callback link
Example pseudocode
def handle_incoming_call():
say("Welcome to Acme.")
gather_input(options=["1", "2", "3"])
def handle_menu_selection(digit):
if digit == "1":
connect_to("sales_queue")
elif digit == "2":
connect_to("support_queue")
elif digit == "3":
record_voicemail()
else:
say("Invalid option.")
handle_incoming_call()
Example TwiML-style flow
If your voice API uses XML instructions, the idea looks like this:
<Response>
<Say>Welcome to Acme. Press 1 for sales. Press 2 for support. Press 3 to leave a message.</Say>
<Gather numDigits="1" action="/voice/menu" method="POST">
<Say>Please enter your choice now.</Say>
</Gather>
</Response>
Then your /voice/menu endpoint checks the digit and returns the next action.
Best practices
- Keep menus short: 3–5 options max
- Offer a way to reach a human
- Support both keypad and speech if possible
- Handle invalid input gracefully
- Test with real phones
- Log call events for debugging
- Consider business hours and time zones
- Make voicemail and callback options easy
Minimal implementation checklist
- Voice API account
- Phone number configured
- Public webhook URL
- Incoming call handler
- Menu handler
- Transfer/queue logic
- Voicemail recording
- Error handling and retries
If you want, I can also show you:
- a working example in Python/Flask,
- a Node.js example, or
- an example for a specific provider like Twilio, Vonage, Plivo, or Telnyx.