Bolexle
Bolexle

Reputation: 23

What is wrong with my JSON output for a Slack Message payload?

I have set up what I think should be a working JSON output to send a message in slack but Slack keeps rejecting it.

I have tried multiple different message layout formats using the guides on slack's api site, but so far the only method that has successfully sent is a fully flat JSON with no block formatting.

function submitValuesToSlack(e) {

  var name = e.values[1];
  var caseNumber = e.values[2];
  var problemDescription = e.values[3];
  var question = e.values[4];
  var completedChecklist = e.values[5];

  var payload = [{
    "channel": postChannel,
    "username": postUser,
    "icon_emoji": postIcon,
    "link_names": 1,
    "blocks": [
      {
        "type": "section",
        "fields": [
          {
            "type": "mrkdwn",
            "text": "*Name:*\n " + name
          }
        ]
      }]
  }];
  console.log(JSON.stringify(payload, null, "\t"));

  var options = {
    'method': 'post',
    'payload': JSON.stringify(payload)
  };

  console.log(options)

  var response = UrlFetchApp.fetch(slackIncomingWebhookUrl, options);

}

When I run this, I get the following output:

[  
   {  
      "channel":"#tech-support",
      "username":"Form Response",
      "icon_emoji":":mailbox_with_mail:",
      "link_names":1,
      "blocks":[  
         {  
            "type":"section",
            "fields":[  
               {  
                  "type":"mrkdwn",
                  "text":"*Name:*\n test"
               }
            ]
         }
      ]
   }
]

Which I believe is correct, however slack api just rejects it with an HTTP 400 error "no text"

am I misunderstanding something about block formatting?

EDIT:

To Clarify, formatting works if I use this for my JSON instead of the more complex format:

{  
      "channel":"#tech-support",
      "username":"Form Response",
      "icon_emoji":":mailbox_with_mail:",
      "link_names":1,
      "text":"*Name:*\n test"
}

Upvotes: 2

Views: 5681

Answers (1)

Erik Kalkoken
Erik Kalkoken

Reputation: 32757

The reason you are getting the error no_text is because you do not have a valid message text property in your payload. You either need to have a text property as top line parameter (classic style - your example at the bottom) or a text block within a section block.

If you want to put to use blocks only (as you are asking) the section block is called text, not fields. fields is another type of section bock that has a different meaning.

So the correct syntax is:

[  
   {  
      "channel":"#tech-support",
      "username":"Form Response",
      "icon_emoji":":mailbox_with_mail:",
      "link_names":1,
      "blocks":[  
         {  
            "type":"section",
            "text":[  
               {  
                  "type":"mrkdwn",
                  "text":"*Name:*\n test"
               }
            ]
         }
      ]
   }
]

Also see here for the official documentation on it.

Blocks are very powerful, but can be complicated at times. I would recommend to use the message builder to try out your messages and check out the examples in the docu.

Upvotes: 2

Related Questions