Skip to content

Commit 2de4754

Browse files
authored
Merge pull request #609 from micaswyers-work/ai-ready-ivr-js
Add AI-Ready updates to voice-ivr project
2 parents 6bde7b8 + 5b41a66 commit 2de4754

File tree

3 files changed

+156
-21
lines changed

3 files changed

+156
-21
lines changed

voice-ivr/.env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Your phone number for call forwarding (Sales option)
2+
# Enter in E.164 format: https://www.twilio.com/docs/glossary/what-e164
3+
# Use a test phone number during development
4+
MY_PHONE_NUMBER=+15551234567
5+
6+
# Note: The following Twilio credentials are automatically provided by Twilio Serverless
7+
# and do not need to be set in your .env file:
8+
# - ACCOUNT_SID: Your Twilio Account SID (https://www.twilio.com/console)
9+
# - AUTH_TOKEN: Your Twilio Auth Token (https://www.twilio.com/console)

voice-ivr/AGENTS.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Voice IVR - Agent Guidelines
2+
3+
This document provides AI assistants with essential information for working with the Voice IVR project.
4+
5+
## Project Overview
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.
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.
18+
19+
### 2. Critical Parameters
20+
21+
Never modify these critical parameters without explicit instructions:
22+
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
26+
27+
### 3. Sensitive Data
28+
29+
Never expose or hardcode these sensitive details:
30+
31+
- Phone numbers (use environment variables)
32+
- Twilio account credentials
33+
- Customer information received during calls
34+
35+
## Coding Conventions
36+
37+
**TwiML must be returned via callback**: All functions must call `callback(null, twiml)` to return TwiML responses. Never use `return twiml` directly.
38+
39+
```javascript
40+
const twiml = new Twilio.twiml.VoiceResponse();
41+
twiml.say('Message to speak');
42+
callback(null, twiml); // Required pattern
43+
```
44+
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.
48+
49+
## Common Tasks
50+
51+
### Adding a New Menu Option
52+
53+
To add a new menu option (e.g., "Support"):
54+
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+
```
59+
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+
}
66+
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+
```
73+
74+
3. Add any necessary environment variable to `.env`:
75+
```
76+
SUPPORT_PHONE_NUMBER=+15551234567
77+
```
78+
79+
4. Add any required unit tests
80+
81+
## Further Resources
82+
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)

voice-ivr/README.md

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
# Voice IVR
22

3-
This application allows users to navigate an IVR (phone tree) using keys or speech-to-text via a Twilio number. When a user calls the number, they are presented with three options: Talk to Sales, Hours of Operation, or Address. Selecting the first option forwards the call to a given phone number. The second provides an immediate voice response with relevant information. Choosing the third option triggers an SMS with the requested details. The application is fully customizable, allowing you to edit the available options and responses.
3+
This application allows users to navigate an IVR (phone tree) using keys or speech-to-text via a Twilio number. When a user calls the number, they are presented with three options:
44

5-
## Pre-requisites
5+
1. Talk to Sales: Forwards the call to a given phone number (set in `.env`)
6+
2. Hours of Operation: Provides an immediate voice response with opening hours information
7+
3. Address: Triggers an SMS with address details to the caller's phone number
68

7-
- A Twilio account - [sign up here](https://www.twilio.com/try-twilio)
8-
- A Twilio phone number
9+
The user can select an option by dialing or saying the appropriate number.
910

10-
### Environment variables
11+
The application is fully customizable, allowing you to edit the available options and responses.
1112

12-
This project requires some environment variables to be set. To keep your tokens and secrets secure, make sure to not commit the `.env` file in git. When setting up the project with `twilio serverless:init ...` the Twilio CLI will create a `.gitignore` file that excludes `.env` from the version history.
13+
**For AI coding assistants:** See [AGENTS.md](./AGENTS.md) for implementation guidelines, do-not-touch areas, coding conventions, and common task recipes.
1314

14-
In your `.env` file, set the following values:
15+
## Prerequisites
1516

16-
| Variable | Meaning | Required |
17-
| :---------------- | :------------------------------------------------------- | :------- |
18-
| `MY_PHONE_NUMBER` | The phone number that you want calls to be forwarded to. | yes |
17+
- An [ngrok][ngrok_url] account
18+
- A [Twilio account](try_twilio_url) with an active phone number that can send SMS
1919

20-
### Function Parameters
20+
### Environment variables
2121

22-
`/voice-ivr` is protected and requires a valid Twilio signature.
22+
This project requires some environment variables to be set. To keep any tokens and secrets secure, make sure to not commit the `.env` file in git. When setting up the project with `twilio serverless:init ...` the Twilio CLI will create a `.gitignore` file that excludes `.env` from the version history.
2323

24-
`/handle-user-input` is protected and requires a valid Twilio signature and expects the following parameters:
24+
Copy `.env.example`, and set the following values:
2525

26-
| Parameter | Description | Required |
27-
| :------------- | :------------------------------- | :------- |
28-
| `Digits` | Automatically provided by Twilio | No |
29-
| `SpeechResult` | Automatically provided by Twilio | No |
30-
| `From` | Automatically provided by Twilio | No |
31-
| `To` | Automatically provided by Twilio | No |
26+
| Variable | Meaning | Required |
27+
| :---------------- | :------------------------------------------------------- | :------- |
28+
| `MY_PHONE_NUMBER` | The "Sales" phone number that you want calls to be forwarded to. | yes |
3229

3330
## Create a new project with the template
3431

@@ -51,16 +48,59 @@ twilio serverless:init example --template=voice-ivr && cd example
5148
twilio serverless:start
5249
```
5350

51+
> **Tip**: Run `npm test` from the repository root to run tests. Other available commands: `npm run lint`, `npm run format`.
52+
5453
5. Open the web page at https://localhost:3000/index.html and follow the instructions to test your application.
5554

56-
ℹ️ Check the developer console and terminal for any errors, make sure you've set your environment variables.
55+
Check the developer console and terminal for any errors, and make sure you've set your environment variables.
56+
57+
## Local Development & Testing
58+
59+
### Exposing your local server
60+
61+
To test with a real Twilio phone number, you need to expose your local server to the internet. You can use [ngrok](ngrok_url):
62+
63+
```bash
64+
ngrok http 3000
65+
```
66+
Copy the HTTPS URL (e.g., `https://abc123.ngrok.io`)
67+
68+
### Configuring your Twilio phone number
69+
70+
1. Go to your [Twilio Console Phone Numbers](https://console.twilio.com/us1/develop/phone-numbers/manage/incoming)
71+
2. Select the phone number you want to use
72+
3. Under "Voice Configuration":
73+
- Set "A Call Comes In" to **Webhook**
74+
- Enter your public URL: `https://your-ngrok-url.ngrok.io/voice-ivr` (or your deployed URL)
75+
- Set HTTP method to **POST**
76+
4. Click **Save**
77+
78+
79+
You can also use the Twilio CLI to configure your phone number's Voice URL.
80+
81+
First, retrieve the phone number's SID (starts with `PN`):
82+
83+
```bash
84+
twilio phone-numbers:list
85+
```
86+
87+
Using the phone number SID and ngrok URL that you copied above:
88+
89+
```bash
90+
twilio phone-numbers:update YOUR_PHONE_NUMBER_SID --voice-url="https://abc123.ngrok.io"
91+
```
92+
93+
Now when you call your Twilio number, it will trigger the `/voice-ivr` function.
5794

5895
## Deploying
5996

60-
Deploy your functions and assets with either of the following commands. Note: you must run these commands from inside your project folder. [More details in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit)
97+
Deploy your functions and assets with the following command. Note: you must run these commands from inside your project folder. [More information about the serverless toolkit in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit)
6198

6299
With the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart):
63100

64101
```
65102
twilio serverless:deploy
66103
```
104+
105+
[ngrok_url]: https://ngrok.com/
106+
[try_twilio_url]: https://www.twilio.com/try-twilio

0 commit comments

Comments
 (0)