Connecting a contextual assistant to a messaging service is one of the last steps (and most satisfying) in the development of your Rasa assistant. There are many options for messaging channels, when you're ready to make your assistant available to real users. In the Rasa Masterclass, we reviewed how to connect the example medicare locator assistant to Telegram and a website chat widget.
In this tutorial, we'll review how to connect your Rasa assistant to a Twilio phone number, so users can text your bot directly from their phone.
Getting Started
For this example, I'm going to use a bot created as a hobby project. This bot is a very simple assistant to help my teenagers decide if they should call me, when I don't respond to a text. Since I've received actual texts like "I broke my collarbone", I thought an assistant to help the injured party decide when to actually call me would be useful. I made it friendly, so it sort of sounds like mom, connected it to Twilio, and asked the kiddos to text it. (And from there, much hilarity ensued. If you're interested, outtakes from mombot
and what I learned available here.)
I created the bot primarily by following the Rasa Masterclass and reading the accompanying Handbooks. From Rasa Masterclass episode #9, I deployed Rasa X to a server, using Google Cloud Platform(GCP) and docker-compose, and activated Rasa X's integrated version control in connection with the GitHub repository containing my assistant's code. Then, following the details in the first part of Rasa Masterclass #11, I configured a domain name and SSL so the assistant would be available at a secure, accessible URL. My bot is hosted via GCP at www.momalachat.com
.
Connecting to Twilio
Twilio is a cloud communications platform that allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.
I started by creating a Twilio project that I named Mom bot Demo
, from the Twilio dashboard.
The project starts as a free trial, and I needed to verify I was human to continue. And I answered a few setup questions from Twilio - yes I write code, my preferred language is node.js, and I'm going to use Twilio in a project today. Then I upgraded to a paid account to remove the restrictions placed on a Twilio trial account. After I upgraded, I had an option to buy a phone number.
(There are a few ways to buy a number within Twilio, and plenty of details in their documentation.
I clicked through and selected a number, all within the Mom bot demo
project.
This is a long page of information about the phone number I selected, so I scrolled down to set up the webhook information for my mombot. It has default settings from Twilio of https://demo.twilio.com/welcome/sms/reply
We have to include the webhook created by Rasa, which has the format:
https://<domain>/core/webhooks/twilio/webhook
Since my bot domain is momala.chat
, I updated the webhook for incoming messages to https://momala.cat/core/webhooks/twilio/webhook
.
After saving the settings, I can see the updated webhook URL in the Mom bot Demo project's phone numbers:
Lastly, I need to get the project's Account SID and Auth Token to include in my Rasa assistant, from the Mom bot Demo project dashboard:
In my terminal, I opened and edited the credentials.yml file, and included the account_sid, auth_token, and twilio_number.
I exited and saved the credentials file, and brought the docker container down, then back up. Then I went to https://momala.chat
to my Rasa X instance, and retrained the model.
And now I can text my bot on the Twilio phone number:
Additional Resources
There are many Rasa resources to help you get started with building an AI assistant:
- The Rasa Masterclass & Handbooks - a series of 12 videos and their companion handbooks, covering everything about Rasa, from getting started to deploying to a Kubernetes cluster.
- The Rasa YouTube channel - with videos, live-coding, and other great content for learning
- Rasa docs on deploying to Twilio