How to send message to a Slack channel in Python
Question:
I need to send out message to a Slack channel with Python.
My Google search (here) and search from Stack Overflow (here) results in little help. What is the right way to get it working?
The closest guide I found is from Slack official guide here but it’s not clear how to do the authentication with SLACK_BOT_TOKEN
to call client.chat_postMessage()
from slack_sdk import WebClient
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
result = client.chat_postMessage(
channel=channel_id,
text="Hello world!"
)
Answers:
In brief
-
create Slack app aka myslackchat ref
-
grant scope chat:write
to Slack app ref
-
get Slack channel id aka channel id
-
get SLACK_BOT_TOKEN
of your Slack app
-
add Slack app myslackchat
to your Slack workspace, and 2nd, add myslackchat
to Slack channel channel id
-
python code to send message to channel id
, with SLACK_BOT_TOKEN
ref
p.s.
How to get
After days seeking Slack documentation for it, here is related guide; please search the page to get to this section OAuth Tokens for Your Workspace
– this is a pain. I have no idea why there is no SLACK_BOT_TOKEN mentioned in this guide.
Full details:
One thing you’ll need before starting is a Slack app. If you don’t have one yet, here’s a very quick guide to help you create one.
Requesting the necessary permissions chat:write
. This one grants permission for your app to send messages as itself (apps can send messages as users or bot users)
Requesting these permissions is easy:
- Load up the settings for your app from the app management page.
- In the navigation menu, choose the
OAuth & Permissions
feature.
- Scroll down to the Scopes section, and pick channels:read and chat:write from the drop down menu.
- Click save changes.
- Scroll back to the top of this page and look for the button that says Install App to Workspace (or Reinstall App if you’ve done this before). Click it.
I need to send out message to a Slack channel with Python.
My Google search (here) and search from Stack Overflow (here) results in little help. What is the right way to get it working?
The closest guide I found is from Slack official guide here but it’s not clear how to do the authentication with SLACK_BOT_TOKEN
to call client.chat_postMessage()
from slack_sdk import WebClient
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
result = client.chat_postMessage(
channel=channel_id,
text="Hello world!"
)
In brief
-
create Slack app aka myslackchat ref
-
grant scope
chat:write
to Slack app ref -
get Slack channel id aka
channel id
-
get
SLACK_BOT_TOKEN
of your Slack app -
add Slack app
myslackchat
to your Slack workspace, and 2nd, addmyslackchat
to Slack channelchannel id
-
python code to send message to
channel id
, withSLACK_BOT_TOKEN
ref
p.s.
How to get
After days seeking Slack documentation for it, here is related guide; please search the page to get to this section OAuth Tokens for Your Workspace
– this is a pain. I have no idea why there is no SLACK_BOT_TOKEN mentioned in this guide.
Full details:
One thing you’ll need before starting is a Slack app. If you don’t have one yet, here’s a very quick guide to help you create one.
Requesting the necessary permissions chat:write
. This one grants permission for your app to send messages as itself (apps can send messages as users or bot users)
Requesting these permissions is easy:
- Load up the settings for your app from the app management page.
- In the navigation menu, choose the
OAuth & Permissions
feature. - Scroll down to the Scopes section, and pick channels:read and chat:write from the drop down menu.
- Click save changes.
- Scroll back to the top of this page and look for the button that says Install App to Workspace (or Reinstall App if you’ve done this before). Click it.