Building a Programmable Voice IVR with Python and Telnyx
Learn how to build a production voice IVR system that handles inbound calls, menu navigation, and call routing. We cover Telnyx API integration, webhooks, state machines, and deployment.
The Problem: Customer Support Over Phone
Your support team is drowning in repetitive calls. Customers call asking "What's the status of my order?" or "How do I reset my password?" These calls take 5 minutes to handle. Your team answers 100 calls a day. That's 500 minutes of labor on questions an automated system could answer in 30 seconds.
You need a voice IVR: Interactive Voice Response. A phone system that answers calls, plays prompts, listens to DTMF tones (button presses), and routes calls intelligently. No building a call center. Just a smart phone system.
Deskline is an IVR platform built with Python and Telnyx. It handles inbound calls, plays menu options, collects user input, and routes or transfers calls. This post walks through the architecture and how to build one.
The Architecture
A voice IVR has four moving pieces: the phone network (Telnyx), webhooks receiving call events, a state machine managing call flow, and business logic connecting to your backend.
Setting Up Telnyx
First, you need a Telnyx account and a phone number. Telnyx gives you an API key. You configure a webhook URL: when Telnyx receives a call, it POSTs to your server with call details.
1. Buy a phone number from Telnyx dashboard
2. Generate an API key for authentication
3. Set webhook URL in your Telnyx app settings
4. Telnyx POSTs to your webhook on call events
5. Your server responds with Telnyx commands (play audio, collect digits, etc.)
The webhook receives a POST with call ID, caller phone, and event type. Your job: decide what to do next and return commands as JSON.
Building the State Machine
Each state handles a specific part of the call flow. The greeting state plays a welcome message. The menu state waits for button presses. The routing state transfers to a live agent.
Handling Webhook Events
Telnyx sends webhooks for three main events: call initiated, DTMF digits received, call hangup. Each webhook includes the call ID, which you use to look up the call session and determine what to do next.
The request-response pattern is fast: Telnyx calls your webhook, you process in milliseconds and respond with the next Telnyx command. The caller hears the response almost instantly.
Playing Audio and Collecting Input
The two main commands you send to Telnyx are: "play audio" (TTS or pre-recorded files) and "gather" (listen for DTMF digits). Combine them to prompt the caller and collect their response.
The "speak" action uses text-to-speech (TTS) to play the prompt. "gather_dtmf" listens for button presses. When the caller presses a digit, Telnyx sends it in the next webhook as the digits field.
Connecting to Your Backend
The real power comes when you connect the IVR to your backend. The caller presses "1" for order status. Your IVR collects their order number, queries your database, and speaks the result back.
The IVR becomes a voice interface to your business logic. Database queries, API calls, business rules all work the same way. The difference is the caller hears responses spoken aloud instead of reading them on a screen.
Production Considerations
- •Signature verification: Telnyx signs every webhook. Verify signatures in your handler to prevent spoofed calls.
- •Call recording: Record calls for compliance and quality review. Enable recording in Telnyx settings and store securely.
- •Error handling: Network timeouts happen. Design fallback messages and graceful error paths.
- •Call timeout: If a caller is silent too long, hang up gracefully. Telnyx handles this with timeout settings.
- •Agent handoff: Transfer to a live agent when needed using the "transfer" action. Map to your desk phone or call center.
- •Billing and monitoring: Telnyx charges per minute. Monitor spending and set alerts. Log call metrics for analytics.
See It in Action
Deskline is a full production voice IVR handling real calls end-to-end. Check out the case study for the complete architecture, including call state persistence, error recovery, and integration with backend systems.
Deskline Case StudyKey Takeaways
- ✓Voice IVRs answer calls, play prompts, collect input, and route intelligently. No call center needed.
- ✓Telnyx handles the phone network. You build a webhook handler and state machine.
- ✓Request-response architecture: Telnyx POSTs events, you respond with commands.
- ✓Connect the IVR to your backend: database queries, API calls, business logic all work through voice.
- ✓Production IVRs need signature verification, error handling, call recording, and agent handoff.