How to make a slack bot in python using Slack’s RTM API
Ever wondered about creating a slack bot to help you come out of some trivial task you need to do every day?
Hello developers! How you Doin’ :) I hope that you are spending your time productively. Today we are going to make a slack bot using python by leveraging the power of slack’s RTM API. The slack’s Real-Time Messaging API, RTM API for short is a “WebSocket-based API that allows you to receive events from Slack in real-time and send messages as users”.
Slack bots are widely used nowadays as they help us to mitigate boring and time-consuming jobs. For our purpose, we will be creating a bot that will help us find things to do when we’re bored. So, for example, our slack bot can give activity to perform like to “start a book you’ve never gotten around to reading” or maybe “Shred old documents you don’t need any more” to help us get out of boredom. So without further ado let’s get started!
Let's jump-off straight by creating a slack app. A very important note here: New slack apps can not use Real-Time Messaging API methods. So we need to create a classic slack app. So, make sure that you create a classic slack app and not a new slack app. If you are interested to know the difference between the classic and the new slack apps, you can check it from here.
So, first of all, hit this link that redirects you to creating a classic slack app. Sign in to slack if you are not, click on Create New App and give your slack app a name and the workspace in which you want to install your app.
Here, I am giving my app name AmuseBot and the workspace in which I will be interacting with the bot will be TimelineWave. Click on Create App button. You will be navigated to the Basic Information page of your slack app after clicking on the create app button.
Now in the Basic Information page, we will tell the slack app what features and functionality we need to perform with the app. Since we just need a bot to exchange messages with the user we will choose Bots feature.
Now, we need to give scopes to our bot. The scope is basically the type of information a bot is allowed to access from the workspace. The scope can be like reading or sending messages from/to the channel etc. The slack provides a legacy bot user option that provides us with a bundle of scopes that will suffice our bot’s scope needs. So, by simply selecting “legacy bot user” we will get some basic bot scopes that will help us prevent selecting different scopes one by one for our bot from a huge list of scopes that slack app provides.
So, click on the Add Legacy Bot User button.
Clicking on the button will prompt you to enter a Display Name and a default username for your bot. The display name will be the name of the bot that will appear on the slack workspace. Choose any display name and the default name for the bot of your choice. Here I am giving again the same AmuseBot name to the display name as well as the default username to my bot.
So, our slack app part is done. One last thing left is getting the Bot User OAuth Access Token of the slack app so that we can receive events from the slack from our python script. So, navigate to the OAuth & Permissions tab from the sidebar of your app and then install the app on your workspace. After you install the app, you will get the OAuth tokens. Copy the Bot User OAuth Access Token.
Just for a recap: we have created a classic slack app. We have then defined the features and functionalities our slack app is going to perform, given scopes to the app and lastly we have given our bot different kinds of name, duh!
Now, we will move ahead with our python script. The python script will basically read the messages from the slack and in turn sends back an appropriate response to the user. So, fire up your terminal and install slack client library that interfaces with the slack’s Real-Time Messaging (RTM) API using pip.
pip install slackclient
Make a file slackbot.py and add the following code into the file.
The code here is fairly simple to understand. First, we have imported the RTMClient from slack which provides information about different events occurring on slack. Next, we have made a function amusebot that triggers whenever an event of type message occurs in the slack. So the function ignores all events which are not related to message type event. So, whenever someone sends a message on slack our amusebot function will trigger. Here payload in the amusebot function is an argument that receives the keyworded, variable argument list from the slack. We will extract the relevant data from the payload variable list like the web_client, bot_id, text sent by the user, etc. One more important thing is we have written an if statement to check if the message sent on the slack is not of the bot. If we don’t use this conditional statement then the bot will keep messaging and then replying to itself forever.
We have used bored API for some activities to be sent to the user. You can try using different APIs like “pokeApi” which gives information on different pokemon. You can also try “wiki API” which can give the user an ability to read the information from Wikipedia on their slack workspace. If you are interested in more ideas on different APIs for your bot, you can check out my friend’s insta page ivejo3000. The page is all about the information on different cool APIs.
So we then make a GET request to the bored API, extract out the activity part from the JSON response and send it back to the user using slack’s chat_postMessage method.
And at last, we have created an instance of RTMClient where we have passed the “Bot User OAuth Access Token” and we have started our bot using RTMClient’s start method.
Now you can interact with the bot after running the script after inviting the bot to your workspace. For inviting the bot simply send a message @YOUR_BOT_DISPLAY_NAME to the workspace in which you have installed the bot.
So now whenever you get bored and want some ideas to get rid of boredom you can simply hop over onto your slack and find some amusing thing to do. I hope that you now have got a basic idea of how you can create a slack app that can really help you out in many ways. If you have any doubts feel free to ping me in person or in the comment section. You can also share your slack bot ideas in the comment section. You can connect with me on Instagram and Twitter.
Show some love with claps if you learned something valuable from this article. And yeah the most important thing, never forget to put semi-colons ;) Adios