I. Introduction

Remember Last time you visit any crowded place, like train station, and encountered some (very) long Queues. It's tiring... Can we do something to solve this kind of problems ? Of course we can.

One can think of some online solutions: building websites, mobile apps... so people can get what they're looking for by a single click! But most of the cases, the UX/UI is not that optimal (to not say it sucks).

Bad UX/UI will make getting the information complicated, hard to navigate, difficult to search, and therefore not something that the users would like to explore.

Now, how about chatbots. Making a chatbot or virtual assistance for this long queue can not only solve problem but transform the user experience. Nowadays, chatbots are gaining attraction, big or small entities such as IBM, Google, Facebook… are working on it and building their in-house products.

In this blog, we will learn how to build a Rasa chatbot, and deploy it to slack.

II. Table of contents

  1. Why should you use Rasa for Building Chatbots?
  2. Anatomy of our chatbot
  3. Extracting User Intent from a Message
  4. Making Interactive Conversations
  5. Talking to our Chatbot
  6. Calling external API
  7. Integrating Rasa chatbot and Slack

1. Why should you use Rasa for Building Chatbots?

Rasa is a set of open-source NLP tools focused on chatbots. In fact, it’s one of the most effective and time efficient tools to build chatbots in minutes. Below are three reasons why using Rasa:

  1. It lets you focus on improving the “Chatbot” part of your project by providing readymade code for other background tasks like deploying, creating servers, etc.
  2. The default set up of Rasa works really well right out of the box for intent extraction and dialogue management, even with lesser data
  3. Rasa is open-source, which means we know exactly what is happening behind the scenes and can customize things very well.

Rasa comes up with 2 components :

Rasa NLU – a library for natural language understanding (NLU) which does the classification of intent and extract the entity from the user input.

Rasa CORE – a chatbot framework with machine learning-based dialogue management which takes the structured input from the NLU and predicts the next best action using a probabilistic model like LSTM neural network.

2. Anatomy of our chatbot

Let’s understand how our Rasa powered chatbot will work before we get into the coding part :


  • When Rasa receives a message from the user, it predicts or extracts the “intent” and “entities” present in the message. This part is handled by Rasa NLU (mentioned previously)
  • Rasa then tries to predict the next action (what it should do next). This decision is taken considering multiple factors. This part is handled by Rasa Core
  • Once the user’s intent is identified, Rasa performs an action called action_request_certificate to get the request the certif in quesion.

2.1 Installing Rasa and it’s dependencies

During the installation of the libraries, multiple issues might occur depending on your operating system (Windows, Mac, Linux) compatibility settings and default system configuration. For Example: C++ Compiler issue, NumPy version issue etc. It is good practice to google it out and find resolution so that complete installation of libraries is done.

*We consider that you have already installed tenserflow & keras

Use the code below to install all the dependencies of Rasa:

pip install rasa

You will also need to install a spaCy English language model:

python -m spacy download en

Create a sample Rasa project in your current directory including some training data and configuration files.

*The command also automatically trains your first model using this data and invites you to speak to the trained chatbot.

rasa init

You’ll have a folder like this:

3. Extracting user intent from a message

The first thing we want to do is to figure out the intent of the user. What does he/she want to accomplish ? to do so, we will make use of RASA NLU to identify user intent and it entities.

Back to the folder you donwloaded earlier :

Data/ – This is the file where you will save your training data for extracting the user intent. There is some data already present in the file:

As you can see, the format of training data for ‘intent’ is quite simple in Rasa. You just have to:

  • Start the line with “## intent:intent_name”
  • Supply all the examples in the following lines

Now let’s write some intent examples for the scenario when the user wants to get a certificate :

## intent:request_certificate
- find me a work certificate
- look for a work certificate
- i'm looking for a work certificate
- would you provide me a salary certificate
- could you provide me a leave auhorization
- i need a work certificate
- i need a salary certificate
- can i have a mission order

We can add as many examples as we want for each intent, and it will be very helpfull to add some slangs and short forms that we use while texting. We aim to make the chatbot understand the way we type text.

config.yml – configuration of NLU and Core models. In-case you are dealing with Tensorflow or Spacy, you need to define such pipeline here. To handle this file, you should know about Machine Learning and Deep Learning.

The language and pipeline keys specify how the NLU model should be built. The policies key defines the policies that the Core model will use. – Rasa Action Server – where you need to write code in Python, that mainly used to trigger External actions like Calling Google API or REST API etc.

3.1 Training the classifier

We can train the classifier by simply following the command below:

rasa train

3.1 Predicting the intent

Let’s test how good is our model :

>>> from rasa.nlu.model import Interpreter
>>> nlu_model = Interpreter.load("C://Users//user//Desktop//xtalent-chatbot//models//nlu")
>>> nlu_model.parse('i need a work certificate')

Here is what the output looks like :

        'name': 'request_certificate', 
        'confidence': 0.9702790975570679
                'start': 9,
                'end': 25,
                'value': 'work certificate',
                'entity': 'certif_type',
                'confidence': 0.9841607878444993,
                'extractor': 'CRFEntityExtractor'
                'name': 'request_certificate',
                'confidence': 0.9702790975570679
                'name': 'bot_challenge',
                'confidence': 0.007967472076416016
                'name': 'affirm',
                'confidence': 0.004922408144921064
                'name': 'greet',
                'confidence': 0.004032162018120289
                'name': 'inform',
                'confidence': 0.003218600060790777
                'name': 'request_unknown_certificate',
                'confidence': 0.0029319943860173225
                'name': 'deny', 
                'confidence': 0.0028675978537648916
                'name': 'goodbye',
                'confidence': 0.0023520870599895716
                'name': 'thankyou', 
                'confidence': 0.00119037798140198},
                'name': 'stop', 
                'confidence': 0.00023829615383874625
    'text': 'i need a work certificate'

Not that NLU model perform well on intent extraction, and it also ranks the other intents based on their confidence scores. This is a usefull feature when the classifier is confused between multiple intents.

4. Making Interactive Conversations

Being interactive is one of the most important aspects of a chatbot. Our main goal focuses on if the chatbot is able to hold a conversation. The chatbot is expected to extract all the needed information to perform a particular task.

4.1 Designing the conversation flow

Let’s think of the simplest conversation our chatbot can have with a user. What would be the flow of such a conversation? Let’s write it in the form of a story!

Me : hi
Bot : Hello! I am certificate search assistant! How can I help?
Me : i need a work certificate
Bot : how many copy you want?
Me : 2
Bot : in which language you want it (FR or EN)?
Me : fr
Bot : your request will be under review following the parameters:
      Certificate : work certificate
      Copies : 2
      Language : fr
    Did that help you? 
Me : yes 
Bot : glad that i helped you :)
Me : Thank you !
Bot : no worries
Me : goodbye
Bot : goodbye !

Let’s see how to do such a simple conversation with Rasa:

The general format is :

## certif path 1              <--- story name for debugging purposes
* greet                       <--- intent detected from the user
- utter_greet                 <--- what action the bot should take
* request_certificate         <--- the following intent in the conversation

This is called a user story path and it is the training data for Rasa Core. The way it works is:

  • Give some examples of sample story paths that the user is expected to follow
  • Rasa Core combines them randomly to create more complex user paths
  • It then builds a probabilistic model out of that. This model is used to predict the next action Rasa should take

4.2 Defining the domain file

The domain is the world of the chatbot. It contains everything the chatbot should know, including:

  • All the actions it is capable of doing
  • The intents it should understand
  • he template of all the utterances it should tell the user, and much more.

5. Talking to your chatbot

Open a new terminal and type the following command:

rasa shell

6. Calling external API (REST API)

In this section, we will write the backend code for our project. In this example, we are using an internal API for requesting certificates (You can use any other API regarding to your context).

Our will be :

Fill in the API_URL, API_TOKEN, API_ENDPOINT with the one you have (if you have any) and you should be good to go. But first, you have to start your action server :

rasa run actions

This will start the server that is running on the file and will be working in the background. Now it will give us access to the certificate API and request the certificate in question for us. Isn’t awesome ?

7. Integrating Rasa chatbot and Slack

7.1 Slack integration

Now we need an application to attach our bot like Messenger, Slack, etc. In This work we will focus on Slack, create the app on the below link:

1. Click on “Create App”, give a name to the app, and select your workspace:

This will redirect you to your app dashboard. From there, you can select the “Bots” option:

2. Click “Add a Bot User” –> Give a name to your bot. In my case, I have named it “xTalentbot”. Now, we need to add it to our workspace so we can chat with it!

3. Go to the app dashboard, find section OAuth & Permissions and click on “Install App to Workspace” option:

Once doing that, Slack will ask to “authorize” the application. Go ahead and accept the authorization.

4. Before we connect any external program to our Slack bot, we need to have an “auth token” that we need to provide when trying to connect with it. Go back to the “app dashboard” and select the “OAuth & Permissions” option. This will open the permissions settings of the app. Select the “Bot User OAuth Access Token” and save it (I have hidden them for security reasons). This token is vital in connecting to our chatbot.

7.2 Installing Ngrok

We are going to use another useful tool to deploy our chatbot to Slack. That’s ngrok and you can use the following link to download it:

7.3 Pushing the chatbot to Slack

All we need is few commands to get this done as Rasa takes care of everything else behind the scenes.

  • Create a yml file slack_credentials.yml in your project directory and paste the “Bot User OAuth Access Token” in place of the Slack token:

  • Open new terminal and start the action server:
rasa run actions

You’ll see that the server is running on port 5055. Now stop the server, and go to endpoints.yml file and paste these lines (they already exist as comments, just remove the #) :

Run the Rasa server using the following command:

rasa run --port 5002 
         --connector slack 
         --credentials slack_credentials.yml 
         --endpoints endpoints.yml 
         --log-file out.log 
         --cors * 

You will get a message like this :

Notice that Rasa server is running at port 5002 so let’s use ngrok on this port.

  • Now deploy port 5002 to the internet:
ngrok http 5002

This will give an output like the below image:

  • Go to your app dashboard on Slack, click on Events Subscription, and then on the “Enable Event Subscriptions” button. Paste the ngrok URL of your Rasa server in this format under the Request URL field:

In the above URL, replace the ngrok part with your ngrok URL:

  • Under the Subscribe to Bot Events, click on the Add Bot User Event button. It will reveal a text field and a list of events. You can enter terms into this field to search for events you want your bot to respond to. Here’s a list of events that I suggest adding:

Once you’ve added the events, click the Save Changes button at the bottom of the screen.

Now you can just refresh your Slack page and start chatting right away with your bot! Here’s a conversation with my chatbot:

In my next blog i will talk about how to add interactive components (like buttons, date picker…) with slack shown in the below conversation :