How and Why I build a BOT for Slack in Python
The era of real artificial intelligence is coming not only exist in scientific papers or novels. Today is real I have seen effort from many universities, public and private sector to drive AI becoming a true thing. All developer around the world waken up by the new trend that shifted from Javascript era to Data science era especially, they do python as a major programming language to solve and explore the new data world.
Today, I would like to propose you the next big things even though it existed so It’s a “ ChatBot ” which make your life easier, faster, and better. So, how? how it changed my life?
Let’s go through a scenario from iCreativeSystem co., Ltd. ( Yeahh! that’s our company ). Every morning we do a stand-up meeting at 10.30 am but we don’t have a real stand-up meeting in the office because some of us is remotely working from somewhere in the world, that’s the problem so we had solved it by using the bot to manage and handle every morning stand-up meeting. It will ask everyone the questions that I had set in our database and then after each person answered the bot will post that person’s answers to the channel. This method helps a lot because we can verify any answers from everyone and I love to know that “How their feel today” “What they gonna do” or “What they’ve done” so it the really powerful method you will love it!
Now, I will tell you how I build slack bot app in Python I promise you It super easy just a few lines of code ( because of python )
Let’s start at…
Prerequisites
- Slack account and join into some workspace
- Basic Python programming
Step 1: Creating Bot app on slack
The first thing is going to this site https://api.slack.com/
At Start here section, Click on “ Build Slack apps”
then click on “ Create a Slack app” the green button in figure 2
Pop-up, popped in front of you like in figure 3, Fill in the blank for App name and your development workspace then “Create App”
After clicking on “Create App” you will redirect to the “Basic Information” below the section you will found “Bots” just click on it! ( figure 4)
The next page will ask you to Add a Bot user or not, No other way to choose just “ Add a Bot User” right now! ( figure 5 )
Now, just make your bot look cool, you can change anything on the page ( figure 6 )
After you add the bot now you will back to the “Basic Information” page then I want you to look at “Install your app to your workspace” then click on “Install App to Workspace”, the green button ( figure 7 )
Then, allow it to install on your workspace
Finally, we have installed in on our workspace then please look at the left-hand side section for “Install App” found it?
if you found it just click and you will see a page like a figure 9. please copy “Bot User OAuth Access Token” we really need to use it
*Important: keep it as a secret key don’t share to anyone even your girlfriend!
Done for Creating app on Slack!!
Step 2: Install essential packages for your BOT in Python environment
My environment is
- Python 3.6.5
- pip 10.0.1
- virtualenv 16.0.0
- Ubuntu 18.04 LTS
( everything works fine with this env )
Let’s say you already have pip installed in your system, if not get-pip
Install virtualenv by pip and create virtualenv
Now, you are on your virtualenv so we named it as .env ( dot in front filename system like Linux and Unix will treat is as a hidden file )
Install an important package it’s SlackClient API in Python
Step 3: Coding time, baby!
In this article, I will lead you to build your own simple bot to make sure that you understand every single thing I will write it down in one file.
Tada!!! ChatBot code is above~
Line by line, Explanation
line 7– 10: All packages which we have to use to build a bot the most important package is slackclient ( DO NOT FORGET IT )
line 13: Create an object from SlackClient, Remember that I told you to copy “Bot User OAuth Access Token” from the slack website we keep it in our environment variable If you are not familiar with Linux or Unix please check this one out I named my variable as SLACK_BOT_TOKEN so it stored my token
line 14: We can get bot_id by this command
line 15: I ask for all channels from slack API, so I get the list
line 16– 24: Any possible answers that bot can reply to use
line 27– 35: I create a function to find channels id from its name by search into the channel list
line 39: start the main area to run the bot
line 43: connect to slack, a with_team_state parameter is to pull state of a workspace or not if your workspace is large I recommended setting to False
line 44: print message to a console
line 45: set a channel to respond to user
line 46 - 50: you can send your message by calling this API
line 51: An infinite loop, the loop always check any response from users
line 52: This API will get real-time data from your workspace
line 53– 54: check empty data if True continue
line 58– 59: check if the data type is “message” and subtype is not “bot_message” then execute code in block
line 61– 65: response message to users
line 67: set delay for each round
line 68: in case that you can not connect to slack, then print the message
Yeah! all done
Now you can connect to your workspace by ChatBot and don’t get me wrong this article just introduce you to some aspect of the chatbot, the bot above is the simple version that I wrote for this purpose.
Hope all of you enjoy the bot and my work. If you like it please give me and 50 claps!! and don’t forget to follow me and iCreativeSystems for the great information from us.
Please leave me a message, love to hear from you^^
Ada Kaminkure ( Ada_92 ),
AdaBrain