Send Slack Messages With Python: Intro to Python slackclient

Tyler Tyler (275)
0

Slack has become one of the most important tools for communication in many companies, mine included. One reason it has seen such success, in my opinion, is because it's more than just a chat application—it's also a platform for developers to build interactive applications. This is an extremely powerful combination for companies.

With that said, we're going to dive into the basics of developing on Slack: creating a bot and sending messages to a Slack channel. This app will interact with Slack using a bot in a single workspace, which means we don't need to mess with advanced authorization flow. But you can also build apps that take actions on behalf of other users, or apps that take actions in other workspaces.

Let's get started!

Posted in these interests:

python
PRIMARY
62 guides
slack
6 guides

The name of the package we'll use is called slackclient, and it can be installed using pip. To install locally you can run:

pip install slackclient

Or add it to your project's requirements.txt file, and install using:

pip install -r requirements.txt

To generate a Slack API token, you'll need to run do the following:

  1. Create a new Slack App
  2. Add permissions
  3. Copy the token URL

Create a new Slack App

To start, you'll need to create a Slack App. Follow the link, and click Create New App. The process is fairly self-explanatory.

Add permissions

In the menu on the left, find OAuth and Permissions. Click it, and scroll down to the Scopes section. Click Add an OAuth Scope.

Search for the chat:write scope, and add it. At this point, you'll need to re-install the app in your workspace for the permissions to take effect.

Copy the token URL

On the same page you'll find your access token under the label Bot User OAuth Access Token. Copy this token, and save it for later.

In the Slack application, choose a channel that you want the bot to interact with. Click the Conversation Settings icon (the cog), and then click Add an app.

Search for your app name, then click Add.

At this point, we have everything we need to send a message. Below is a simple example to get you started:

import slack

client = slack.WebClient(token='xxxyourtokenxxx')
client.chat_postMessage(channel='general', text='This is only a test.')

You've heard it elsewhere, and I'm going to say it here as well. You shouldn't store the slack token in your codebase. So even if you hardcode the token at first, don't commit.

Environment variables

An acceptable solution is to set the token as an environment variable on your server or container. So in your Dockerfile or startup script, you could set the environment variable:

SLACK_TOKEN=xxxslacktokenxxx

Then in Python:

import os

slack_token = os.environ.get('SLACK_TOKEN')

Secrets manager

An even better option is to use a secrets manager. We use Kubernetes to manage our application, so we utilize Kubernetes Secrets. If you go this route, you'll have to use whatever solution fits your use case. I don't have many recommendations besides Vault.