Warm tip: This article is reproduced from stackoverflow.com, please click
json slack-api

Slack API

发布于 2020-03-27 10:19:44

I have a issue ticket application and I want to post a message in dedicated channel when a new ticket is created. The idea is to have a basic inform message like 'new ticket' and a button leading to the ticket webpage.

But I have an issue with the slack api, I'm not able to add the button (only the text is posted). Even when I use the json of the documentation example, it doesn't work.

Note: I am able to post simple messages, no problem.

Tried to adapt the message, give an url to button in place of value

JSON:

{
    "channel": "XXXXXXXXXX",
    "type": "section",
    "text": {
    "type": "mrkdwn",
    "text": "You can add a button alongside text in your message."
    },
    "accessory": {
    "type": "button",
    "text": {
        "type": "plain_text",
        "text": "Button",
        "emoji": true
    },
    "value": "click_me_123"
    }
}

The result : https://i.imgur.com/8CueU0y.png

Even the text is not handled correctly.

Questioner
Thibaud Lafont
Viewed
85
Erik Kalkoken 2019-07-03 21:52

Your JSON does not follow the correct syntax for messages and can therefore not be displayed by Slack.

You need to include your JSON for the blocks under a property blocks in your message definition - on the same level like channel. Your JSON for blocks also needs to be an array.

Working example:

{
    "channel": "XXXXXXXXXX",
    "blocks":
    [
       {
          "type":"section",
          "text":{
             "type":"mrkdwn",
             "text":"You can add a button alongside text in your message."
          },
          "accessory":{
             "type":"button",
             "text":{
                "type":"plain_text",
                "text":"Button",
                "emoji":true
             },
             "value":"click_me_123"
          }
       }
    ]
}

See this link for the reference documentation on "Putting blocks in messages"