Building a Conversational Chatbot for Slack using Rasa and Python -Part 2 | by Parul Pandey | Towards Data Science

Note: This article was written long ago and there may be changes in RASA which haven’t been incorporated into the article. It is advised to go to their official documentation site for the latest details.
“Slack went down again, so the colleagues had to talk to each other”.
When slack went down momentarily during mid-2018, the whole tech world had descended into chaos and mayhem. Internet was flooded with discussions on slack and #Slack was trending on Twitter. Well, slack has already become an integral part of a lot of companies and people use it a lot to communicate and discuss things. Some companies which work remotely use slack all the time for real-time communication.
This is the concluding part of the article: Building a Conversational Chatbot for Slack using Rasa and Python -Part 1. In the first part, we discussed in detail about Rasa Stack: an open source machine learning toolkit that lets developers expand bots beyond answering simple questions. We then used two modules of Rasa namely Rasa NLU and Rasa Core to build a fully functional chatbot capable of checking in on people’s mood and take the necessary actions to cheer them up. The actions included showing the users an image of a dog, cat or bird depending upon the user’s choice.
In this article, we will utilize all the generated files to deploy the Bot on slack. I highly recommend that you read Part 1 before attempting Part 2.
Unlike the previous part, we will have to install the latest version of Rasa Core. It is highly recommended that you create a virtual environment and then proceed with the installations
#creating a virtual environmentconda create --name bot python=3.6#Activate the new environment to use itWINDOWS: activate botLINUX, macOS: source activate bot#Install latest Rasa stack#Rasa NLUpython -m pip install rasa_nlu[spacy] (https://rasa.com/docs/nlu/installation/)#Rasa Corepython -m pip install -U rasa_core (https://rasa.com/docs/core/installation/)#Language Modelpython -m spacy download en_core_web_mdpython -m spacy link en_core_web_md en --force;
Clone my GitHub repository.
The repository contains the files generated in Part 1. However, due to some changes in Rasa API w.r.t versions, there are some changes in the functions and commands.
Let’s us create a slack integration by creating a slack app. Let’s go through the process of creating a Slack app.
Create a Slack account and go to https://api.slack.com/. Now choose either an existing development Slack workplace(in case you have one) or create a new one. Name it DemoWorkplace or anything you like.
Now create a Slack app in DemoWorkplace and give it a name. Let’s call our app Robo.
Start adding features and functionalities to the app. We’ll first create a bot user under the Bots Tab and integrate it into the app. This will make the app conversational. Since this bot will be created as a user, we can choose to keep it constantly online. Save all the changes made.
To make sure that the bot has been integrated, navigate to the Add Features and Functionality under Basic Information Taband make sure that the Bots and Permissions Tabs are active. Our bot has been integrated into the app.
Next, we can add a lit bit more info about our bot including a picture, a description and a background colour. Scroll to Display Information and add in the information.
Save all changes as you go.
Lastly, we need to install this app into our workplace which we defined earlier. Authorize it and we have our app ready and integrated into the workplace.
The process has been summarised in the gif below.
Ngrok is a multi-platform tunnelling, reverse proxy software that establishes secure tunnels from a public endpoint such as the internet to a locally running network service. In simple words it means, it opens access to your local app from the internet.
Download ngrok from here. make sure you Login first.
Unzip to install via $ unzip /path/to/ngrok.zip
Connect your account via $ ./ngrok <authtoken>
Navigate to the directory where you unzipped ngrok and type $ ngrok <authtoken> in the console. The token can be accessed from here.
Fire it up.
Start it by telling it which port we want to expose to the public internet : ./ngrok http 5004
If all goes well you should see the following screen:
Here http://-------.ngrok.io is the ngrok url.
Create a Python Script
Since we are done with all the requirements, it’s time to deploy our bot. For this, we will need to write a Python script called run_app.py, which will integrate our chatbot with the slack app that we created above. We will begin by creating a slack connector for our Rasa chatbot. We will use RasaNLU interpreter to load the NLU model directly from the python script.
We will train our model again to make sure everything is good and running.
python nlu_model.py
The actions file that we created in Part 1, now needs to be run on a separate server. This is a change in the latest version of Rasa Core. Read the documentation for more details.
Start the custom action server
python -m rasa_core_sdk.endpoint --actions actions
Open a new terminal and train the Rasa Core model
python dialogue_management_model.py
Start the Agent by running run_app.py file . Make sure to provide the slack token in the script. The slack token can be obtained as follows.
Start the ngrok on port 5004 and grab your ngrok_url.
Provide the url: https://<your_ngrok_url>/webhooks/slack/webhook to ‘Event Subscriptions’ page of the Slack configuration. Wait for it to be verified.
Lastly, subscribe to some Workplace events like :
app_mention so that our bot responds when someone mentions it by name
message_im which allows the user to send direct messages to the bot.
Make sure to save all the changes as you go
Ensure the custom actions server is runningEnsure ngrok is running on port 5004Navigate to Slack interface and talk to your bot
This might sound like a herculean task but if you follow it step wise step, you will finally be able to create a working slackbot called Robo in no time. You will be able to chat with your bot, like the way I am able to:
This was a pretty comprehensive tutorial but the fact that we were able to build a fully function Slackbot makes all the hard work worthwhile. Rasa is a pretty useful library and you can experiment and tinker with it to create some truly useful chatbots.
Read Part 1 here
Link to Github Repository