【问题标题】:Syntax help - adding attachments to slack incoming webhooks语法帮助 - 将附件添加到松弛的传入 webhook
【发布时间】:2017-09-26 21:02:51
【问题描述】:

我正在编写一个松弛的集成,我正在尝试添加一个消息附件,但我被困在将它们全部放在一个 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 语句,以便我了解它是如何完成的吗?

【问题讨论】:

    标签: curl slack-api


    【解决方案1】:

    其实很简单。 attachments 只是 JSON 数组中的另一个键,与 text 处于同一级别。

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

    {
      "text": "here goes your message text",
       "attachments": 
       [
          {
    
             "text": "Optional text that appears within the attachment",
             ...
          }
       ]
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多