Build Twilio Autopilot Chatbot for SMS and Whatsapp using Python
By: Pragnakalp Techlabs
Tags: Python, Chatbots, Bots, Chatbot Development, Dialogflow

Autopilot is a conversational AI platform offered by Twilio to build, train, and deploy artificially intelligent chatbots, Conversational IVRs (voice-driven phone menus), and Alexa skills using natural language understanding and machine learning.

This blog contains a step-by-step tutorial to create your first virtual assistant with Twilio Autopilot.

Prerequisites:

1) You need to sign up for a Twilio account or sign into your existing account. You can sign up for a free Twilio trial account here.

2) Install Flask, you can install it using the following command:

pip install Flask

3) Install ngrok.

1) Create a bot

Once you have logged in to the Twilio, and if it’s your first time, you need to select few things including the programming language(select python), then it will take you to the Twilio console (i.e. dashboard), select the three dotted circle from your left hand as shown in the figure.

Now select the AutoPilot option from the menu as shown below.

Click on “Build a bot” option from the left panel as shown below.

Trending Bot Articles:

You will see some pre-trained templates on the screen, you can even use those templates to build your project if it fulfills your requirement Here we will create a bot from scratch, for that scroll down, and select the “start from scratch” option.

Provide name to the bot and select the “create bot “ option.

2) Create task for your bot

Once you have created a new bot, you will see the below like screen, which has some predefined task. We will create a new task, for that click on the “Add a task” buttons.

Provide the task name and then click on the “Add” button to create a new task.

Now we will add the training phrases, for that click on the “Train” option of the booking task, as shown in the figure.

Add the training phrases which the user can use to invoke the task, add minimum 8 to 10 sample data as shown in the figure.

3) Unzip the package

Now unzip the package in some folder and run the following command in the terminal

Open new terminal and run the ngrok on port 5000

ngrok http 5000

Now copy the https url from the ngrok screen as shown below.

4) Changes in the bot and code file

Go back to the Twilio, select the “program” option of the booking task as shown in the figure.

Now remove the default code and copy and paste the below code over there as shown in the figure.

Where “https://ae54546f.ngrok.io” is the ngrok address, you need to paste the address which you get on your terminal with /dynamicsay.

Once you have changed the code, now “Save” it and then “build” the model.

You need to make change in the code file named “dynamicsay.json”, change the ngrok URL in that file with your ngrok address as shown below.

Stop the flask app and again run it using the following command

Testing

Now come back to the Twilio, and select the “Simulator” option from the left-hand side menu.

Initiate the conversation using “Hello” followed by the “booking”, bot will continue to ask you some questions.

Twilio Programmable SMS

1) Go to your Console Phone Numbers page.

2) Now buy or select the phone number you want to use for your Assistant, we already had a number, hence we have selected it. If you don’t have a number you can select on the “+” icon to buy a new number.

3) Once you click on the number, you will see the below like screen.

4) Now scroll down the page to the Messaging Section as shown below. In Messaging Configuration select Webhooks, TwiML Bins, Functions, Studio, or Proxy option, and fill the URL in the following format https://channels.autopilot.twilio.com/v1/<ACCOUNT_SID>/<ASSISTANT_SID>/twilio-messaging.

If you are not been able to find the URL see step 5 & 6, otherwise continue with step 7.

5) Go to your Autopilot bot, Select the “Channels” tab from the Assistant menu, From channels select the “Programmable SMS” option.

6) You will get the Messaging URL, copy that, and paste it in the URL section.

7) Once you have entered the Messaging URL, now SAVE the setting as shown below.

8) Now send the SMS from that number, you will get the response.

9) If you face this error “Permission to send an SMS has not been enabled for the region indicated by the ‘To’ number:” then please enable the relevant permissions on your account using the Messaging Geographic Permissions page.

10) If you face this erorr “ The ‘to’ phone number provided is not yet verified for this account.” then please add that number in the Verified caller IDs from this page.

Twilio AutoPilot with WhatsApp

First, we need to set up the sandbox:

Set up the Sandbox

1) Follow these step-by-step instructions for setting up your Sandbox of the WhatsApp console menu. When you click on the link you will see the below like screen. Save the number in your cell, and send the code as a whatsapp message on that number.

2) Once you send the code as a message in the whatsapp you will see “Message Received” on the screen.

3) Once configured, you can see all the configuration details in the Sandbox page of the WhatsApp menu. You’ll need these for the next set of steps.

Connect Autopilot to the Sandbox

Once you have successfully configured your Sandbox, you’ll need to connect it to your Autopilot Assistant.

1) Go to the Autopilot menu in the console and click into the Assistant you want to connect to WhatsApp.

2) Go to the Channels tab in the Assistant Menu and select WhatsApp.

3) Copy the URL displayed in the Configuration tab. This is the callback URL used to send incoming messages to your Assistant.

4) Go back to the Sandbox page in the WhatsApp console menu. Paste the callback URL where it says ‘When a message comes in’.

5) Now scroll down to the page and Save it.

Test

Now that you have WhatsApp configured it’s time to test!

1) Open the WhatsApp app

2) Tap into the chat window with your WhatsApp bot

3) Send your first message, you should get a response from Autopilot, as shown below.

Feel free to comment your doubts/questions. We would be glad to help you.

If you are looking for Chatbot Development or Natural Language Processing services then do contact us or send your requirement at letstalk@pragnakalp.com. We would be happy to offer our expert services.