1

Update: I got this working and posted an answer.

I am trying to create a markdown block for a slack bot using jq inside of a bash script. Most of the tutorials I've found are for reading json but I am trying to create json.

I'm close but I'm still doing something wrong. This is the desired slack format, I've copied the relevant section below:

{
    "text": "Danny Torrence left a 1 star review for your property.",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Danny Torrence left the following review for your property:"
            }
        },

And here is a portion of my script:

FALLBACK_MESSAGE="TEST MESSAGE - $HOSTNAME"
FIRST_INNER_SECTION=$(jq -n --arg secType "mrkdwn" --arg textVal "Hi\nperson\n<@U12345789>" '{type: $secType, text: $textVal}')
FIRST_OUTER_SECTION=$(jq -n --arg secType "section" --arg textVal "$FIRST_INNER_SECTION" '{type: $secType, text: $textVal}')
echo $FIRST_INNER_SECTION
echo $FIRST_OUTER_SECTION
MY_STR=$(jq -n --arg text "$FALLBACK_MESSAGE" --arg blocks "$FIRST_OUTER_SECTION" '{text: $text, blocks: [$blocks]}');
echo $MY_STR
curl -X POST -H 'Content-type: application/json' --data "$MY_STR" https://hooks.slack.com/services/123456789

I am getting an invalid blocks format error. I think it's due to creating multiple strings and trying to combine them with jq (seeing lots of slashes and newlines). I tried using different flags besides -n but they didn't work. I am going to try creating a mega string now but would prefer to split it up like I've done. Any help here?

Update with Megastring working. Would still like to find a way to format this better.

JSON_STRING=$( jq -n --arg fallbackText "Fallback message" --arg sectionType "section" --arg markdownType "mrkdwn" --arg textType "test" '{text: $fallbackText, blocks: [{type: $sectionType, text: {type: $markdownType, text: $textType}}]}')
echo $JSON_STRING
curl -X POST -H 'Content-type: application/json' --data "$JSON_STRING" https://hooks.slack.com/services/12345678
Bix
  • 760
  • 8
  • 22

1 Answers1

2

Alright I figured it out. I'll leave this up in case it's helpful for anyone.

See this post for instructions to escape the markdown string

# Setup messages
fallback_message="TEST MESSAGE - $HOSTNAME"
markdown_message="TEST MESSAGE - $HOSTNAME \n Hi <@U12345678>\n\`\`\`Can we do a\nmultiline code block\`\`\`"

# Convert markdown message to correct format for jq parse
printf -v markdown_message_unescaped %b "$markdown_message"

# Create the json string
json_string=$( jq -nr \
    --arg jq_fallback_message "$fallback_message" \
    --arg jq_section_type "section" \
    --arg jq_markdown_type "mrkdwn" \
    --arg jq_markdown_message "$markdown_message_unescaped" \
    '{
        text: $jq_fallback_message, 
        blocks: [
            {
                type: $jq_section_type,
                text: {
                    type: $jq_markdown_type, 
                    text: $jq_markdown_message
                }
            }
        ]
    }')

# Echo and send to slack
echo $json_string
curl -X POST -H 'Content-type: application/json' --data "$json_string" https://hooks.slack.com/services/123456789
Bix
  • 760
  • 8
  • 22