I am trying to make make send a Slack message using their blocks format. Problem is that I am having various nasty strings on input and the scenario keeps failing because of it. I need to strip various quotes, newlines and other things JSON does not support. The message I am trying to build is something like this, which works with simple inputs, but is very fragile. And I have several other instances of this problem in different scenarios.
~ Target JSON structure
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": ":some-emoji: New feedback from {{trim(1.`1`)}} - {{trim(1.`2`)}}.",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " 🗺️❓: More info: {{1.`3`}}\n👍: {{1.`5`}}\n🤔: {{1.`4`}}\n💬: {{1.`6`}}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "FYI <@USR123456>, <@USR789012>."
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Available actions:"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": ":other-emoji: open feedback portal (row {{1.`__ROW_NUMBER__`}})",
"emoji": true
},
"url": "https://some-url.com",
"action_id": "button-action"
}
},
{
"type": "divider"
}
]
}
I’ve looked through docs and have been Googling this since yesterday and the best practical approach seems to be to chain several replace functions and maybe a RegExp which is way too tedious work for such a simple task. Feels like I must be missing something.
What I have already tried
- Using chained replaces feels wrong and the readability is bad.
"blocks": [
…
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " 🗺️❓: Location, {{replace(1.`3`; \"\\n\"; \" \")}}\\n👍: {{replace(replace(replace(1.`5`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}\\n🤔: {{replace(replace(replace(1.`4`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}\\n💬: {{replace(replace(replace(1.`6`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}"
}
},
…
- Composing a string and transforming to JSON does not seem to work, the output is like this:
"{\n\t\"blocks\": [\n\t\t{\n\t\t\t\"type\": \"header\",\n\t\t\t\"text\": {\n\t\t\t\t\"type\": \"plain_text\",\n\t\t\t\t\"text\": \":emoji: New feedback from somebody@example.com.\",\n\t\t\t\t\"emoji\": true\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\t\"type\": \"section\",\n\t\t\t\"text\": {\n\t\t\t\t\"type\": \"mrkdwn\",\n\t\t\t\t\"text\": \"
Please tell me what is the best way to deal with this. Thank you!