Chatbot using RASA Framework

Yeshwant Kumar
13 min readApr 28, 2021

Here I would build a basic chatbot using Rasa Framework and integrate it in Telegram.

Chatbot using Rasa

Buiding chatbots and AI assistants has become a great necessity for today’s world.Due to increase in demands and necessities of people,It would be very hactic to schedule and counsel each and everyone and interact with them even inorder to get the knowledge of their basic necessities. So inorder to meet this problem, Engineers have come up with a great idea using Machine Learning and Artificial intelligence to automate this task of interaction and build virtual assistants and chatbots to solve this problem and answer most of the queries with utmost efficiency and accuracy.So now we will try to build a similar chatbot that can interact directly with people and solve their queries.

Table Of Content:

  1. Introduction to chatbot
    1.1. What is chatbot ?
    1.2. Advantages and uses of chatbots
    1.3. Prerequisites
  2. Introduction to RASA
    2.1. Basic introduction
    2.2. Architecture of RASA
    2.3. Installation of RASA
  3. The Business problem statement
  4. Implementing the solution
    4.1. Creating custom intents
    4.2. Creating custom stories
    4.3. Testing the bot through UI
  5. Deploying the bot
    5.1. Integration of the bot in Telegram
  6. Conclusion

1. Introduction to chatbot

1.1. What is chatbot ?
A chatbot is an application that can initiate a conversation and can respond to user queries as a human would do. A chatbot can be of 2 types-
The first one is the rule based engine, It is a simple if-else construct which is easier to build but it can also be used by technical people because it is not much user friendly and the efficiency of this type of chatbot depend on the skill of the developer who developed this application.
The second type of chatbot is a chatbot with Natural Language Understanding.These are more common and they can be used by end users, At core they have a natural language understanding engine using which the intents of the customer conversation are identified and then the bot will respond accordingly based on the intention of the customer or end user.

1.2. Advantages and uses of chatbot
1. A chatbot ensures high availability, It means the chatbot is always available for the customer to answer their queries.
2. It is easy to build and deploy and is cost-effective.
3.It provides uniform customer experience to each and every person despite of their geographical location.

USES:
1· Appointment Booking
2. Address a query
3· Addressing Grievances
4· Automation of routine tasks
5· Customer support
6· Frequently Asked Questions

1.3. Prerequisites
1· Python installed
2. Microsoft build tools with visual C++ 14.0 installed.

2. Introduction to RASA

2.1. Basic Introduction
RASA is an open-source framework to build high performing chatbots and one of the major advantages of RASA are, It understands the messages and it tries to hold meaningful conversations,It tries to use Machine Learning and Deep Learning in background to improve the conversation experience. And here we are going to use RASA X over RASA opensource as a wrapper because RASA X will give us graphical user interface to test our chatbot and it can improve assistant based learning and can be updated easily.
Rasa has two main modules:
1.NLU for understanding user messages
2.Core for holding conversations and deciding what to do next

2.2. Architecture of RASA

Architecture of RASA

The architecture looks simple as above, Now we will try to understand in detail.
Message In: This is what the user is saying,simply it is the input message.
Interpreter: The message is received here.It tries to extract the intents and entities from the message received.
Tracker: It holds the current state of the conversation,it tells us at what state our chatbot currently is.
Policy: This will identify what is the next action to be taken.The action to be taken after the current state of the chatbot.
Action: After this action is identified this will again be updated again in the tracker so that the tracker also knows that the old state has been replaced by new state.
Message Out: Here we get the message out to the user.

2.3. Installation of RASA

1.The first and foremost condition is, we should have python and visual
c++ 14.0 installed.
2.Create a new folder for chatbot project and open it using pycharm
3.Create a new_env for the chatbot
4.Now install rasa-x and spacy by using the command

pip install rasa-x
pip install spacy

5.Then type the following commands

# To download for english language('en_core_web_sm')
python -m spacy download en
# To download library for spacy
python -m spacy download en_core_web_md
# To link the spacy with the english keywords
python -m spacy link en_core_web_md en

Here we used rasa-x over rasa because it provides us GUI for better interaction.Even we installed spacy as rasa-x internally uses spacy for text preprocessing.
After all these commands run successfully try to run the command rasa init and subsequently type yes for all the permisssion grants.Then run the command rasa shell where now we can write our inputs.
Now when we give our input as a message, it interacts and gives us an output.

Your input ->  hi
Hey! How are you?
Your input -> good
Great, carry on!

Now you may have a doubt how this is working and where from are the output is coming.So all these messages are found in nlu.md file which has all the intents mentioned previously.
So all this get identified from the user utterences

So here hey,hello, hi etc are user utterences or input messages and when the user gives them as input it takes its base intent as greet and try to map it.
Now that is ok that we understood how is the input being received, But you can also see that we are getting output as “Hey! How are you?”, so now the question is where from this is coming from. All theseare defined in the file named domain.yml where we have all the intents and their responses defined.
Now we need to look the order how it receives the input and gives away the related output.This is defined in “stories.md” file which has the order defined.
Now we will look into the simple flow how it works:

Here let us say a user has given an input as “Hi”. From this query the intent is being identified.
Then for this input message, We have a follow up statement as “Hey! How are you?”. This is defined in domain.yml . Now we need to consecutive order how these follow up.This is defined in the stories.yml. Now if you give reply as “sad”. It again gives the followed output.

the flow is similar for each input

So this is the way how the conversation takes place.

3. The Business problem statement

As the corona pandemic has taken control over the world and there are many false news and allegations on the pandemic, So inorder to provide basic but correct and accurate news mostly, we would build a chatbot to handle this problem. So our main goal here is to build a chatbot which can answer queries related to the COVID-19 disease. Here we are going to collect different questions and answers related to corona virus and we will compile them together and put it in our chatbot.So here I have taken questions related to corona virus from the “cdc website”.This is not only the single source, we can collect the data from ministry of human affairs and WHO and even many other websites.
Finally we are going to build a Q & A botwhich could answer the user queries about the frequently asked questions related to corona virus epidemic.

4.Implementation of the Solution

Until now we have seen basically how the RASA works but now we need to give our custom queries which are going to be used by user to question or ask something about the corona disease.

First we will start from nlu.yml-
It contains the intents and user utterences that is what the user is going to say.
So now we will start giving few intents and user_utterences related to corona. They would basically be like:

version: "2.0"nlu:
- intent: greet
examples: |
- hey
- hello
- hi
- hello there
- good morning
- good evening
- moin
- hey there
- let's go
- hey dude
- goodmorning
- goodevening
- good afternoon
- intent: goodbye
examples: |
- good afternoon
- cu
- good by
- cee you later
- good night
- bye
- goodbye
- have a nice day
- see you around
- bye bye
- see you later
- intent: affirm
examples: |
- yes
- y
- indeed
- of course
- that sounds good
- correct
- intent: deny
examples: |
- no
- n
- never
- I don't think so
- don't like that
- no way
- not really
- intent: mood_great
examples: |
- perfect
- great
- amazing
- feeling like a king
- wonderful
- I am feeling very good
- I am great
- I am amazing
- I am going to save the world
- super stoked
- extremely good
- so so perfect
- so good
- so perfect
- intent: mood_unhappy
examples: |
- my day was horrible
- I am sad
- I don't feel very well
- I am disappointed
- super sad
- I'm so sad
- sad
- very sad
- unhappy
- not good
- not very good
- extremly sad
- so saad
- so sad
- intent: bot_challenge
examples: |
- are you a bot?
- are you a human?
- am I talking to a bot?
- am I talking to a human?
- intent: corona_intro
examples: |
- What is corona virus
- what is covid
- what is a novel corona virus
- what is covid-19
- tell me about corona
- can you tell me about covid
- intent: corona_spread
examples: |
- how does corona virus spread
- how does the virus spread
- intent: corona_food_spread
examples: |
- Does corona spread from food
- how will corona spread from food
- intent: warm_weather
examples: |
- will warm weather stop the spread
- will it stop with warm weather
- intent: high_risk
examples: |
- who is at a higher risk of infection

Now as we have marked the changes in our “nlu.yml” file so we will modify our “stories.yml ”file which will help us to control the conversational flow.It will be like :

version: "2.0"stories:- story: happy path
steps:
- intent: greet
- action: utter_greet
- intent: mood_great
- action: utter_happy
- story: sad path 1
steps:
- intent: greet
- action: utter_greet
- intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- intent: affirm
- action: utter_happy
- story: sad path 2
steps:
- intent: greet
- action: utter_greet
- intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- intent: deny
- action: utter_goodbye
- story: what is corona
steps:
- intent: corona_intro
- action: utter_corona_intro
- story: how does corona spread
steps:
- intent: corona_spread
- action: utter_corona_spread
- story: corona warm weather
steps:
- intent: warm_weather
- action: utter_warm_weather
- story: corona food spread
steps:
- intent: corona_food_spread
- action: utter_corona_food_spread
- story: corona high risk
steps:
- intent: high_risk
- action: utter_high_riskNow we will define all our utterences in the “domain.yml” file.So that they can easily be mapped from their intents and user utterances.
version: "2.0"intents:
- greet
- goodbye
- affirm
- deny
- mood_great
- mood_unhappy
- bot_challenge
- corona_intro
- corona_spread
- corona_food_spread
- warm_weather
- high_risk
responses:
utter_greet:
- text: "Hey! How are you?"
utter_cheer_up:
- text: "Here is something to cheer you up:"
image: "https://i.imgur.com/nGF1K8f.jpg"
utter_did_that_help:
- text: "Did that help you?"
utter_happy:
- text: "Great, carry on!"
utter_goodbye:
- text: "Bye"
utter_iamabot:
- text: "I am a bot, powered by Rasa."
utter_corona_intro:
- text:"Coronaviruses are a group of related viruses that cause diseases in mammals
and birds. In humans, coronaviruses cause respiratory tract infections that
can be mild, such as some cases of the common cold (among other possible causes,
predominantly rhinoviruses), and others that can be lethal, such as SARS, MERS,
and COVID-19"
utter_corona_spread:
- text: "This virus was first detected in Wuhan City, Hubei Province, China. The\
\ first infections were linked to a live animal market, but the virus is now\
\ spreading from person-to-person. It’s important to note that person-to-person\
\ spread can happen on a continuum. Some viruses are highly contagious (like\
\ measles), while other viruses are less so. The virus that causes COVID-19\
\ is spreading from person-to-person. Someone who is actively sick with COVID-19\
\ can spread the illness to others. That is why we recommend that these patients\
\ be isolated either in the hospital or at home (depending on how sick they\
\ are) until they are better and no longer pose a risk of infecting others.\n\
How long someone is actively sick can vary so the decision on when to release\
\ someone from isolation is made on a case-by-case basis in consultation with\
\ doctors, infection prevention and control experts, and public health officials\
\ and involves considering specifics of each situation including disease severity,\
\ illness signs and symptoms, and results of laboratory testing for that patient.\n\
The virus that causes COVID-19 seems to be spreading easily and sustainably\
\ in the community (“community spread”) in some affected geographic areas. Community\
\ spread means people have been infected with the virus in an area, including\
\ some who are not sure how or where they became infected."
utter_corona_food_spread:
- text: "Coronaviruses are generally thought to be spread from person-to-person through
respiratory droplets. Currently there is no evidence to support transmission
of COVID-19 associated with food. Before preparing or eating food it is important
to always wash your hands with soap and water for 20 seconds for general food
safety. Throughout the day wash your hands after blowing your nose, coughing
or sneezing, or going to the bathroom."
utter_warm_weather:
- text: "It is not yet known whether weather and temperature impact the spread of
COVID-19. Some other viruses, like the common cold and flu, spread more during
cold weather months but that does not mean it is impossible to become sick with
these viruses during other months. At this time, it is not known whether the
spread of COVID-19 will decrease when weather becomes warmer. There is much
more to learn about the transmissibility, severity, and other features associated
with COVID-19 and investigations are ongoing."
utter_high_risk:
- text: "Older adults and people of any age who have serious underlying medical conditions
may be at higher risk for more serious complications from COVID-19. These people
who may be at higher risk of getting very sick from this illness, includes;
Older adults, People who have serious underlying medical conditions like...
Heart disease, Diabetes, Lung disease,"
actions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbye
- utter_iamabot
- utter_corona_intro
- utter_corona_spread
- utter_corona_food_spread
- utter_warm_weather
- utter_high_risk
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true

So now we have modified all our necessary files. So now we need to train on our data. To do this the command is very simple:

rasa train

We are doing this to completely train our model with the new intents and utterences and their mappings.And now after training this this model gets stored in the models folder and it gives the path to the models folder where the model is stored.

Now as we trained so now we need to test our model, This can be done using the command rasa x.
If there is an error regarding rowproxy like

from sqlalchemy.engine import reflection, RowProxy, Connection ImportError: cannot import name ‘RowProxy’

then try to install the version SQLAlchemy==1.3.22 using the command and the model would work absolutely fine and it will open an interface in the web browser for rasa x.

pip install SQLAlchemy==1.3.22

Here you can start querying the chatbot and it will give you accurate answers.

interactive-bot

And to use this interactive GUI interface we used rasa x over rasa.

5. Deploying the bot

Telegram Integration

Now we have our application successfully running locally but we need to have it access publicly so that it can be accessed from anywhere.So inorder to do this we can use ngrok that help us to integrate this publicly.

  1. First download the ngrok.
  2. Extract the zip file and then open the file ngrok.exe, and type “ngrok http 80 ”(or other port number as shown for you) then it will appear as

So now it will mask the local host to public url.

Now we need to create a telegram bot as well.

1.First open the telegram in mobile or PC.
2.Then open botfather of telegram.
3.create a new bot and give its name as you wish and Give a username to your bot, which must end in _bot.
4.This generates a access token.

So now inorder to use this token open the credentials.yml file and write it as,

telegram:access_token: "490161424:AAGlRxinBRtKGb21_rlOEMtDFZMXBl6EC0o"verify: "your_bot"webhook_url: "https://your_url.com/webhooks/telegram/webhook"

then run the commmad

rasa run

This will run the bot in the telegram and it would appear as:

Now even if you want to use it in other platforms like slack,cisco webex,facebook etc, the simple documentation is given here:
https://rasa.com/docs/rasa/messaging-and-voice-channels/”.

6. Conclusion

Here we have simply built a chatbot and integrated it in telegram.Only the prerequisite is python and visual C++ 14.0. And then we got our rasa-x and spacy installed. Then we embedded with our english language and linked it to our spacy so that it would help in text-preprocessing.then we use rasa init and try to custom change our domain,credentials,stories and nlu files.Then we try to install ngrok and run it at port 5005.and run it using rasa run command.

References:

You can get the code from my github :

To download ngrok:

My linkedin profile:

--

--