The SignalWire Realtime SDK enables you to create powerful communications automation on the fly. We used it to create an example programmable voice automation that reads out multiple options to hear information (such as hours and location), forward the call to a representative, reprompt, and use speech-to-text to create bookings. These features encompass what most interactive voice response (IVR) systems can do.
This example is just under 100 lines of javascript code. It’s already built to be fully production ready and easily customizable. To run the example you can follow the instructions in the GitHub Repository or use this Replit Template that can be run in the browser. In this post, we'll go through the example code step-by-step and explain what it's doing, as well as what you can customize for your own custom IVR.
Understanding the Code
The Realtime SDK connects as a RELAY Consumer Voice Client and registers a Context. Context is used in SignalWire to determine where to route calls and messages.
We then create an event handler for received calls. When a call is made to the SignalWire Inbound Phone Number configured to the context for this consumer, they will be passed to this function. Properties of the call are also available, such as call.from and call.to.
Create a Playlist
Next, we want to play a welcome sound, take a pause, and read out the call options while we prompt the caller. One of the options is to hear all the options again and re-prompt. To reduce redundancy, we can use a feature of the Realtime SDK that allows the creation of playlists.
We’ve combined playing an audio mp3, text to speech, and silence into a playlist object. That playlist along with the call object gets passed to a custom function, promptCallOptions.
Prompt the Caller
The promptCallOptions function is created so that we can recursively call this function when the caller asks to re-prompt.
The first thing it does is use the call.prompt function to prompt the caller to enter an option into the dial pad. We pass it the playlist object so it knows to play when prompted. By specifying the digits key, we’re telling it to accept dial pad digits instead of speech.
We set the max number of digits to input as 1 and the timeout as 2 seconds after pressing. By not indicating a terminator, simply pressing a digit will indicate a selection is confirmed. You can increase the number of digits and specify a terminator if you wanted to ask for something like “enter your pin followed by the pound key.”
We use prompt.waitForResult to block the script until an option is selected. Once selected, the script moves on to a switch case. The digits are returned as strings, so we check for the numbers in quotations.
List of Menu Options
Option 1 is to simply read out the hours and location and then hang up. Since we don’t need a whole playlist, we can just use call.playTTS and playback.waitForEnded to ensure it’s read out completely before calling call.hangup.
Option 2 is to make an appointment. Here we create an event handler for when the prompt has ended so that we can read out the result. Then we initiate a prompt using voice recognition by including the speech key. We set the endSilenceTimout to 3 seconds.
This means as soon as the caller finishes saying their name, number, and reason for calling, we accept that as completed input and the event handler will then read out the text in the console.
Once read to the console, call.hangup ends the call. Here you could modify the code to send the recognized text out as an SMS to a provider or call a third party API for making appointments in a calendar.
Option 3 is to restate the options, so we simply call the promptCallOptions function again.
Option 0 is using call.connectPhone to forward the call to a verified number. This can be used to allow callers to speak with a representative.
Now you can quickly and easily write and deploy an IVR application using the SignalWire RELAY Realtime API. This sample code performs all the operations you would expect from a standard IVR application - it can receive calls, play sounds, prompt a user to select from a list of options, read out requested information, recognize speech, and forward calls.
What you do next is entirely up to you. Integrate it with a third party API to further automate or dive in to what you can do with the Realtime API beyond Voice. This programmable API also be used for video, messaging, and chat.
If you have any questions as you set up and begin using your IVR, stop by our Community Slack or our Forum to connect with our team of expert developers.