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

Sco*_*key 1 curl slack-api

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

这是我现在拥有的基本curl语句,我想添加一个附件:

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

以下是附件的示例:

{
    "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
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

Eri*_*ken 5

它实际上很容易。attachments只是 JSON 数组中的另一个键,与 处于同一级别text

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

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

         "text": "Optional text that appears within the attachment",
         ...
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

顺便提一句。您可以channelicon_url同样的方式添加其他键。