python - Slack 发送附件返回 500 错误

标签 python python-3.x slack slack-api

我正在尝试将附件发送到 Slack 网络 Hook 。

我已遵循 API 文档并可以发送简单的消息。当我尝试发送附件时出现 500 错误,我认为我的有效载荷存在问题,但我无法解决这个问题。

如何让附件发布成功?

import slackweb

slack = slackweb.Slack(url='WEB HOOK URL HERE')

slack.notify(text="Maguro is a sushi")

attachments = []
attachment = {
    "attachments": [
        {
            "fallback": "Required plain-text summary of the attachment.",
            "color": "#36a64f",
            "pretext": "Optional text that appears above the attachment block",
            "author_name": "Bobby Tables",
            "author_link": "http://flickr.com/bobby/",
            "author_icon": "http://flickr.com/icons/bobby.jpg",
            "title": "Slack API Documentation",
            "title_link": "https://api.slack.com/",
            "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "Priority",
                    "value": "High",
                    "short": False
                }
            ],
            "image_url": "http://my-website.com/path/to/image.jpg",
            "thumb_url": "http://example.com/path/to/thumb.png",
            "footer": "Slack API",
            "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
            "ts": 123456789
        }
    ]
}

attachments.append(attachment)
slack.notify(attachments=attachments)

最佳答案

您在带有附件的 Slack 消息中缺少两个必填字段:textchannel。您还需要将 short 字段的值小写为 false

See the corrected message here in Slack's message tester :

{
    "text": "You need this field",
    "channel": "C########",
    "attachments": [
        {
            "fallback": "Required plain-text summary of the attachment.",
            "color": "#36a64f",
            "pretext": "Optional text that appears above the attachment block",
            "author_name": "Bobby Tables",
            "author_link": "http://flickr.com/bobby/",
            "author_icon": "http://flickr.com/icons/bobby.jpg",
            "title": "Slack API Documentation",
            "title_link": "https://api.slack.com/",
            "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "Priority",
                    "value": "High",
                    "short": false
                }
            ],
            "image_url": "http://my-website.com/path/to/image.jpg",
            "thumb_url": "http://example.com/path/to/thumb.png",
            "footer": "Slack API",
            "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
            "ts": 123456789
        }
    ]
}

创建一个包含这两个字段和您的 attachments 变量的 JSON 结构,您应该可以开始了。

关于python - Slack 发送附件返回 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50601632/

相关文章:

python - 机器人框架 - UnicodeDecodeError : 'ascii' codec can't decode byte 0xe9 in position 49: ordinal not in range(128) while inputting text

python - 如何更改为背景颜色

java - 将 Flask 消息淡出为多条消息

python zlib如何解压多个对象

python - 在多个装饰器之间传递参数

python - 不明白这个SyntaxError : illegal target for annotation

php - Laravel 记录到 Slack 工作簿导致 "Curl error (code 3): <url> malformed"

slack-api - 为什么 Slacks 交互式消息不以 JSON 格式发布?

slack - 如何在 Slack 代码片段中创建超链接

c++ - 使用外部类的枚举来避免在 .proto 中定义?