我正在尝试向Slack web 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)
答案 0 :(得分:1)
您的Slack消息中缺少两个必填字段:text
和channel
。您还需要将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结构,你应该好好去。