Building an Intelligent Coffee Order System with Twilio Autopilot
Tags: node, bots, twilio, machinelearning
Published: 2019-03-18 18:39:27

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4c69cscp0m0up1m2dz72.png
title: Building an Intelligent Coffee Order System with Twilio Autopilot
published: true
tags: nodejs, bots, twilio, machinelearning
canonical_url: https://www.twilio.com/blog/intelligent-coffee-order-system-with-twilio-autopilot
description: Learn how you can get started building intelligent SMS bots using Twilio Autopilot by building a coffee ordering system
---

Wouldn't it be great if you could save daily a few, maybe awkward, interactions with other humans and let bots take care of it instead? However, often these bots are not too intelligent when it comes to interacting with you. In this blog post we'll learn how we can build a smarter SMS bot in just a few minutes.

In a previous blog post we talked about how we love to serve coffee at conferences and other events "Twilio-Style" by allowing attendees to order their coffee via SMS. If you haven't read the blog post, make sure to check out [Serving Coffee with Twilio Programmable SMS and React](https://www.twilio.com/blog/2018/03/serving-coffee-with-sms-and-react.html). Unfortunately we had a fairly rudimentary way of handling the orders. Until now.

Let's see how we can upgrade this existing project using Twilio Autopilot to be truly intelligent.

If you would prefer to see how this works by watching a video, here is our [tutorial on YouTube](https://www.youtube.com/watch?v=gOOrvhbJ9No):
{% youtube gOOrvhbJ9No %}

## Setup

Before we get started, make sure you have your setup ready. If you've performed the steps in our previous blog post, you are all set. If you haven't, make sure to check out "[Serving Coffee with Twilio Programmable SMS and React](https://www.twilio.com/blog/2018/03/serving-coffee-with-sms-and-react.html)". Alternatively follow the setup instructions in the `README` of the [Barista Lite GitHub project](https://github.com/dkundel/barista-lite).

## Creating your Personal Coffee Assistant

In order to get started with Twilio Autopilot, we'll have to create a new "Assistant". For this head to the [Autopilot section of the Twilio Console](https://www.twilio.com/console/autopilot/list) and create a new Assistant. You can give the assistant any name. I'll name mine "CoffeeBot" for now.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/49hWq7ZmrekGOB9Z8U4V5ZeWudwozNDPaG3U-t1yAErFNN.width-500.png)

Once created let's make sure we wire it up to our phone number to start testing the out-of-the-box experience. Click on "Channels" on the left side and choose "Programmable Messaging".

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/9P5-R320tIvGzPkzz8F3P3ADZq49A3Nf0_ztI4uCgtUmpQ.width-500.png) ![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/UsACrs-1LdTAltmQjlpr2N2zWeefBKriL1dNc79lxAi00w.width-500.png)

You should see a URL there. Copy it and go to your phone number in the [Twilio Console Phone Numbers section](https://www.twilio.com/console/phone-numbers) and update the "When a message comes in" webhook to the URL you copied and make sure to press Save.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/iI_1-681QCN7ieoc8nwQxVBxAOy9z2Vl1isemiMsYswQVW.width-500.png)

Once saved, take your phone and send any message to your number. You should see as a reply "This is your new Task".

This message comes from the default task that has been created when you created your assistant. But what is a Task?

## What are Tasks, Samples, Fields, Models, ...???

When you start working with Autopilot and you haven't worked with any Natural Language Processing before, there might be lots of new terms that might be confusing for you. So let's try to clear these up a bit more.

- **Tasks:** These are different units of work that you want to perform. Autopilot allows you to perform different "actions" inside a task. Those could be things like "say" to communicate a message to the user, "handoff" to forward the communication to a human, "redirect" to ping a webhook to decide what to do next, "collect" to gather a bunch of data, or many more.
- **Samples:**   In order to let Autopilot understand when to trigger which task we need to provide it with sample statements and map these against the existing tasks. The more samples you have per task, the more intelligently your bot will be able to route your user to the right task.
- **Fields:** Sometimes your samples are not fully static. For example a sample like "I would like to have one espresso" has some important pieces of info that you want to extract that might vary from user to user. Say the quantity and the type of coffee are actually fields in this sample. We'll later look at how we can work with them and the different available Field Types.
- **Models:**  Every time you modify these parts of your Autopilot assistant, you'll have to rebuild a new model. You can imagine a model as a giant "smart" decision tree that is the outcome of all the info you gave your assistant. The more info you give it, the more complex and smarter your model gets.

## Creating your First Task

In order to see your existing tasks and create new ones, click on the ["Task Builder" section](https://www.twilio.com/console/autopilot/CoffeeBot/tasks) of your assistant. You'll see one existing task there already called "hello\_world". It also has some labels attached to it to signalize that it has been configured as the `Fallback`, `Initiation` and `OnFailure` task. If you want to understand what these all mean or change one of them, click on the "Defaults" tab in the Tasks view.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/bOy5avgMYP0lGYYvJilNyoz1GHQJA3shORznw1z2hbUhBF.width-500.png)

Instead of creating a task from scratch, let's start by modifying this one. Click on the task and you'll see a code editor pop up that contains the following JSON:

```json
{
  "actions": [
    {
      "say": "This is your new Task"
    }
  ]
}
```

Inside it you can see it shows the exact text we received earlier. Let's modify it to have a more friendly greeting:

```json
{
  "actions": [
    {
      "say": "Hi there! Thanks for reaching out to your friendly Coffee Bot. Please let me know what you would like to order."
    },
    {
      "listen": true
    }
  ]
}
```

As you can see we changed the text behind the "say" key but also added a second action called "listen" to tell Autopilot that it should keep the session open. After modifying the JSON, hit save.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/cJ5p8Phj__TGermuE-aBvY5V2e_Zh3Tk6FJgm3GDdUqAMJ.width-500.png)

Next let's add some samples to trigger this task. Go to the ["Natural Language Router" section](https://www.twilio.com/console/autopilot/CoffeeBot/router) and click on the expand button next to the text field. This way we can add multiple samples at once by adding them on different lines. Feel free to add whatever sample sentences you want or copy the following:

```plain
hey there
hi
what's up
hello
```

Select the `hello_world` task next to it and press the "+" button to add them:

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/Faf81oe0R0sCEeJXMYKozA1UL57RE_LdzZ_cI3_EmflWEz.width-500.png)
 
After doing all these changes we need to create a new model. Go to the ["Build Models" tab](https://www.twilio.com/console/autopilot/CoffeeBot/router/model-builds) and create a new Model Build with a name like "v0.0.1".

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/hry2KwfbeZPRAtIzXb9DHzVx-ofPU-TNwJZkXasJdXBMIU.width-500.png)

Once your model's status changes to "Completed" your bot is ready to be tested.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/C-tFP-Quat8QSN9CVFNKXdGUZ8GNN8d3_lc1CQmyknzwmq.width-500.png)

For this text anything you want to your number and you should see an updated message.

![Screenshot of Autopilot welcome message](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/Photo_20.02.19_18_38_20.width-500.jpg) 

## Creating a Dynamic Task

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/original_images/Ms1HxAWtbxB7kM9bNbClZvK_p7n_S8-FM7qzbAQxPTn8ifIuAT-4v5xiMIOPuhOLSEyLx6jJZ8zm5a)

Alright, now that we have a static task let's bring in some more dynamic experiences using Fields. As previously mentioned, Fields allow you to add placeholders inside your samples that will later be automatically extracted by Autopilot so that we can work with them.

Before we can work with the fields though we'll need to create a new task and something that will handle the field values later. For this let's first create a Twilio Function that will log the value of a field and return a `say` action.

Go to the [Functions section](https://www.twilio.com/console/runtime/functions/manage) of [Twilio Runtime](https://www.twilio.com/console/runtime/overview) and create a new "Blank" function. Give it a name like "Log Field Value" and add a path like `/log-value`. Change the code of your Function to the following:

```js
exports.handler = function (context, event, callback) {
  console.log(event.Field_Quantity_Value)
  const response = {
    actions: [
      {
        say: "Thank you for your order."
      }
    ]
  }
  callback(null, response);
};
```

Afterwards go back into your CoffeeBot Assistant and create a new task in [Task Builder section](https://www.twilio.com/console/autopilot/CoffeeBot/tasks). Give it a name like `new_order` and add the following code:

```json
{
  "actions": [
    {
      "redirect": "https://<your_runtime>.twil.io/log-value"
    }
  ]
}
```

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/fYpklGZsM-qIrl0CT9rngsiE8X_Js0_43lcErVBg5Pr6GO.width-500.png)

Make sure to update the URL with the link to your Twilio Function. Afterwards click save to make sure your changes are not lost.

Next we need to add Fields to this task. Click on the "Modify" link next to our task and in the dialog click the "View Fields" link. In there you'll be able to add new Fields. Create a new Field with the name `Quantity`. Autopilot comes with a variety of predefined data types for Fields. In our case we care about the Quantity number. So go ahead and choose "Twilio.NUMBER" as the type of this field.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/y_w-gsw-R4vvZk0onIP9MSIi4GppSYQEkiOeM3rPkSzOQj.width-500.png)

Afterwards close the dialog again and switch to _S__amples_ as we'll have to add new samples for our task. Expand the input field and place the following values into it:

```plain
I would like to have {Quantity} coffee please.
{Quantity} coffee.
Could you make me {Quantity} coffee please?
Can I have {Quantity} coffee?
```

The `{Quantity}` tells Autopilot that this is a placeholder for the Field "Quantity". Associate the samples with the `new_order` task and add them by hitting the "+" button.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/-BU6Naz8LjEzWDvq0OT8qkIu_2CH7bI9XgkCWDFVG7VJQP.width-500.png)

After creating the samples, go to the `Build Models` section again and trigger a new model build. Once that's finished, go back to your Twilio Function to see the logs in the bottom of the Function and take out your phone.

Text it something like "Ten coffee please" and check the logs of your Function. You should see it log "10".

That's because we are logging the parameter `Field_Quantity_Value`. Autopilot will automatically pass every captured Field as `Field_<FIELD_NAME>_Value` to the webhook. You can also see that it turned `Ten` into `10`. That's because we told Autopilot that this Field is of type number. It will now handle both numbers as digits or words. Try it by texting "Can I have 15 coffee?"

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/H8UqFjqp_mdzWYcguT-LQY43lEoi_hp5hxkhkyJrUq1tY_.width-500.png)

## Adding Custom Field Types

While the built-in Field Types serve a variety of use cases, there will be the situation where you'll want to have your own Field Type. In our CoffeeBot case this would be, for example, the different types of coffee that we serve.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/original_images/JYu4LjtQ3d9OOvdLkgAN0MeLvUTKI_Q7LVvxquUzrL_7Pyiex6UqoDXReMAA0BOdo9L_xYZmQBdcHp)

To create a new Custom Field Type go into the Natural Language Router part of your bot and click on the [Manage Fields](https://www.twilio.com/console/autopilot/CoffeeBot/router/field-types) tab. Press the "Create your first Field Type" button and give it a name like `coffee_type`. Once created click on the name in the list of Field Types and press the Plus button to add new examples. This is where you'll want to add possible values for this type. In our case this would be any valid Coffee type. You can enter one value per line for ease of use.

```plain
espresso
americano
latte
mocha
tea
coffee
flat white
```

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/6XJxGn-YtMAIQHueJkmLVQg7shhrlxZ9VJ94O2GGI9_6l-.width-500.png)

Note that the values that you provide here will not be the only valid ones. Autopilot will learn and try to match other words to this field type as well.

After you created them, let's add `CoffeeType` as a valid Field for our `new_order` task. Go back into the [View Fields](https://www.twilio.com/console/autopilot/CoffeeBot/tasks/tasks/new_order/fields) part of your `new_order` Task and add a new Field of name `CoffeeType` and data type `coffee_type`.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/UW5CWm2oHG0Ye4yhEbGM2Gzt_g0sMqLnXLRjnAZyNhHyIp.width-500.png)

Now before we build our new model, we need to update our samples to use the new field. Go ahead and delete the old samples for the `new_order` and instead create new samples using the following value:

```plain
I would like to have {Quantity} {CoffeeType} please.
{Quantity} {CoffeeType}.
{CoffeeType}
Could you make me {Quantity} {CoffeeType} please?
Can I have {Quantity} {CoffeeType}?
```

Once saved, go into [Model Builds](https://www.twilio.com/console/autopilot/CoffeeBot/router/model-builds) and build a new Model by incrementing the version in your build name.

Your model will now be able to understand both quantity and different coffee types.

## Connecting our Ordering System

To actually be able to create new orders we now need to change the action that we are triggering to our original Twilio Function URL and update our Twilio Function.

Go to your "Barista Create Order" Twilio Function from the previous blog post and update it accordingly:

```js
exports.handler = function (context, event, callback) {
  const ORDER_LIST = 'orders';
  const SERVICE_SID = context.SYNC_SERVICE_SID || 'enter Sync Service Sid';

  const orderType = event.Field_CoffeeType_Value;
  const orderQuantity = event.Field_Quantity_Value || 1;
  const order = `${orderQuantity}x ${orderType}`;

  const orderData = {
    order: order,
    phoneNumber: event.UserIdentifier,
    status: 'open'
  };

  // Create a sync list item for the order
  const twilioClient = context.getTwilioClient();
  twilioClient.sync
    .services(SERVICE_SID)
    .syncLists(ORDER_LIST)
    .syncListItems.create({ data: orderData })
    .then(x => {
      callback(null, {
        actions: [
          {
            say: `Thank you for ordering ${order}`
          }
        ]
      });
    })
    .catch(err => callback(err));
};
```

This will read the right fields as well as fall back to a quantity of one if no quantity could be detected. Additionally we are using the `event.UserIdentifier` here to get the phone number since this will be the phone number for SMS and voice bots. Note that this won't work for Alexa or Google Home for example.

Save your Function changes and copy the URL to your Function. Go back into your Autopilot Task and modify the `new_order` task and update the URL for the redirect to your Function URL.

Make sure you rebuild your model one more time to catch all changes.

If you haven't yet opened your terminal do that now and start your React interface from the previous blog post by running:

```bash
npm start
```

You should see in your browser the following screen:

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/2JO_7mES5rGq68K36k1R3zzMbntaURgFOGIeAnE_7K7ST-.width-500.png)

Let's test if everything is working by texting something like "Can I have two latte please?". Note that we are spelling it lowercase and are altering our sentence from the example sentences.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/diU-_pCiOEGwVmo-TlPFQwCrQpwi1ql3JmPn5uzjR3KNAD.width-500.png)

You should get a confirmation of your order of "2x Latte" and it should appear in the browser UI. Click "Finish Order" or "Cancel Order" and you should receive the respective confirmation via SMS.

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/Sg9I7t6-Syf7KyG7gsYgDAFNwE1E5iGz2oLSaas0RrUxkX.width-500.jpg)

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/original_images/huVgRNib8wjC1mQEyROrziT0FbnFep5FkPh6MVSkEGzql7ZpNjgqMXE_Ai78qxIH9bYfcbJeKhbggU)

## What's Next?

![](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/original_images/hyNQhFfiLX-90pf78CoPKD8NYdP78qRNR3NEuaB3ICj_hghwgkQ3XUpBbe9fqNrYv28vSa9RMbHGRU)

This is just the beginning of what you can do with Twilio Autopilot. If you want to learn more, I suggest you check out the [Collect action](https://www.twilio.com/docs/autopilot/actions/collect) that allows you to do form-filling with ease. You could use it for example to ask the customer for additional information like their name or if they want to have soy milk added to their coffee. Or check out how you can use the [Handoff](https://www.twilio.com/docs/autopilot/actions/hand-off) functionality to connect either to a [Twilio Flex](https://www.twilio.com/flex) agent or directly to the phone of your barista for any questions the bot can't answer.

And if you want to import/export your bot to another account for example, you should check out the [autopilot-cli](https://github.com/twilio/autopilot-cli) that will help you with building your bot.

If you have any questions or if you want to show me what cool thing you built with Autopilot or just in general, feel free to reach out to me:

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