diff --git a/voice-ivr/README.md b/voice-ivr/README.md
index c4aa7dc5..b4374730 100644
--- a/voice-ivr/README.md
+++ b/voice-ivr/README.md
@@ -1,28 +1,27 @@
# Voice IVR
-Navigate a IVR (phone tree) using keys or speech-to-text
+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.
## Pre-requisites
+- A Twilio account - [sign up here](https://www.twilio.com/try-twilio)
+- A Twilio phone number
+
### Environment variables
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.
In your `.env` file, set the following values:
-| Variable | Description | Required |
-| :------- | :---------- | :------- |
-MY_PHONE_NUMBER Your phone number true
+| Variable | Meaning | Required |
+| :---------------- | :------------------------------------------------------- | :------- |
+| `MY_PHONE_NUMBER` | The phone number that you want calls to be forwarded to. | yes |
### Function Parameters
-`/voice-ivr` is protected and requires a valid Twilio signature as well as the following parameters:
-
-| Parameter | Description | Required |
-| :-------- | :---------- | :------- |
-
+`/voice-ivr` is protected and requires a valid Twilio signature.
-`/handle-user-input` is protected and requires a valid Twilio signature as well as the following parameters:
+`/handle-user-input` is protected and requires a valid Twilio signature and expects the following parameters:
| Parameter | Description | Required |
| :------------- | :------------------------------- | :------- |
diff --git a/voice-ivr/assets/index.html b/voice-ivr/assets/index.html
index 6848d110..cdb37dff 100644
--- a/voice-ivr/assets/index.html
+++ b/voice-ivr/assets/index.html
@@ -1,90 +1,131 @@
-
+
-
-
-
-
- Get started with your Twilio Functions!
+
+
+
+
+ Get started with your Twilio Functions!
-
-
+
+
-
-
-
-
-
Your live application with Twilio is ready to use!
-
-
-
-
Get started with your application
-
- Follow these steps to try out your new app:
-
-
- This app presents callers with an IVR (phone tree) whose options can be selected using
- either phone keypad digits or the caller's voice.
-
-
-
Call your Twilio phone number
-
You should be greeted by a voice listing all options
-
Press any of the mentioned digits or try saying the words
-
-
+
+
+
+
+
+
+
+
+
Welcome!
+
Your live application with Twilio is ready to use!
+
+
+
+
Get started with your application
+
Follow these steps to try out your new app:
+
+ This app presents callers with an IVR (phone tree) whose options can
+ be selected using either phone keypad digits or the caller's voice.
+
+
+
Call your Twilio phone number
+
You should be greeted by a voice listing all options
+
Press any of the mentioned digits or try saying the words
+
+
-
-
-
-
-
Troubleshooting
-
-
- Check the
-
- phone number configuration
-
- and make sure the Twilio phone number you want for your app has a voice webhook
- configured to point at the following URL
-
-
-
-
-
-
-
-
+
+
+
+
+
Troubleshooting
+
+
+ Check the
+
+ phone number configuration
+
+ and make sure the Twilio phone number you want for your app has a
+ voice webhook configured to point at the following URL
+
+
+
+
+
+
+
+
diff --git a/voice-ivr/changelog.md b/voice-ivr/changelog.md
index 3982d461..144828c2 100644
--- a/voice-ivr/changelog.md
+++ b/voice-ivr/changelog.md
@@ -3,6 +3,7 @@
## [Unreleased]
## [1.0.0]
+
### Added
-- Initial release.
+- Initial release.
diff --git a/voice-ivr/functions/handle-user-input.protected.js b/voice-ivr/functions/handle-user-input.protected.js
index f7223e81..c5400093 100644
--- a/voice-ivr/functions/handle-user-input.protected.js
+++ b/voice-ivr/functions/handle-user-input.protected.js
@@ -1,25 +1,58 @@
-function sendMessage(context, event) {
+/**
+ * handle-user-input Function
+ *
+ * Description:
+ * This file contains the input handler to the Voice IVR Function template.
+ * The incoming phone call will be transferred from the voice-ivr Function
+ * to this function using the Gather TwiML verb which will also pass in the
+ * users input. This Function will read the users input and respond
+ * back depending on the users selection.
+ *
+ *
+ * Contents:
+ * 1. SMS Handler
+ * 2. Main Handler
+ */
+
+/**
+ * 1. SMS Handler
+ *
+ * This function will send an SMS of the address to the caller
+ * if the address option was chosen.
+ *
+ */
+
+async function sendMessage(context, event) {
const client = context.getTwilioClient();
- return client.messages
- .create({
- from: event.To,
- to: event.From,
- body: 'Here is our address: 375 Beale St #300, San Francisco, CA 94105, USA',
- })
- .then(
- (resp) => resp,
- (err) => {
- console.log(err);
- return Promise.resolve();
- }
- );
+ return client.messages.create({
+ from: event.To,
+ to: event.From,
+ body: 'Here is our address: 375 Beale St #300, San Francisco, CA 94105, USA',
+ });
}
/**
- * Handles the user input gathered in the voice-ivr Function
+ * 1. Main Handler
+ *
+ * This Twilio Function will create a new Voice Response using Twiml
+ * based on the users input from the voice-ivr Function.
+ *
+ * The function will fetch the user inputs by reading the Digits or
+ * SpeechResult variables from the incoming request parameters. If the
+ * user input was given by speech-to-text, it will convert it to its digit
+ * counterpart.
+ *
+ * If option 1 (sales) was chosen, the function will forward the call
+ * and dial the MY_PHONE_NUMBER specified in /.env. If option 2
+ * (opening hours) was chosen, the Voice Response will respond to the
+ * caller with the opening hours. If option 3 (address) was chosen,
+ * the SMS handler will be executed and text the address to the caller.
+ * If any other input was chosen, the call will go in a loop and redirect
+ * the call to the voice-ivr Function.
+ *
*/
-// eslint-disable-next-line consistent-return
-exports.handler = function (context, event, callback) {
+
+exports.handler = async function (context, event, callback) {
let UserInput = event.Digits || event.SpeechResult;
const twiml = new Twilio.twiml.VoiceResponse();
@@ -60,16 +93,10 @@ exports.handler = function (context, event, callback) {
twiml.redirect('voice-ivr');
}
- let request = Promise.resolve();
- if (UserInput === '3') {
- request = sendMessage(context, event);
+ try {
+ if (UserInput === '3') await sendMessage(context, event);
+ } catch (err) {
+ console.log(err);
}
-
- request
- .then(() => {
- return callback(null, twiml);
- })
- .catch((err) => {
- return callback(err);
- });
+ return callback(null, twiml);
};
diff --git a/voice-ivr/functions/voice-ivr.protected.js b/voice-ivr/functions/voice-ivr.protected.js
index 6cd8345d..b3b2fb55 100644
--- a/voice-ivr/functions/voice-ivr.protected.js
+++ b/voice-ivr/functions/voice-ivr.protected.js
@@ -1,7 +1,25 @@
/**
- * Returns TwiML that prompts the users to make a choice.
+ * Voice IVR
+ *
+ * Description:
+ * This file contains the entry point to the Voice IVR function template
+ * which will prompt the user with an IVR phone tree with three options.
+ *
+ *
+ * Contents:
+ * 1. Main Handler
+ */
+
+/**
+ * 1. Main Handler
+ *
+ * This is the entry point to your Twilio Function, which will create
+ * and return a TwiML Voice Response that will prompt the user with
+ * three options: Talk to Sales, Hours of Operation, or Address.
* If the user enters something it will trigger the handle-user-input Function and otherwise go in a loop.
+ *
*/
+
exports.handler = function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
const gather = twiml.gather({