[英]How to send message to a Slack channel in Python
I need to send out message to a Slack channel with Python.我需要使用 Python 向 Slack 频道发送消息。
My Google search ( here ) and search from Stack Overflow ( here ) results in little help.我的 Google 搜索( 此处)和 Stack Overflow 搜索( 此处)几乎没有帮助。 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()
我找到的最接近的指南来自 Slack 官方指南,但不清楚如何使用
SLACK_BOT_TOKEN
进行身份验证以调用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创建 Slack 应用程序又名 myslackchat ref
grant scope chat:write
to Slack app ref授予 scope
chat:write
Slack 应用参考
get Slack channel id aka channel id
获取 Slack 频道 ID 又名
channel id
get SLACK_BOT_TOKEN
of your Slack app获取 Slack 应用的
SLACK_BOT_TOKEN
add Slack app myslackchat
to your Slack workspace, and 2nd, add myslackchat
to Slack channel channel id
将 Slack 应用程序
myslackchat
添加到您的 Slack 工作区,第二,将myslackchat
添加到 Slack 频道channel id
python code to send message to channel id
, with SLACK_BOT_TOKEN
ref python 代码将消息发送到
channel id
,带有SLACK_BOT_TOKEN
参考
ps ps
How to get怎么获得
After days seeking Slack documentation for it, here is related guide ;在为它寻找 Slack 文档几天后,这里是相关指南; please search the page to get to this section
OAuth Tokens for Your Workspace
- this is a pain.请搜索页面以访问此部分
OAuth Tokens for Your Workspace
- 这很痛苦。 I have no idea why there is no SLACK_BOT_TOKEN mentioned in this guide.我不知道为什么本指南中没有提到 SLACK_BOT_TOKEN。
Full details:完整详情:
One thing you'll need before starting is a Slack app.在开始之前您需要做的一件事是 Slack 应用程序。 If you don't have one yet, here's a very quick guide to help you create one .
如果您还没有,这里有一个非常快速的指南来帮助您创建一个.
Requesting the necessary permissions chat:write
.请求必要的权限
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:请求这些权限很容易:
OAuth & Permissions
feature.OAuth & Permissions
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.