Integrating Slack API Using Python: Step-by-Step Guide

In this article I will describe how to use Slack API to send message and files in slack channel

Daria Katok
3 min readOct 1, 2024

In this article, I will walk you through the process of implementing Slack API integration using Python. We’ll cover how to connect to Slack’s API, send messages to channel, providing you with practical code examples and clear steps to get started quickly. Give me a 👏 to support my blog 😊.

Step 1: Create a Slack App

For integration with Slack client you need to create slack app in your workspace.

  1. You need to follow this link : https://api.slack.com/apps.

2. Press “Create New App

3. Choose “From “Scratch” option

4. Provide name and select you workspace for integration. Press “Create App” button

Step 2: Configure OAuth & Permissions

  1. Navigate to OAuth & Permissions: On the left sidebar, under “Features” click on “OAuth & Permissions”.

2. Scroll down to “Scopes.” Under “Bot Token Scopes,” click “Add an OAuth Scope” and required scopes for you task. For example :

  • chat:write: To allow your app to post messages in channels.
  • chat:write.customize: To send messages with a custom name and avatar.
  • files:write: To upload and share files within Slack channels.

Full list of scopes you can find here : https://api.slack.com/scopes

3. Scroll up and click “Install to Workspace.”

4. Follow the prompts to install the app to your Slack workspace.

5. After installation, you’ll receive a Bot User OAuth Token. Copy this token and keep it secure.

Step 3: Add Slack App to chat

  1. Open Slack app. Create new channel or navigate to exist channel.
  2. Click on channel name and switch to Integration tab. Click on “Add an App” button
  3. Select you app from the list
  4. Copy channel id and keep it secure

Step 4: Write Python Code to Send Messages via Slack API

  1. Clone the repository to your local machine and add a .env file with SLACK_BOT_TOKEN and CHANNEL_ID variables saved from the previous steps.
  2. Create a virtual environment for your project and install dependencies from the requirements.txt file.
  3. Update the script.py file according to your needs
  4. Run the script.py file by command python script.py.

Voila! I hope you enjoy the result !

Remember that you can run script automatically using GitHub Webhooks

--

--