'Unable to post message via the Slack API - getting error 'no_text'

Why am I getting an error 'no_text' ?

The Json below is valid and is taken from the slack documentation example.

In bot.postMessage(channel, '', params) if I populate the second parameter (i.e. replacing ' ' with 'some_text) it prints 'some_text' but without the attachment.

bot.postMessage(channel, 'some_text', params) --> works but the attachment doesn't show up.

    const element = {
      "text": "Would you like to play a game?",
      "response_type": "in_channel",
      "attachments": [
          {
              "text": "Choose a game to play",
              "fallback": "If you could read this message, you'd be choosing something fun to do right now.",
              "color": "#3AA3E3",
              "attachment_type": "default",
              "callback_id": "game_selection",
              "actions": [
                  {
                      "name": "games_list",
                      "text": "Pick a game...",
                      "type": "select",
                      "options": [
                          {
                              "text": "Hearts",
                              "value": "hearts"
                          },
                          {
                              "text": "Global Thermonuclear War",
                              "value": "war"
                          }
                      ]
                  }
              ]
          }
      ]
  }

    console.log('JSON.stringify(element): '+JSON.stringify(element));
    params = {
      icon_emoji: ':r2:',
      attachments: JSON.stringify(element)
    }
    bot.postMessage(channel, '', params).always(function (data) {...}


Solution 1:[1]

The issue arises from the lack of a text field in the parameters that is passed to bot.PostMessage. Your params should be

params = {
  icon_emoji: ':r2:',
  text: "Would you like to play a game?",
  response_type: "in_channel",
  attachments: element
}

and the element now should start from the actual attachment

const element = [
      {
          "text": "Choose a game to play",
          "fallback": "If you could read this message, you'd be choosing something fun to do right now.",
          "color": "#3AA3E3",
          "attachment_type": "default",
          "callback_id": "game_selection",
          "actions": [
              {
                  "name": "games_list",
                  "text": "Pick a game...",
                  "type": "select",
                  "options": [
                      {
                          "text": "Hearts",
                          "value": "hearts"
                      },
                      {
                          "text": "Global Thermonuclear War",
                          "value": "war"
                      }
                  ]
              }
          ]
      }
  ]

Solution 2:[2]

I met the same issue and found the solution.
The problem is that if only attachments field is added into the payload, it will report no_text error. But if text field is added, slack message will only show the text content.

The solution:
When we want to display attachments, we need to add a basic blocks field instead of text field. Something like

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "bar"
            }
        }
    ],
    "attachments": [
        {
            "color": "#FF0000",
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "foo"
                    }
                }
            ]
        }
    ]
}

If putting the above payload into the Slack build kit, it will be a misleading. That's also why I stuck with the issue.

I would recommend to use chat.postMessage test to debug the payload. It will work like a charm.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 oscarz