Slack机器人向用户组发布消息。

huangapple go评论57阅读模式
英文:

Slack bot post mesage to usergroup

问题

我能够创建用户组并更新用户组。但在 Slack 文档中找不到将消息发布到用户组的方法。

我找到了这个函数 chat.postMessage,但从它接受的参数中,我找不到将消息发布到用户组的选项。

任何有关确定适当的 Slack API 函数的帮助都将不胜感激。

英文:

I was able to create usergroups and update usergroups.

But there was no method in slack document to post a message to usergroup

https://api.slack.com/methods

I found this function chat.postMessage but from the arguments it takes I couldn't find usergroups as an option to post the message to.

https://api.slack.com/methods/chat.postMessage

Any help figuring out the appropriate slack api function is appreciated.

答案1

得分: 2

chat.postMessage 是将消息发布到各种对话(例如公共频道、私人频道、直接消息或多人直接消息)的正确方式,在 channel 参数中,您应该提供正确的对话 ID,通常以 C**G**D** 开头,后跟字母和数字符号。Slack 用户组具有 S** ID,并且不被视为对话。如果您尝试使用 channel=S** 参数调用 chat.postMessage,将会收到 channel_not_found 错误。

Slack 用户组 通常用于更容易地将用户分开以便在频道中提及或批量邀请他们。最典型的用途是提及。然而,它们的提及方式有一些特殊,可以在 Slack 文档中的这里找到:https://api.slack.com/reference/surfaces/formatting#mentioning-groups,例如,您应该在您的 text 参数中包含 <!subteam^GROUP_ID> 或在您的文本 "type": "mrkdwn" 区块中包含。

例如,这段代码:

import logging

from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

log = logging.getLogger(__name__)

if __name__ == "__main__":
    client = WebClient(token="xox**")
    try:
        pt_mention = client.chat_postMessage(channel="C**",
                                             text="在纯文本中提及<!subteam^S**>用户组")
        print(pt_mention)

        block_mention = client.chat_postMessage(channel="C**",
                                                # text="在区块中提及<!subteam^S**>用户组。",
                                                blocks=[
                                                    {
                                                        "type": "section",
                                                        "text": {
                                                            "type": "mrkdwn",
                                                            "text": "在区块中提及<!subteam^S**>用户组"
                                                        }
                                                    }
                                                ])
        print(block_mention)
    except SlackApiError as e:
        logging.exception(f"发生错误: {e.response['error']}")

会在 C** Slack 频道中生成两条连续的消息,其中提及了具有 ID S**@test 用户组:此截图

然而,使用 conversations.invite 调用无法将用户组邀请到 Slack 频道中,您应该使用另一种方法,该方法在此处进行了描述。

英文:

chat.postMessage is a correct way to post message into various conversations, i.e. public channel, private channel, DM or multi-person DM and in the channel argument you should provide the correct conversation ID, usually starting from C**, G**, D** followed with alphanumeric symbols. Slack usergroups have S** ID and are not considered as conversations. If you'll try to call chat.postMessage with channel=S** argument you'll get a channel_not_found error.

Slack user groups are usually created to make easier users separation for mentioning or mass-inviting them into channels. Most typical, for mentioning. However, their mentioning is kind specific and described in the Slack documentation here: https://api.slack.com/reference/surfaces/formatting#mentioning-groups, i.e. you should include <!subteam^GROUP_ID> in your text argument or in your text "type": "mrkdwn" block.

For example, this code:

import logging

from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

log = logging.getLogger(__name__)

if __name__ == "__main__":
    client = WebClient(token="xox**")
    try:
        pt_mention = client.chat_postMessage(channel="C**",
                                             text="Mentioning the <!subteam^S**> user group in plain text.")
        print(pt_mention)

        block_mention = client.chat_postMessage(channel="C**",
                                                # text="Mentioning the <!subteam^S**> user group in the block.",
                                                blocks=[
                                                    {
                                                        "type": "section",
                                                        "text": {
                                                            "type": "mrkdwn",
                                                            "text": "Mentioning the <!subteam^S**> user group in the block."
                                                        }
                                                    }
                                                ])
        print(block_mention)
    except SlackApiError as e:
        logging.exception(f"Error occurred: {e.response['error']}")

produces two sequential messages into the C** Slack channel where it mentions @test user group with ID S**: on this screenshot.

However, it is not possible to invite a user group into Slack channel with conversations.invite call, you should use another method, described here.

huangapple
  • 本文由 发表于 2023年6月30日 02:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定