Python Slack Bot is among one the highest-earning bot. Today, you’ll learn about what this bot is and will make a simple python slack bot.
Getting started with Python Slack Bot
In our previous tutorials, you learned about various automation libraries, automating desktop and browser tasks, creating a telegram chatbot, and more but in this tutorial today, you’ll learn a similar way of creating a python slack bot that will help send messages and handle events in your slack workspace.
Introduction to Slack
Slack is a new messaging program designed specifically for the workplace to communicate with your team. It is faster, better, and more secure than emails. It is a free or paid cross-platform tool, accessible from desktop, mobile, and web as well. You can visit Salck’s official website to know more about it.
In order to make a Python slack bot, you’ll have to follow the following algorithm:
- Get an API token for your bot from slack’s API site.
- Import Python Slack Bot Library and/or other necessary libraries in python.
- Trigger the bot through the API Token.
- Send the messages you want through your bot.
Get the Slack API Token
In order to code your own Slack bot, you need to get a slack API from slack’s API website. While creating a telegram bot in our last tutorial, we took an API token from Telegram BotFather, but here we’ll do the same from slack’s official API website. If you haven’t checked our previous tutorial on Telegram Chatbot with python, it is recommended to go through it to have a basic knowledge of APIs. Follow these simple steps to get an API for your bot from slack.
1. Go to Slack’s official API page.
2. Click on the Your Apps button on the top right of the page.
3. And then click on Create your first app and select from scratch.
4. Type the name you want to give to your bot (here Webmatrices Bot) and select the slack workspace where you want to add the bot and click on Create App. (Keep in mind that the workspace can’t be changed later.)
5. Now your API application and you’ll be redirected to your app dashboard and here you’ll now configure your bot.
6. Scroll the page down and click on Bots.
7. Now click on Review scopes to Add.
8. Now here you’ll provide the necessary permissions to your bot like read/write messages to a channel and/or more. Scroll down to the scopes section and click on Add an OAuth Scope. Now you can select as many permissions as you want, but for now, we only need writing message permission for a chatbot so I’ll add only that and move ahead.
9. Similar to what we did in the telegram chatbot, now we’ll generate an API token for your slack bot too. For this, we need to install our app in our selected workspace. Scroll to the top of the page and click on Install to Workspace and click on allow.
10. Now you’ll see your bot token is generated and you can copy it for future use.
Diving into the Code
The first and foremost thing you need to do is import the required packages. For this task you’ll require only one package:
and you can install
slack_sdk using the following command:
pip install slack_sdk
Now you import these packages to your code simply by the following code:
import slack_sdk as slack
Import API token to your code
As you know, an API token is a confidential credential and it should not be made public in your code. Therefore, you’ll need to store the API token somewhere outside your code and import it as a variable whenever needed. In order to maintain the confidentiality of your API token, you can use various methods. You can also save your API token as an environment variable on your device and import it using the os module, in a similar way as you did in the telegram chatbot tutorial. Or you can also save the API token as .env file and import it with
python-dotenv module as shown below.
Save API token as .env file and import using python-dotenv module
For this, you’ll need to create a new file named .env and save the token value as a variable.
Now you’ll need to install
python-dotenv module using pip. You’ll also need
pathlib library for this but they are pre-installed in python 3.4 and above.
pip install python-dotenv
Now you’ll import this module to your code and use it to import API token.
#Importing Libraries import os import pathlib import python-dotenv #Set the path of .env file using pathlib env_path=Path('.') / '.env' #loading env with load_dotenv function of python-dotenv module load_dotenv(dotenv_path=token_path) #saving bot api token as variable bot_api_token=os.environ['bot_token']
Triggering the Bot and Event Handling
As we’ve accessed the API token variable, now we’ll create a slack client listener with the API token. There are different types of client listeners for slack but we’ll be using the WebClient only in this tutorial.
Bot Sending Messages to a Channel
In order to make the bot send messages to a channel in your specific workspace, you’ll need to add the bot to that channel on slack. Let’s see how to do that.
Adding Bot to Slack Channel
1. Go to Slack’s Website and log in with your account and select the workspace your bot is added to (here Webmatrices).
2. Go to the channel where you want the bot to send messages to (here #blogs) and add the bot. For that, you’ll need to click on the icon on the top right corner of the page with your profile photo and a number alongside it.
3. Then select the Integrations tab and click on Add an App.
4. Now select the bot you want to add with its name (here: Webmatrices Bot) by clicking the Add button alongside its name.
Now your bot is added to the channel you want and you can start sending messages to this channel through your bot.
Sending Messages to a Channel
Now you can use the
.chat_postMessage() function of the slack module to send messages to the channel. This function takes two parameters: one is channel (the name of the channel where you want to send the message) and the other is text (the text you want to send).
client.chat_postMessage(channel='#blogs',text='Hello Webmatrices Readers !')
import slack_sdk as slack import os from pathlib import Path from dotenv import load_dotenv #Set the path of .env file using pathlib token_path=Path('.') / '.env' #loading env with load_dotenv function of python-dotenv module load_dotenv(dotenv_path=token_path) #saving bot api token as variable bot_api_token=os.environ['bot_token'] client=slack.WebClient(token=bot_api_token) client.chat_postMessage(channel='#blogs',text='Hello Webmatrices Readers !')
So, now you’ve learned to create a python slack bot. Now you can use this script to create your own bot for specific tasks. You can create such chatbots and host them on different servers to keep them alive 24×7 and you can sell them to companies to earn from your bot. More and more functionalities will be added to the bot in upcoming tutorials.