Question
In Flow Designer, the Slack step "Post to Channel" includes a Blocks field that allows blocks data to be sent to Slack. How do I format the content of this step to make sure the post to Slack succeeds?
Environment
All versions of xMatters
Answer
In Slack, blocks are visual components that can be stacked and arranged to create app layouts and improve the appearance and interactivity of messages.
To successfully send blocks data from Flow Designer to Slack, the data must consist of a valid JSON object that has been converted to a string.
Here's how to create suitable content for the Blocks field:
- Use Slack's online Block Kit Builder or your preferred tool to create a JSON object that conforms to the Slack Blocks specification.
- If you're using something other than the Block Kit Builder, make sure your JSON is valid (using an online validation tool, for example).
Your data is now ready to paste into the Blocks field of the Post to Channel step in Flow Designer.
Example
Stringifying a JSON object includes escaping any characters that will prevent Slack from accepting your Blocks code (such as converting \n to \\n) and collapsing any white space.
Here's an example JSON object from Slack's reference documentation for Blocks:
[
{
"block_id": "text1",
"type": "section",
"text": {
"type": "mrkdwn",
"text": "A message *with some bold text* and _some italicized text_."
}
}
]
And here's what it looks like after it has been stringified:
"[{\"block_id\":\"text1\",\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"A message *with some bold text* and _some italicized text_.\"}}]"
This stringified data is almost ready to paste into xMatters. Follow the steps below to get your data ready.
How to Prepare Your Data
To prepare your data for the xMatters "Slack - Post to Channel" Flow step:
- Start with suitable data, eg from https://app.slack.com/block-kit-builder
- Note that data from the Slack Block Kit Builder will be preceded by {"blocks": - this will be removed in step 6
- Validate the JSON data, eg with https://jsonlint.com/
- Paste the data into a text editor
- You can delete line breaks and whitespace if you want, but it's not necessary.
- Replace all \ (backslashes) with \\ (double backslashes).
- Replace all " with \" (ie, escape every double-quote with a backslash).
- Delete everything before the first [ (open square bracket).
- Delete everything after the last ] (open square bracket)
- Paste your data into the "Slack - Post to Channel" step in your workflow.
Here's an example of correctly-prepared data:
[{\"block_id\":\"text1\",\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"A message *with some bold text* and _some italicized text_.\"}}]
Comments
4 commentsPlease sign in to leave a comment.
These steps does not work.
Is there any change after this post ?
I am looking for ways to send blocks to slack via xMatters.
March 2022: instructions have been updated with field-tested steps and examples.
For someone else having trouble. With the updated instructions of removing the outer quotes and removing the outer JSON key "blocks" and the first curly braces which gets generated when using the Slack Block Kit builder.
Added step-by step instructions to prepare data for pasting into the "Slack - Post to Channel" Flow step in xMatters.