How to Create Messaging App “SlackBot” Using Python

Sabina Akter
5 min readDec 24, 2022

Hello Everyone, Welcome to my MediumPage!! In this article, I will go over on how to Create a SlackBot using Python.

Let’s get started!

Prerequisites:

  • I used VS Code, but you can use any text editor you prefer
  • python3 installed in your local machine
  • pip installed in your local machine
  • Basic python programming language knowledge
  • Basic knowledge on how to use slack app

Step:1 | Go to api.slack.com and Click “Create an app”

Select Create an app

Make sure you’re logged into your workspace! If you don’t have any slack workspace, create one. ( See the link here).

Step:2 | Select “From Scratch and git it a name, I named Slack Bot and select your slack workspace name from the drop-down. Then click create app

Select From scratch
Name app & choose workspace

Step: 3 | Select “Bots” From The Menu

Select Bots

Step: 4 | Click “Review Scopes to Add

Review Scopes to Add

Step: 5 | Scroll Down And Click “Add an OAuth Scope”

Add an OAuth Scope

Step: 6 | Select “chat:write” From The Dropdown Menu

Select “chat:write”

Step: 7 | Scroll Up And Click “Install to Workspace”

Install Workspace

Once you click install to Workspace you will get your slack workspace “User OAuth Token” and you can copy it and store somewhere, we will need that inside .env file later.

Step : 8 | Open VS Code, Make a New Folder With a “main.py” and “.env” Files 📁

You can make this folder anywhere you prefer, like in your Desktop or Downloads or Documents . Note that the .env file will be hidden in your finder, but will visible in your code editor. The project structure should be as following:

Project/Folder Structure

I have created a new folder and named it SLACKBOT and inside SLACKBOT I have created 2 files main.py and .env

Step : 9 | Copy The “Bot User OAuth Token” and Paste it in The .env File With The Environment Variable Name: SLACK_TOKEN

SLACK_TOKEN

The API key 🗝️ will help Slack to identify your Slack workspace.

📓Note: Do NOT share this key with anyone.

Step : 10 | Open VS Code Terminal And Install “slackclient” and “python-dotenv”, See The Command below:

Step : 7 |Open VS Code, Make a New Folder With a “main.py” and “.env” Files 📁

📔 Note: make sure you’re in the SLACKBOT directory.

Step : 11 | In The main.py File Type The Following Scripts :

main.py file

You can check my GitHub repo here.

GitHub Repo

You see there are some imports that will need to fetch and use our API Key 🔑

Step : 12 | Go to Slack, Create a New Channel Called “testnew”

Channel create

In slack click on the + icon on the channel and one box will pop up for Create a channel, give the channel a name “testnew” and hit Create.

Step : 13 | Click On The App On Slack and You should see The App Name We Created Slack Bot

Slack Bot App

Step : 14 | Go To The #testnew Channel and In The Message Box Type The App Name @Slack Bot, once a box pop up Want to add this app, Click Add to Channel

App Added To The Channel

Final Step : Test Your Code By Running The Following Script on Terminal:

python3 main.py

⚠️Make sure you’re in the SLACKBOT directory on VS Code

If everything okay, then you should see the message like this attached in your slack channel #testnew:

Message “Hello World on #testnew Channel”

📓 Note: When I run the python3 main.py, I have change the message from “Hello” to “Hello World!” in VS Code and save it, see below:

client.chat_postMessage(channel=’#general’, text=”Hello World!”)

Now sending another message from the VS Code, just change the message content, see below and save it:

client.chat_postMessage(channel=’#general’, text=”Hello Woman In Tech!”)

  • Then go back to the VS Code cli and run again python3 main.py

You will see the message on #testnew slack channel, see the attached screenshot:

2nd message on the slack

🤔❌ Running Into Errors? See the 1st error I got on VS Code Cli and how I fix it:

Error: Slack AP failed on VS Code Cli

Solution: I have reinstalled the workspace by going to https://api.slack.com/. See the attached.

Go to OAuth @ Permissions and click Reinstall to Workspace

Then run the python3 main.py on the VS Code Cli that should fix the issue.

2nd Error:

The server responded with: {‘ok’: False, ‘error’: ‘not_in_channel’}

Solution: I have realize I didn’t add the app in the #testnew channel

I just added the app Slack Bot to the #testnew channel and run the python3 main.py from the Cli that fixed the issue.

You’ve reached the end of the tutorial!

Congratulations 👏👏👏👏👏!! You did an amazing job 🎉🎉🎉🎉🎉!!

Stay Tuned for This Project. I would like to add more feature on this project !!

👏👏👏👏 Happy Learning!!

Thank you everyone 😃, for reading my blog!!

If you think, this article is helpful 😄, please give this story a clap 👏👏 to inspire me.

You can connect me on linkedin 👎

https://www.linkedin.com/in/aktersabina122/

Please follow me on Medium and Twitter 👎:

https://aktersabina122.medium.com/

https://twitter.com/aktersabina122

References:

https://api.slack.com/apps

--

--