python - Slack 发送附件返回 500 错误
问题描述
我正在尝试将附件发送到 Slack 网络挂钩。
我遵循了 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 消息中缺少两个必填字段:text
和channel
. 您还需要将该short
字段的值小写为false
.
{
"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 - python中的Shift Aritmetic Right操作
- python - 如何将设置环境变量的批处理脚本与python脚本(3.7)集成?
- javascript - 在提交之前是否有另一种方法可以在 HTML 表单中添加多个标签?
- ios - 如何防止从 UIImageView 中删除图像?
- javascript - Gulp sass 正在吃 scss 文件而没有给出任何错误
- reactjs - 未捕获的 ReferenceError:RegeneratorRuntime 未在 React 中定义
- google-maps - 清理谷歌地图折线
- javascript - T[keyof T] 不能用于索引 {}
- opengl-es - 为什么我在 emscripten 使用“#version 300 es”得到“不支持的着色器版本”?
- javascript - 规范化 MongoDB,从文档中省略字段