|
1 | | -# Voice IVR - Agent Guidelines |
| 1 | +# Voice IVR |
2 | 2 |
|
3 | | -This document provides AI assistants with essential information for working with the Voice IVR project. |
| 3 | +This sample demonstrates a phone tree (IVR) using Twilio Voice and Programmable SMS, where callers navigate options by keypad or speech-to-text to reach sales, hear hours, or receive an address by SMS. |
4 | 4 |
|
5 | | -## Project Overview |
| 5 | +## Environment Variables |
6 | 6 |
|
7 | | -The Voice IVR project implements an interactive voice response system using Twilio. The system allows callers to navigate a phone tree using keypad digits or speech recognition. When users call the Twilio number, they can choose between: talking to sales, hearing business hours, or receiving an SMS with the company address. |
| 7 | +Copy `.env.example` to `.env`. Never commit `.env`. |
8 | 8 |
|
9 | | -## Documentation Links |
10 | | - |
11 | | -All Twilio documentation URLs in this guide can be accessed in markdown format by adding `.md` to the end of the URL. |
12 | | - |
13 | | -## Do-Not-Touch Areas |
14 | | - |
15 | | -### 1. Webhook Signature Verification |
16 | | - |
17 | | -The `.protected.js` suffix on function files indicates they require valid Twilio signatures. Never remove this protection or modify the validation logic. Twilio's serverless toolkit handles this automatically. |
| 9 | +```bash |
| 10 | +cp .env.example .env |
| 11 | +``` |
18 | 12 |
|
19 | | -### 2. Critical Parameters |
| 13 | +| Variable | Where to find | Format | |
| 14 | +| -------- | ------------- | ------ | |
| 15 | +| `MY_PHONE_NUMBER` | The phone number you want the Sales option to forward calls to — use your own mobile during development | E.164 format: `+15551234567` | |
20 | 16 |
|
21 | | -Never modify these critical parameters without explicit instructions: |
| 17 | +> Note: `ACCOUNT_SID` and `AUTH_TOKEN` are injected automatically by Twilio Serverless and do not need to be set in `.env`. |
22 | 18 |
|
23 | | -- `numDigits: 1` in voice-ivr.js - This ensures the system expects a single digit input |
24 | | -- `input: 'speech dtmf'` in voice-ivr.js - This enables both speech and touch-tone input |
25 | | -- Webhook paths (`voice-ivr` and `handle-user-input`) - These must match Twilio configurations |
| 19 | +## Commands |
26 | 20 |
|
27 | | -### 3. Sensitive Data |
| 21 | +```bash |
| 22 | +# Install Twilio CLI serverless plugin (once) |
| 23 | +twilio plugins:install @twilio-labs/plugin-serverless |
28 | 24 |
|
29 | | -Never expose or hardcode these sensitive details: |
| 25 | +# Initialize a new project from this template |
| 26 | +twilio serverless:init example --template=voice-ivr && cd example |
30 | 27 |
|
31 | | -- Phone numbers (use environment variables) |
32 | | -- Twilio account credentials |
33 | | -- Customer information received during calls |
| 28 | +# Run locally |
| 29 | +twilio serverless:start |
34 | 30 |
|
35 | | -## Coding Conventions |
| 31 | +# Expose webhooks locally |
| 32 | +# Requires ngrok — install and authenticate at https://ngrok.com before running |
| 33 | +ngrok http 3000 |
| 34 | +# Set the resulting HTTPS URL + /voice-ivr as the webhook in Twilio Console (Voice → A Call Comes In → POST) |
36 | 35 |
|
37 | | -**TwiML must be returned via callback**: All functions must call `callback(null, twiml)` to return TwiML responses. Never use `return twiml` directly. |
| 36 | +# Deploy to Twilio Serverless |
| 37 | +twilio serverless:deploy |
38 | 38 |
|
39 | | -```javascript |
40 | | -const twiml = new Twilio.twiml.VoiceResponse(); |
41 | | -twiml.say('Message to speak'); |
42 | | -callback(null, twiml); // Required pattern |
| 39 | +# Test |
| 40 | +npm test |
43 | 41 | ``` |
44 | 42 |
|
45 | | -## Tests |
46 | | - |
47 | | -Run `npm test` from the repository root. Comprehensive test coverage exists in `tests/` for both functions, covering DTMF inputs, speech recognition, error handling, and SMS delivery. |
| 43 | +## Project Structure |
48 | 44 |
|
49 | | -## Common Tasks |
| 45 | +- `functions/voice-ivr.protected.js` — entry point; presents the IVR menu via TwiML `<Gather>` |
| 46 | +- `functions/handle-user-input.protected.js` — handles digit/speech input; forwards call, reads hours, or sends SMS |
| 47 | +- `.env.example` — environment variable template |
| 48 | +- `tests/` — Jest test suite |
50 | 49 |
|
51 | | -### Adding a New Menu Option |
| 50 | +## Agent Boundaries |
52 | 51 |
|
53 | | -To add a new menu option (e.g., "Support"): |
| 52 | +**Always:** |
54 | 53 |
|
55 | | -1. Add new option in `voice-ivr.protected.js`: |
56 | | - ```javascript |
57 | | - gather.say('Press 4 or say Support to get technical help'); |
58 | | - ``` |
| 54 | +- Confirm `.env` is configured before running any command |
| 55 | +- Use the Environment Variables section to guide the user to each credential — don't ask them to find values without direction |
| 56 | +- Confirm the app is running before asking the user to test it |
59 | 57 |
|
60 | | -2. Add speech recognition and handler in `handle-user-input.protected.js`: |
61 | | - ```javascript |
62 | | - // In speech normalization section |
63 | | - if (UserInput.toLowerCase().includes('support')) { |
64 | | - UserInput = '4'; |
65 | | - } |
| 58 | +**Never:** |
66 | 59 |
|
67 | | - // In switch statement |
68 | | - case '4': |
69 | | - twiml.say('Connecting you to our support team'); |
70 | | - twiml.dial(context.SUPPORT_PHONE_NUMBER); |
71 | | - break; |
72 | | - ``` |
| 60 | +- Run the app with missing or placeholder credentials |
| 61 | +- Hardcode credentials or phone numbers in source files |
| 62 | +- Skip the `cp .env.example .env` step |
73 | 63 |
|
74 | | -3. Add any necessary environment variable to `.env`: |
75 | | - ``` |
76 | | - SUPPORT_PHONE_NUMBER=+15551234567 |
77 | | - ``` |
| 64 | +## Verify It's Working |
78 | 65 |
|
79 | | -4. Add any required unit tests |
| 66 | +1. Call your Twilio phone number. You should hear the IVR prompt offering three options. |
| 67 | +2. Press `1` (or say "Sales") — the call should forward to the number set in `MY_PHONE_NUMBER`. Press `3` (or say "Address") — you should receive an SMS with the address to the caller's number within a few seconds. |
80 | 68 |
|
81 | | -## Further Resources |
| 69 | +## Twilio Resources |
82 | 70 |
|
83 | | -- [Twilio TwiML Voice Documentation](https://www.twilio.com/docs/voice/twiml) |
84 | | -- [Twilio Speech Recognition](https://www.twilio.com/docs/voice/twiml/gather#speechmodel) |
85 | | -- [Twilio SMS Documentation](https://www.twilio.com/docs/sms) |
86 | | -- [Twilio Serverless Functions](https://www.twilio.com/docs/runtime/functions) |
| 71 | +- [Twilio Console](https://console.twilio.com) — credentials, phone numbers, webhook configuration |
| 72 | +- [Twilio Voice TwiML](https://www.twilio.com/docs/voice/twiml) — TwiML verb reference |
| 73 | +- [Twilio Serverless Toolkit](https://www.twilio.com/docs/labs/serverless-toolkit) — deploy and manage Twilio Functions |
| 74 | +- [Programmable SMS](https://www.twilio.com/docs/sms) — SMS API reference |
0 commit comments