In the last article titled “Slack manual” I have discussed in detail about how to create WorkSpace and Channels in Slack. If you have not read that article, I would highly recommend you to go read that article first before going through the steps mentioned-below. In this article I shall be talking about how you can send messages from your Python App to your Slack Workspace using Slack API. So without further ado let us get started!
Install Python
Go to the following link:
https://www.python.org/downloads/
and install Python. If you’re using Mac, choose Mac version of Python and install that. The steps of installing Python are pretty straightforward. If you encounter problems while installing Python, comment down below. I shall sort them out.
Install Slacker and SlackClient using pip command
Once you install Python, you would be able to use the pip command. Open CMD(command prompt) on Windows, and use the following commands as shown below:
pip install slacker pip install slackClient
If you are using terminal on Mac, and pip command is not working, then first you need to install the pip by using the following command in terminal:
sudo easy_install pip
Services of SlackAPI
Once you have installed SlackClient and Slacker in your computer, you now need to go to the services of Slack. For doing so, first you need to login to your workspace, which I have explained in the last tutorial, and type the following URL in the URL box of your favourite browser:
{Your-slack-workspace}.slack.com/apps/manage
{Your-slack-workspace} is the workspace that you have created in the last article. In my case, I have created the workspace named “slackcheck-workspace.slack.com”(as shown below).
Now search for Bots in Slack apps(as shown below) and hit enter.
Now click on the Bots as shown below.
Click on the App Configuration as shown below:
Now give the username to your Bot. Here I am given “slacker” to the username(as shown below) and click Add bot integration.
Now you would have an API Token, which we would be required for Bot to work. Copy and paste the API Token in a text document, as we would use it later.
You can change the icon of the BOT as well by scrolling down and finding the following field shown below:
Now scroll down below and click Save Integration.
Once you save the Integration, you should go back to your WorkSpace.
Now there you would see the Channels, as in my case, following are the two Channels:
You can create a new Channel, but I am going to send messages to the #general Channel.
Adding the Bot to the Channel
Step 1:
In the Apps section, you would see the “slacker” app(as shown below) and click on the gear button at the top(highlighted):
Step 2:
Click on the Add Bot to the Channel Option(shown below):
Step 3:
Select the Channel in which you want to send messages and Click Add(as shown below):
Python Part to Connect The Bot
Step 1:
Create the Python file, and name is whatever you want. I have “SlackPython.py”.
Step 2:
Write the following snippet of Python code:
from slacker import Slacker
slackClient =
Slacker("xoxb-332633498646-5ONFuk6Bau3TdwmrOtcEB5dC")
messageToChannel = "Hello from the Other side of
Python!"
slackClient.chat.post_message("#random",
messageToChannel)
Output
Run the above snippet of code. You would see the following output:
Hurrah! You have done it!
Summary
In this article, I have shown you how you can send messages to Slack from your own Python Application. We have also seen how we can add Bots to the channels. That’s it for this tutorial. I hope you like it!
Related article
- Summary of Python Development.
- All Python articles are here