Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message notifications produce "This content can't be displayed"

Tags:

json

slack-api

I'm sending messages into Slack via a web hook. Message notifications using a single "text" JSON element produce the correct notification text with the Slack app for OS X and iOS. The message text is also correct in the channel. However, when using the new "blocks" method, the notification text becomes "This content can't be displayed". The message does correctly show up in the channel. It's just the notification that is not working.

According to https://api.slack.com/messaging/composing/layouts the text in the first block is used for the notification:

When you're using blocks in your message payload, the text field becomes a fallback message displayed in notifications.

Without using the blocks style, this works:

curl -s -k -X POST https://hooks.slack.com/services/${webhook} -H 'Content-type: application/json' --data @- << EOF
{
  "text": "${message}"
}
EOF

If you generate a simple blocks style equivalent using the block builder app at https://api.slack.com/tools/block-kit-builder by clearing out any existing code and then adding a Section block you will have something like this:

[
    {
        "type": "section",
        "text": {
            "type": "plain_text",
            "text": "This is a plain text section block.",
            "emoji": true
        }
    }
]

Now at the bottom of the page, enter your channel and click "Send to Slack". The result is that the text arrives in the channel correctly, however the notification that pops up contains the text "This content can't be displayed".

At first I thought that my own block style code was incorrect, however, this code that is generated by the block kit builder app does not work either which leads me to believe there is a problem/bug with Slack itself.

Or is there something that I have overlooked?

Thank you

like image 201
CSR Avatar asked May 26 '19 22:05

CSR


3 Answers

There are multiple "text"properties in the message structure. I believe the doc is referring to the top-level one outside all blocks definitions.

Example:

{
  "channel": "C1H9RESGL",
  "text": "Text here for notifications",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "Danny Torrence left the following review for your property:"
      }
    }
  ]
}
like image 134
Erik Kalkoken Avatar answered Oct 23 '22 12:10

Erik Kalkoken


If you're using the slack api .chat_postMessage() python method to post blocks content you can get around the "This content can't be displayed" message by doing the following.

client = slack.WebClient(token=SLACK_API_KEY)

review_message = [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "Danny Torrence left the following review for your property:"
      }
    }
  ]

client.chat_postMessage(channel=f'@{user_id}', text="New Review", blocks=review_message)

I know this is sort of outside of the scope here since the original poster used curl. But its not immediately clear how to get around this same problem if you are using the python .chat_postMessage() method.

like image 1
BlueSquare23 Avatar answered Oct 23 '22 13:10

BlueSquare23


From the documentation:

When you're using blocks in your message payload, the top-level text field becomes a fallback message displayed in notifications. Blocks should define everything else about the desired visible message.

Notification text needs to be added as a top-level "text" field

{
  // ...
  "text": "Content for message preview in notifications",
  "blocks": [
    // ... Content for message visible in app

Unfortunately their block kit builder preview experience flags this config with a warning ⚠️ implying bad syntax, but it is the correct/valid way to configure notification text for block kit messages. The documentation (linked above) shows this configuration in an example payload, but when you click preview to view the code in the block kit builder it prefills the builder with only the blocks array sliced from the documentation's example payload.

(Tried to add this to the accepted answer but its edit queue is full.)

like image 1
Allison Avatar answered Oct 23 '22 12:10

Allison