简体   繁体   English

如何向 Python 中的 Slack 频道发送消息

[英]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:请求这些权限很容易:

  • Load up the settings for your app from the app management page.从应用管理页面加载应用的设置。
  • In the navigation menu, choose the OAuth & Permissions feature.在导航菜单中,选择OAuth & Permissions功能。
  • Scroll down to the Scopes section, and pick channels:read and chat:write from the drop down menu.向下滚动到 Scopes 部分,然后从下拉菜单中选择 channels:read 和 chat:write。
  • 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.点击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python在频道上向用户发送Slack消息 - Send a Slack message to a user on a channel using Python 通过 Slack Chat 将消息发送到 Slack 消息到 Slack 频道。发布消息 API? - Send Message to Slack Message to Slack Channel through Slack Chat.Post Message API? 使用python将json消息发布到松弛通道 - post json message to slack channel using python 无法使用 slack SocketModeHandler 从 Python 中的 slack 读取通道消息 - Cannot read channel message from slack in Python using slack SocketModeHandler 如何使用Slack API将格式化的消息发布到通道中 - How to post formatted message into a channel with Slack API 如何使用Webhook和Python将错误消息发送到Slack? - How to send error message to slack using webhook and Python? 如何使用日志记录发送松弛日志作为消息在python和flask中? - How to send log using logging to slack as message in python and flask? 如何在安装 Slack 应用程序后立即向用户发送消息并创建频道 - How to send user a message and create a channel right after installation of Slack app 通过应用bot向松弛用户发送直接消息,但不在应用通道中发送 - send direct message to slack user from a app bot but not in app channel 我如何获得一个通过 Python 发送消息以发布 @here 并 ping 通道的 slack 机器人? - How do I get a slack bot that is sending a message via Python to post @here and ping the channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM