You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Slack WebHook发送附件返回500错误,如何成功发送?

搞定Slack WebHook附件发送的500错误问题

嘿,我一眼就瞅出你代码里的问题啦!你能发简单消息说明WebHook配置没问题,但发送附件时返回500,是因为你构建附件的结构多套了一层,让Slack服务器没法正确解析请求 payload 咯。

问题出在哪?

你现在的代码里,先创建了一个attachments列表,然后把一个包含attachments键的字典塞进去,再传给slack.notify()的attachments参数。但实际上,slackweb的notify方法要求attachments参数直接接收附件对象的列表,不需要在外层再套一个带attachments键的字典——你这相当于把整个请求的结构又嵌套了一遍,服务器当然会报错啦!

修正后的代码

直接把附件对象放进列表里,去掉多余的嵌套:

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

# 简单消息测试(没问题可以保留)
slack.notify(text="Maguro is a sushi")

# 正确构建附件列表:直接是单个附件对象的数组,去掉外层的"attachments"键
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参数就行
slack.notify(attachments=attachments)

小补充

要是你想同时发主文本和附件,直接在notify里同时传两个参数就行:

slack.notify(text="这是主消息内容", attachments=attachments)

另外,既然简单消息能发,WebHook的权限和URL肯定是对的,不用再纠结这方面啦~

内容的提问来源于stack exchange,提问作者Piers Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:13:45