The Rasa Masterclass is a weekly video series that takes viewers through the process of building an AI assistant, all the way from idea to production. Hosted by Head of Developer Relations Justina Petraityte, each episode focuses on a key concept of building sophisticated AI assistants with Rasa and applies those learnings to a hands-on project. At the end of the series, viewers will have built a fully-functioning AI assistant that can locate medical facilities in US cities.
To supplement the video content, we'll be releasing handbooks to summarize each episode. You can follow along as you watch to reinforce your understanding, or you can use them as a quick reference. We'll also include links to additional resources you can use to help you along your journey.
Introduction
In the previous episode of the Rasa Masterclass, we learned how to share the medicare locator assistant with test users. Sharing the assistant with test users is an important middle step for QA, allowing you to build up the training data set with realistic dialogues and review collected conversations for actionable insights.
In this episode, we'll go one step further by making the assistant available to real users on a publicly accessible messaging channel. This is an exciting milestone-our assistant will finally be live in production.
We'll connect the medicare locator assistant to two channels: Telegram and a website chat widget. Let's begin!
Configuring DNS and SSL
Before we can connect our assistant to messaging channels, we need to do some additional setup on the Rasa X server. As you recall, when we deployed Rasa X, we were able to access Rasa X in the browser by going to an IP address with an http protocol, like http://34.62.185.11/. That was fine while we were in early development, but in order to securely connect to external applications-our messaging channels-we need to configure a domain name and SSL. After completing these steps, the Rasa X server will be accessible at a secure, human-friendly URL like https://rasamasterclass.com.
First, we'll assign a domain name to the server instance. Then, we can configure an SSL certificate, bound to the domain.
Assigning a static IP address
If you're following along with this tutorial and using Google Cloud Platform as your host, you can find the IP address for your VM instance in your GCP Console under Compute Engine>VM Instances.
Before you point your domain to this address, you'll want to complete one additional step. By default, the external IP address assigned to a GCP VM instance is ephemeral, meaning that a new IP address is assigned each time the server is stopped or started. This would break the connection with the domain and require you to update your DNS records to point to the new IP.
To prevent this, you can promote your external IP address from ephemeral to static. Select your VM instance and click Edit. Then, scroll down to the Network interfaces settings. In the External IP dropdown, select Create IP address.
Enter a name, and click Reserve. This IP address will now be associated with your server until it is explicitly removed.
Pointing to a domain
You can purchase the domain name for your Rasa X instance from any registrar you would like; for example, Google domains. Once you've registered the domain, you can apply it to your Rasa X instance by creating an A record that maps the domain to the external IP address for your server. Be sure to check your domain registrar's documentation for specific instructions on creating DNS records.
After updating DNS records, be sure to allow time for them to propagate. Test the new configuration by visiting http://yourdomain.com in the browser.
Installing an SSL certificate
An SSL certificate creates a secure connection between the browser and the server. It verifies that the requested hostname matches the name registered on the certificate.
To secure your setup, you can either purchase a certificate from a certificate authority or you can use a free option like Let's Encrypt. In this tutorial, we'll use certbot: a free, open source tool that makes it easy to install an SSL certificate from Let's Encrypt.
Begin by establishing an SSH connection to your VM instance. In the terminal, stop the docker container:
sudo docker-compose down
Run the following command to install certbot:
sudo apt-get install certbot
Start the installation:
sudo certbot certonly
Certbot asks a series of questions to guide you through the process of configuring the SSL. First, you'll be asked how you would like to authenticate with the ACME CA. Choose option 1, Spin up a temporary webserver.
When prompted, provide a valid email address, accept the terms and conditions, and finally, enter the domain name you applied to the Rasa X server.
When the process has been completed successfully, you'll see the following message in your terminal:
By default, the certificate is saved to a /letsencrypt/ directory. We'll need to move the files to the /etc/rasa/ directory to make them accessible to the docker container.
sudo cp /etc/letsencrypt/live/<rasa.example.com>/privkey.pem /etc/rasa/certs/
sudo cp /etc/letsencrypt/live/<rasa.example.com>/fullchain.pem /etc/rasa/certs/
Re-start the docker container to put the updates into effect:
sudo docker-compose up -d
Test the changes by navigating to https://yourdomain.com. You should now see a secure lock symbol in the address bar.
Note:
Let's Encrypt certificates are valid for 90 days. You can renew the certificate with the following command (either manually or through an automated cron job):
sudo certbot renew
Connecting to Messaging Channels
Rasa comes pre-configured to connect with popular messaging channels like Slack, Facebook Messenger, Telegram, and many more. You can also connect to a website chat widget, SMS, or integrate your own custom channel.
In this episode, we demonstrate how to connect the medicare locator assistant to two channels: Telegram and a website chat widget. The configuration process is similar across different channels, but you can find step-by-step instructions for each channel in the documentation.
Telegram
Telegram is a popular free messaging app that syncs messages across a number of different devices-smartphones, tablets or laptops. It supports third party integrations like AI assistants, so it's a great platform to build on.
Telegram provides an assistant called BotFather that helps developers register a new bot integration. Use the /newbot command to get started. When prompted, provide a name and username for your assistant. The name is displayed in contact details. The username is a short name used in mentions and must end in bot
. When the assistant's profile is complete, you'll be given an API token.
Now that we have the Telegram API token, we can connect our Rasa assistant. Head back to your server and open a terminal session.
The credentials.yml file holds the configuration details for connecting to external applications, like messaging channels. This file is included automatically when a starter project is created with the rasa init
command.
From the /etc/rasa directory, open the credentials.yml file:
sudo nano credentials.yml
Edit the contents of the file to include the following details:
telegram:
access_token: "490161424:AAGlRxinBRtKGb21_rlOEMtDFZMXBl6EC0o"
verify: "your_bot"
webhook_url: "https://your_url.com/core/webhooks/telegram/webhook"
Here, we're providing the channel name (telegram) and the access_token, which we obtained from Telegram's BotFather assistant. In the verify field, provide the username you registered with BotFather. Be sure to replace the your_url.com placeholder in the webhook_url with the domain of your Rasa X server.
Save the changes and then restart the docker container:
sudo docker-compose restart
We can now chat with the medicare locator assistant on Telegram:
Webchat
To connect your assistant to a website, you'll need a plugin to create the chat widget-a chat interface where users can talk to your assistant. We'll use an open source option called Rasa WebChat. It uses SocketIO to send and receive messages in real time.
If you don't have a website already, you can try out the WebChat widget with a simple hello world example-a basic HTML document.
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Much like we did when setting up Telegram, we'll need to add configuration details for the SocketIO channel to the credentials.yml file. Open the credentials.yml file in the editor and add the following:
socketio:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: true
Here, user_message_evt
and bot_message_evt
define the event names used by the Rasa dialogue management engine when sending or receiving messages over SocketIO. The third configuration, session_persistance
, determines how SocketIO should handle session management. By default, the session restarts on every page reload. By setting this parameter to "true" you can persist the session until the browser or tab is closed, or until a specific event is called to clear the session storage.
Once the changes have been saved to credentials.yml, you can embed the widget by adding a snippet of HTML between the tags of your website:
<div id="webchat"/>
<script src="https://storage.googleapis.com/mrbot-cdn/webchat-latest.js"></script>
// Or you can replace latest with a specific version
<script>
WebChat.default.init({
selector: "#webchat",
initPayload: "/get_started",
customData: {"language": "en"}, // arbitrary custom data. Stay minimal as this will be added to the socket
socketUrl: "http://localhost:5500",
socketPath: "/socket.io/",
title: "Title",
subtitle: "Subtitle",
})
</script>
There are three values you'll want to change in the configuration object:
Test the new channel by opening the web page in a browser and chatting with the assistant.
Working with Messaging Channels
A single Rasa assistant can be connected to multiple channels-in fact, you may find it easier to configure the connections for multiple assistants at once in your credentials.yml file rather than connecting one at a time.
Conversations users have with the assistant-across all connected channels-are collected in Rasa X for review. In the Rasa X Conversations screen, each conversation is labeled with the channel it originated from.
Another useful feature when working with multiple channels is the ability to specify channel-specific utterances. Different messaging channels may come with different UI messages you can use, or you may want the assistant to use different responses depending on the channel. To use channel-specific utterances, add the channel key to your response template in domain.yml:
responses:
utter_ask_game:
- text: "Which game would you like to play?"
channel: "slack"
custom:
- # payload for Slack dropdown menu to choose a game
- text: "Which game would you like to play?"
buttons:
- title: "Chess"
payload: '/inform{"game": "chess"}'
- title: "Checkers"
payload: '/inform{"game": "checkers"}'
- title: "Fortnite"
payload: '/inform{"game": "fortnite"}'
Conclusion
In this episode of the Rasa Masterclass, our assistant finally made the leap from the development and testing stage to interacting with the outside world. Our assistant can now have conversations with real users on real messaging platforms.
Not only is this a gratifying step, it's also crucial for the continued improvement of the assistant. Conversations with real users provide valuable insights and training data. As you review conversations in Rasa X, you should use these conversations to add new training examples and determine the direction of future development. For example, you may decide to add a new intent or action, make adjustments to machine learning algorithms-or even change the scope of the assistant's domain.
As a next step, try connecting your assistant to one of the messaging channels listed in the documentation, and spend some time analyzing your assistant's interactions in Rasa X. When you're ready, meet us back here for the next episode of the Rasa Masterclass.
Additional Resources
- Episode 11 - Connecting an assistant to external messaging channels (YouTube)
- Messaging and Voice Channels (Rasa docs)
- Channel-specific Responses (Rasa docs)
- IP Addresses (Google Cloud docs)
- DNS Basics (Google support)
- Certbot (Certbot website)