我可以将格式丰富的 Slack 消息作为机器人而不是 Webhook 发送吗?

Can I send richly-formatted Slack messages as a Bot and not as a Webhook?

我在 Python 开始编写一个 Slack 机器人,但当我找不到使用以下两种方法中的任何一种发送格式丰富的消息的方法时停了下来:

sc.rtm_send_message("channel_name", my_message)

sc.api_call("chat.postMessage", channel="channel_name", text=my_message, username="username", icon_url="icon_url")

其中 my_message = json.dumps({'attachments': [{...}]})

我现在知道我可以使用 webhook 方法来完成此操作,但是可以使用上述方法吗?

API(方法 chat.postMessage)和传入 webhook 都提供相同的选项来格式化您的消息,包括标记和附件。

提示:如果您想在附件中使用标记,请确保添加字段 "mrkdwn_in" 并命名您要在其中使用它的字段,否则它会被 Slack 忽略。

示例:

{
    "attachments": [
        {
            "title": "Title",
            "pretext": "Pretext _supports_ mrkdwn",
            "text": "Testing *right now!*",
            "mrkdwn_in": ["text", "pretext"]
        }
    ]
}

有关完整文档,请参阅 here

我发现我哪里出错了。

我在 sc.api_call 方法中将消息传递给了错误的参数。

我应该把它传递给 sc.api_call(attachments=...) 参数,而不是 text 参数。