--- name: ivr-prompt-generator title: "IVR Prompt Generator" description: "Generate professional IVR/phone system prompts. AI writes caller-friendly scripts from business descriptions, TTS renders in multiple voices, test via live Telnyx call playback." language: python framework: flask telnyx_products: [Voice, AI Inference, Cloud Storage] --- # IVR Prompt Generator Generate professional IVR/phone system prompts. AI writes caller-friendly scripts from business descriptions, TTS renders in multiple voices, test via live Telnyx call playback. ## Telnyx Webhook Events This app handles these webhook events ([Call Control docs](https://developers.telnyx.com/docs/api/v2/call-control)): - `call.answered` — Call connected — app begins interaction (TTS greeting, gather) - `call.speak.ended` — TTS playback finished — app transitions to next action (gather, transfer, etc.) ## External Service Integrations - **Email / SMTP** — Email notifications and alerts ## Architecture ``` Inbound Phone Call │ ▼ ┌──────────────────┐ │ Answer + Greet │ ── TTS welcome message └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ Listen for Input │ └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ AI Inference │ │ • Business logic │ └────────┬─────────┘ │ ◄──── conversation loop │ ├──► Voice response ├──► Email └──► Cloud Storage ``` ## Telnyx API Endpoints Used - **AI Inference (script writing)**: `POST /v2/ai/chat/completions` -- [ref](https://developers.telnyx.com/api/inference/chat-completions) - **TTS Generate**: `POST /v2/ai/generate` -- [ref](https://developers.telnyx.com/api/inference/generate) - **Create Call (preview)**: `POST /v2/calls` -- [ref](https://developers.telnyx.com/api/call-control/create-call) - **Speak (playback)**: `POST /v2/calls/{id}/actions/speak` -- [ref](https://developers.telnyx.com/api/call-control/speak) - **Cloud Storage (S3-compatible)**: `s3.put_object(...)` via boto3 against `https://{region}.telnyxcloudstorage.com`, then a presigned GET URL -- [docs](https://developers.telnyx.com/docs/cloud-storage/quick-start) Telnyx Cloud Storage is S3-compatible, so rendered TTS audio is uploaded with the AWS SDK (boto3) pointed at the region-scoped Telnyx S3 endpoint — not a REST API. Auth uses your Telnyx API key as **both** the access key and the secret key. Each upload returns a time-limited presigned GET URL (valid 1 hour) you can drop straight into a Call Control `speak`/`playback_audio` command or a TeXML `` verb. ## Environment Variables Copy `.env.example` to `.env` and fill in: | Variable | Type | Example | Required | Description | Where to get it | |----------|------|---------|----------|-------------|------------------| | `TELNYX_API_KEY` | `string` | `KEY...` | **yes** | Telnyx API v2 key | [Portal](https://portal.telnyx.com/api-keys) | | `MAIN_NUMBER` | `string` | `+18005551234` | **yes** | Telnyx phone number (E.164) | [Portal](https://portal.telnyx.com/numbers/my-numbers) | | `CONNECTION_ID` | `string` | `149...` | **yes** | Call Control connection ID | [Portal](https://portal.telnyx.com/call-control/applications) | | `AI_MODEL` | `string` | `moonshotai/Kimi-K2.6` | no | AI Inference model | [Docs](https://developers.telnyx.com/docs/inference/models) | | `TTS_MODEL` | `string` | `telnyx/tts` | no | TTS model | [Docs](https://developers.telnyx.com/docs/inference) | | `BUCKET_NAME` | `string` | `voiceovers` | no | Cloud Storage bucket | [Portal](https://portal.telnyx.com/storage) | | `TELNYX_STORAGE_REGION` | `string` | `us-central-1` | no | Cloud Storage region (selects the S3 endpoint host) | [Docs](https://developers.telnyx.com/docs/cloud-storage/quick-start) | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/ivr-prompt-generator-python cp .env.example .env pip install -r requirements.txt python app.py ``` ### Webhook Configuration 1. Expose your local server: ```bash ngrok http 5000 ``` 2. Copy the HTTPS URL and configure in [Telnyx Portal](https://portal.telnyx.com): - **Call Control Application** → Webhook URL → `https://.ngrok.io/webhooks/voice` - **Messaging Profile** → Inbound Webhook URL → `https://.ngrok.io/webhooks/sms` ## API Reference ### `POST /prompts/generate` ```bash curl -X POST http://localhost:5000/prompts/generate \ -H "Content-Type: application/json" \ -d '{"business_name": "Acme Corp", "business_type": "SaaS", "departments": ["Sales", "Support", "Billing"]}' ``` **Response:** ```json {"set_id": "ivr-a1b2c3d4", "prompts_generated": 8, "voice": "nova"} ``` ### `GET /health` ```bash curl http://localhost:5000/health ``` ```json {"status": "ok"} ``` ## Webhook Endpoints ### `POST /webhooks/voice` Handles Telnyx Call Control webhook events. Called automatically by Telnyx — do not call directly. ## Troubleshooting | Issue | Cause | Fix | |-------|-------|-----| | `401 Unauthorized` | Invalid or missing API key | Verify `TELNYX_API_KEY` in `.env` matches your key in the [Portal](https://portal.telnyx.com/api-keys) | | Webhook not received | Local server not publicly reachable | Expose it with a tunnel (e.g. ngrok) and set the webhook URL in the [Telnyx Portal](https://portal.telnyx.com) | | `422 Unprocessable Entity` | Missing or malformed request fields | Check the request body against the API Reference above | ## Related Examples - [Abandoned Cart Recovery (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/abandoned-cart-recovery-python/README.md) - [Accounting Tax Season Line (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/accounting-tax-season-line-python/README.md) - [After Hours Nurse Triage (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/after-hours-nurse-triage-python/README.md) - [AI Appointment Booking SMS Flow (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-appointment-booking-sms-flow-python/README.md) - [AI Appointment Reminder SMS Voice (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-appointment-reminder-sms-voice-python/README.md) ## Resources - [AI Inference Guide](https://developers.telnyx.com/docs/inference) - [Cloud Storage Quick Start (S3-compatible)](https://developers.telnyx.com/docs/cloud-storage/quick-start) - [Telnyx Developer Docs](https://developers.telnyx.com) - [Telnyx Portal](https://portal.telnyx.com) ## Why Telnyx Telnyx is an **AI Communications Infrastructure** platform — voice, messaging, SIP, AI, and IoT on one private, global network.