node.jsslackslack-apislack-block-kit

I am getting invalid_blocks_format while trying to post a message to slack


I am trying to post a message to slack using node.js but I got invalid_blocks_format error. In the slack API reference says that: this error occurs because of invalid JSON or wrong Block KIT syntax. I have checked both and everything seems correct. An example of my payload:

  {
    "channel": "CHANNELID",
    "blocks": [
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": "*Someone* just replied"
        }
      },
      {
        "type": "section",
        "fields": [
          {
            "type": "mrkdwn",
            "text": "*Email:*\n<https://example.com/some-url?query-param=value|someone@email.com>"
          },
          {
            "type": "mrkdwn",
            "text": "*Campaign:*\n<https://example.com/some-url|Name - Some value>"
          },
          {
            "type": "mrkdwn",
            "text": "*Replying To:*\nanotheremail.com"
          },
          {
            "type": "mrkdwn",
            "text": "*Assigned To:*\nName Surname"
          }
        ]
      },
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": ">Stop\n>\n>On Aug 8, 2022, at 6:40 PM, Sender <sender@email.com> wrote:\n>\n> Hi Someone,\n>\n>My name is Name from A company.\n>\n>Some content\n> \n>Content continues(https://blog.example.com/ulr?query-param=value) Content. \n>Would you like to book a demo?(https://example.com/url) \n>Let me know and I’ll schedule it.\n>\n>\n>Name Surname\n>Customer Success Manager\n>Company Name\n>\n> \n>\n>\n>\n>CONFIDENTIALITY NOTICE: This message, including any attachments, is the property of Company name Ltd., its affiliates and/or subs"
        }
      },
      {
        "type": "actions",
        "elements": [
          {
            "type": "button",
            "text": {
              "type": "plain_text",
              "text": "View Recipient"
            },
            "action_id": "view",
            "url": "https://example.com/view?query=asd",
            "style": "primary"
          },
          {
            "type": "static_select",
            "placeholder": {
              "type": "plain_text",
              "text": "Set lead status"
            },
            "action_id": "set-lead-status",
            "options": [
              {
                "text": {
                  "type": "plain_text",
                  "text": "Open Lead"
                },
                "value": "env=prod;recipientID=1234567;leadStatus=Open"
              },
              {
                "text": {
                  "type": "plain_text",
                  "text": "Ignore Lead"
                },
                "value": "env=prod;recipientID=1234567;leadStatus=Ignored"
              },
              {
                "text": {
                  "type": "plain_text",
                  "text": "Lost Lead"
                },
                "value": "env=prod;recipientID=1234567;leadStatus=Lost"
              },
              {
                "text": {
                  "type": "plain_text",
                  "text": "Won Lead"
                },
                "value": "env=prod;recipientID=1234567;leadStatus=Closed"
              }
            ]
          },
          {
            "type": "button",
            "text": {
              "type": "plain_text",
              "text": "Pause Recipient"
            },
            "action_id": "pause",
            "value": "env=prod;recipientID=1234567;"
          },
          {
            "type": "button",
            "text": {
              "type": "plain_text",
              "text": "Unsubscribe"
            },
            "action_id": "unsubscribe",
            "value": "env=prod;recipientID=1234567;",
            "style": "danger",
            "confirm": {
              "title": {
                "type": "plain_text",
                "text": "Unsubscribe recipient"
              },
              "text": {
                "type": "plain_text",
                "text": "Are you sure you want to unsubscribe email@address.com?"
              },
              "confirm": {
                "type": "plain_text",
                "text": "Yes"
              },
              "deny": {
                "type": "plain_text",
                "text": "Cancel"
              }
            }
          }
        ]
      }
    ],
   "token": "token-value"
  }

Also when I try to post this over https://app.slack.com/block-kit-builder, message posted without any problem. What can be the problem and how can I fix it?

Thanks for any help/suggestion.

UPDATE

I figured out the problem but not the solution.

Problem is url's with more than one query parameters. If any text has a link like;

https://example.com/url?param1=value1&param2=value2

Sources suggested to encode texts but if I do that, slack prints it without decode it. So sent message is not readable. I am still open to suggestions.


Solution

  • I was using an old version of slack lib on node and messages contain ampersand (&) caused the problem like links with more than one query parameters. Updating library solved my problem.