In this tutorial, you can set up a simple SMS chatbot using the Twilio and API.ai platforms. You will get a step-by-step process for setting up your Twilio and API.ai accounts, and integrating the two platforms together to form a simple SMS chatbot. (This blog is a continuation of How to Design a Chatbot.)
Setting Up Twilio
- Sign up for Twilio. Trial accounts are available. With the trial account, you get one free Twilio phone number with voice, SMS, or MMS capabilities. (For this tutorial, we will use SMS.) For more information, please check out Twilio’s free trial page.
- Verify the account with your phone number.
- Click “Get started” and “Get a free phone number.”
- Send a message to test. Enter your own cell phone number in the To field, and the From field is your new Twilio phone number that you will receive messages from when we get the chatbot set up later.
- Then click “Make Request.” This will tell you if the Twilio number created sends messages successfully or not. If it did not succeed, you can create a ticket with Twilio support. Hopefully, it succeeded. If so, click “Yes.”
Congratulations! You have successfully set up your Twilio phone number. In the next step, we will set up the API.ai account.
There are many options when it comes to chatbot platforms. (If you want to see more options for chatbot platforms, please see the section Chatbot Platforms in How to Design a Chatbot.) We will be using API.ai. API.ai is a code-based chatbot framework that provides the flexibility to store data, produce analytics, and incorporate some level of AI. There is a lot of flexibility with this platform. It also has a good list of SDKs for integration, one of which is Twilio.
Setting Up API.ai
- Sign up at API.ai for a free account.
- Create an agent. Agents are modules that understand natural language process, which transforms a user’s request into actionable data.
Creating Entities with API.ai
Entities are used for extracting parameter values from natural language inputs. Any important data you want to get from a user’s request should have a corresponding entity. The importance of entities is that in natural language, people have different ways of phrasing things that mean the same thing. Take the example below—A user can say “NY,” “NYC,” “New York,” or “The Big Apple,” and they all refer to New York City. With entities, You can specify all synonyms associated with a particular word or item.
Create Intent with the City Entity
Create an intent. Intents represent a mapping between what a user says and what action should be taken by your software. In the example below, a user can type “Tell me about the Big Apple” or “What is NY like?” and “The Big Apple” and “NY” get mapped to the entity City that was created above. (Note: you may create multiple intents with API.ai.)
When creating the intent, there is an Actions section. Actions correspond to the steps your application will take when specific intents are triggered by user inputs. An action may have parameters for specifying detailed information about it.
You can access the values using $City in the Response section.
Integrate API.ai with Twilio
- Go to Integrations, toggle Twilio, and copy the request URL to your clipboard.
- Click on All products and services > Programmable SMS > Create a new Messaging Service
- In the configuration, click Process Inbound Messages, and paste the link in Request URL
- Add a number by clicking Numbers. Select the number and click Add Selected.
- Twilio and API.ai are now linked together. Time to test it out.
As you can see, this is a very simple SMS. Now that you know the steps to create a simple SMS chatbot using Twilio and API.ai, you can make the mapping as simple or as complicated as you want.