Building the IT Crowd Answering Machine with Twilio Functions and JavaScript
Tags: javascript, speechrecognition, functions, twilio
Published: 2017-08-29 10:07:15

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5i4uvg3vi3bw044bivan.png
title: Building the IT Crowd Answering Machine with Twilio Functions and JavaScript
published: true
description: Learn how to build your own IT Crowd inspired speech recognition based phone answering machine using a few lines of JavaScript and Twilio Functions.
tags: javascript, speech recognition, functions, twilio
canonical_url: https://www.twilio.com/blog/2017/07/building-the-it-crowd-answering-machine-using-twilio-functions.html
---

_[Building the IT Crowd Answering Machine with Twilio Functions and JavaScript](https://www.twilio.com/blog/2017/07/building-the-it-crowd-answering-machine-using-twilio-functions.html) was originally published on the [Twilio Blog](https://www.twilio.com/blog) on July 4, 2017._

---

One of my favorite things from IT Crowd is their "Hello IT" machine. It’s the perfect solution for lazy people who are tired of answering the same IT support questions again and again. With [Twilio Functions](https://www.twilio.com/functions) and the new [Speech Recognition](https://www.twilio.com/speech-recognition) we can build our own version of this machine with just a few lines of code.

If you are not familiar with the answering machine from IT Crowd make sure to [check out this video of it in action](https://www.youtube.com/watch?v=PtXtIivRRKQ).

You can also check out the final result by calling one of these numbers:
- ðŸ‡¬ðŸ‡§ +44 20 3389 5853
- ðŸ‡ºðŸ‡¸ (415) 702-4376

Before we get going make sure you have a Twilio account. [Sign up for free](https://www.twilio.com/try-twilio).

Now let’s get coding!

![gif of moss typing in front of fire](https://www.twilio.com/blog/wp-content/uploads/2017/07/9u-nebu3e4qL8G-_qBcUMw0hk7Ga5DQqyHXNfeBDSCsa3WMkscKAYWehJQZfEDcjn6G2kzzXacGjJ0eWDwvpDjBuhNmavXbYWK4jlOfrLZdB7JJQeiPOZ0DjDfnt0Cq4B4RAxika.png)

## "Hello IT"

The first thing we need to do is create a new Twilio Function that will handle all of our requests. Twilio Functions allows you to host Node.js code directly within your Twilio Console. No need for you to spin up your own server.

To get started:

- Go into the [Runtime section](https://www.twilio.com/console/runtime/overview) of your Twilio Console
- click on [Functions](https://www.twilio.com/console/runtime/functions/manage)
- click on the red "+" and choose the blank template

Your screen should look like this:

![screenshot of twilio functions console](https://www.twilio.com/blog/wp-content/uploads/2017/07/ypsO9B0LZnKvuVuP-ylVhB3yGfmAPr-R3oindiEWxTBHSDEG1HTRQy18recs7By-FjEKUYbmSmyvB38tBMo4etbbT3E7e_y6pCg8MT-rNwAp223-jumasQAnHAVvVljRPHMoHZBr.png)

The code that you see is currently generating and returning an empty [TwiML](https://www.twilio.com/docs/api/twiml/) response. Additionally we have the option to name our Function and give it a path. Each account is given a unique base URL that looks like `fancy-badger.twil.io`, the path is used to differentiate between the Functions you create for your account. I’ll name mine "IT Support Hotline" and give it the path `/it-support`. You can give yours other values but make sure to replace it accordingly throughout the post.

Choose for Event in the Configuration section "Incoming Voice Calls" as this is the event we will use this Function for. Let’s start by greeting the caller with "Hello IT". We’ll choose a British accent since we don’t have an Irish one. So your hotline will sound a bit more like Moss answering than Roy. Change the commented line to the following code:

```js
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();
  twiml.say({ voice: 'man', language: 'en-gb' }, 'Hello I.T.');
  callback(null, twiml);
};
```

Make sure to save your Function. You can test if it works by going in your browser to the URL that you configured for this Function. You should see the following code returned:

```xml
<Response>
  <Say voice="man" language="en-gb">Hello I.T.</Say>
</Response>
```

Let’s configure this to a phone number to be able to fully test it. Go to the [Phone Numbers section](https://www.twilio.com/console/phone-numbers/incoming) and either click on one of your existing numbers that you would like to configure or [purchase a new phone number](https://www.twilio.com/console/phone-numbers/search). Once you are on the configuration screen, choose in the "Voice & Fax" section for "A call comes in" the values "Function" and the name of your Twilio Function.

![screenshot of phone number configuration screen](https://www.twilio.com/blog/wp-content/uploads/2017/07/CMrOly1uJrxfj7OssUp2QFm-d-qrn2qOMi9ExVXJ0uedwbb4wqghEic-DjgSH8wF1uMDzSXa55Rh7plJaceQnfQL5mkv798Ec2xfXMneDsQH_p5J7ont-BJTZfCCN5-PwKIrETTH.png)

Save the changes and give your phone number a call. You should be greeted by a male voice with a British accent saying "Hello IT".

![gif of roy answering the phone](https://www.twilio.com/blog/wp-content/uploads/2017/07/IwN_MiC7DQlX_aGN4zPkyv2OnXMI1648gsVfqZPCyLsIGh3ij8xIj-MZVx142sT_9IFiBAjz0jAGLahAfj4mpi7-RywFb-WLRrnB99fXEBJ0tuXsZDdqKb1sYh9lSBoGT9ZMn0G6.png)

## "Have you tried turning it off and on again?"

Now let’s add some [Speech Recognition](https://www.twilio.com/speech-recognition) to the mix by adding the following user flow:

1. User is greeted with "Hello IT"
2. System waits for user to finish what they are saying
3. System ignores what has been said and asks "Have you tried turning it off and on again?"
4. If the user mentions "thank" somewhere we say "You’re welcome, mate!".
5. If they don’t say it, we ask the same question again (because most likely they haven’t).

In order to use speech recognition we  use the [Gather verb in our TwiML](https://www.twilio.com/docs/api/twiml/gather) and set input to "speech". We will also set the action property to our URL, so that when there is a result Twilio will call this function again, and append a query parameter that helps us later to add more call flows. The first time the Function is called there will be no results from the speech recognition with [Gather](https://www.twilio.com/docs/api/twiml/gather), so we return the greeting. On subsequent calls to the Function we will get a `SpeechResult` parameter, accessible as `event.SpeechResult`, which contains the text the API retrieved from the speech.

Change your Function code appropriately:

```js
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();
  if (!event.SpeechResult) {
    twiml
      .gather({ input: 'speech', action: '/it-support?next=onoff' })
      .say({ voice: 'man', language: 'en-gb' }, 'Hello I.T.');
  } else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) {
    twiml.say({ voice: 'man', language: 'en-gb' }, "You're welcome, mate.");
    twiml.hangup();
  } else {
    askOnOff(twiml);
  }
  callback(null, twiml);
};
 
function askOnOff(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support' })
    .say(
      { voice: 'man', language: 'en-gb' },
      'Did you try turning it off and on again?'
    );
}
```

Save the changes in your Function and give your phone number another call. It should greet you with "Hello IT" and once you said something ask you again and again if you tried to turn it off and on again until you say "Thank you" or "Thanks".

![gif of Jen answering the phone](https://www.twilio.com/blog/wp-content/uploads/2017/07/ZxptNmL3bxUIenP7T2dadihimY_kgvSH9uueeYGwMQbk0ARQD4cSSBtCuU93qwwo7FnwJwP2nZBsCxwa2AOsSpa5v4Z7tVZ3vPh840aNS2Eva4vxvTMJ41-Z4yt-tbqH3fdNdFYs.png)

## "Is it definitely plugged in?"

One important question is still open though. "Is it definitely plugged in?". Let’s add that in case the person answers "yes" to the previous question. Add the following lines to your code:

```js
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();
  if (!event.SpeechResult) {
    twiml
      .gather({ input: 'speech', action: '/it-support?next=onoff' })
      .say({ voice: 'man', language: 'en-gb' }, 'Hello I.T.');
  } else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) {
    twiml.say({ voice: 'man', language: 'en-gb' }, "You're welcome, mate.");
    twiml.hangup();
  } else if (event.next === 'plugged') {
    if (event.SpeechResult.toLowerCase().indexOf('yes') !== -1) {
      askPluggedIn(twiml);
    } else {
      askOnOff(twiml);
    }
  } else {
    askOnOff(twiml);
  }
  callback(null, twiml);
};
 
function askOnOff(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support?next=plugged' })
    .say(
      { voice: 'man', language: 'en-gb' },
      'Did you try turning it off and on again?'
    );
}
 
function askPluggedIn(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support' })
    .say({ voice: 'man', language: 'en-gb' }, 'Is it definitely plugged in?');
}
```

Don’t forget to save and you should be good to go to test your support hotline again.

## Doing the work for them

We got the basic answering machine done but why don’t we leverage the fact that we can do so much more with Twilio Programmable Voice to try to help the caller. We could do something very sophisticated here but for now let’s actually ask them again for their problem and simply send them the link to the Stack Overflow search page with that question.

For this we need one more `gather` and one conditional where we send the SMS. Change your code to this:

```js
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();
  if (!event.SpeechResult) {
    twiml
      .gather({ input: 'speech', action: '/it-support?next=onoff' })
      .say({ voice: 'man', language: 'en-gb' }, 'Hello I.T.');
  } else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) {
    twiml.say({ voice: 'man', language: 'en-gb' }, "You're welcome, mate.");
    twiml.hangup();
  } else if (event.next === 'plugged') {
    if (event.SpeechResult.toLowerCase().indexOf('yes') !== -1) {
      askPluggedIn(twiml);
    } else {
      askOnOff(twiml);
    }
  } else if (event.next === 'problem') {
    askForProblem(twiml);
  } else if (event.next === 'solution') {
    const linkToStackOverflow =
      'https://stackoverflow.com/search?q=' +
      require('querystring').escape(event.SpeechResult);
    twiml.say(
      { voice: 'man', language: 'en-gb' },
      "Alright I found a few solutions. I'll send you an SMS with them. Bye."
    );
    twiml.sms(linkToStackOverflow);
  } else {
    askOnOff(twiml);
  }
  callback(null, twiml);
};
 
function askOnOff(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support?next=plugged' })
    .say(
      { voice: 'man', language: 'en-gb' },
      'Did you try turning it off and on again?'
    );
}
 
function askPluggedIn(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support?next=problem' })
    .say({ voice: 'man', language: 'en-gb' }, 'Is it definitely plugged in?');
}
 
function askForProblem(twiml) {
  twiml
    .gather({ input: 'speech', action: '/it-support?next=solution' })
    .say(
      { voice: 'man', language: 'en-gb' },
      'Alright what was your exact problem again?'
    );
}
```

Give your phone number another ring and perform the following scenario to test it:

- Call number
- Reply with "Hello"
- Say "Yes" to the question of turning it off and on again
- Say "Yes I did" to the question of whether it’s plugged in
- State a problem you want to search for
- You should receive an SMS with the link to Stack Overflow ðŸ‘

That’s it! Don’t forget to save your Function and once it restarts your new IT support hotline is ready to be distributed to everyone who constantly bugs you for help.

![gif of IT Crowd excited](https://media.giphy.com/media/XWRWrUOqs2Xpm/giphy.gif)

## What’s Next?

We’ve only used a few features from Programmable Voice so far and this is merely the beginning. There is a bunch of additional features you might want to look into:

- Use the [`<Play />`](https://www.twilio.com/docs/api/twiml/play) verb to replace [`<Say />`](https://www.twilio.com/docs/api/twiml/say) with the original sound from IT Crowd or your own recordings that you can store in the assets section of the Runtime.
- Use a smarter lookup for solutions by using the StackOverflow or Google API and sending the link to an actually useful result
- Send the actual question to you via Email or Slack. Maybe file a ticket in your system?
- Port the hotline to one of the [other languages supported](https://www.twilio.com/docs/api/twiml/gather#languagetags).
I would love to hear what kind of ideas you have or if you build something similar that makes your life easier. If you have any questions just send me a message:

I would love to hear what kind of ideas you have or if you build something similar that makes your life easier. If you have any questions just send me a message:

- Email: dkundel@twilio.com
- Twitter: [@dkundel](https://twitter.com/dkundel)
- GitHub: [dkundel](https://github.com/dkundel)

![gif of moss getting up and leaving](https://media.giphy.com/media/x5AlLBS6YmXUQ/giphy.gif)

---

_[Building the IT Crowd Answering Machine with Twilio Functions and JavaScript](https://www.twilio.com/blog/2017/07/building-the-it-crowd-answering-machine-using-twilio-functions.html) was originally published on the [Twilio Blog](https://www.twilio.com/blog) on July 4, 2017._