语法帮助 - 添加附件到 slack 传入的 webhooks

Syntax help - adding attachments to slack incoming webhooks

我正在编写一个 slack 集成,我正在尝试添加一个消息附件,但我坚持将它们全部放在一个 curl 语句中的语法。

这是我现在想要添加附件的基本 curl 语句:

curl \
-X POST \
-H "Content-type: application/json" \
--data "{\"text\":\"$MESSAGE\"}" \
https://hooks.slack.com/services/code1/code2/code3

下面给出附件的例子:

{
    "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
        }
    ]
}

但是,我对如何将该附件块放置在 curl 语句中感到困惑。有人可以写一个完整的 curl 语句,其中包括我的附件块,以便我看看它是如何完成的吗?

其实很简单。 attachments 只是 JSON 数组中的另一个键,与 text.

处于同一级别

所以你的新 JSON 数组应该看起来像这样(在字符转义之前):

{
  "text": "here goes your message text",
   "attachments": 
   [
      {

         "text": "Optional text that appears within the attachment",
         ...
      }
   ]
}

顺便说一句。您可以以相同的方式添加其他键,例如 channelicon_url