【问题标题】:Create jq json for slack bot为 slack bot 创建 jq json
【发布时间】:2021-05-04 18:07:31
【问题描述】:

更新:我得到了这个工作并发布了一个答案。

我正在尝试在 bash 脚本中使用 jq 为 slack 机器人创建一个降价块。我发现的大多数教程都是用于阅读 json,但我正在尝试创建 json。

我很接近,但我仍然做错了什么。这是desired slack format,我复制了下面的相关部分:

{
    "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:"
            }
        },

这是我的脚本的一部分:

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

我收到无效的块格式错误。我认为这是由于创建了多个字符串并试图将它们与 jq 结合起来(看到很多斜杠和换行符)。我尝试使用除 -n 之外的其他标志,但它们不起作用。我现在将尝试创建一个巨型字符串,但更愿意像我所做的那样将其拆分。这里有什么帮助吗?

更新 Megastring 工作。仍然想找到一种更好地格式化它的方法。

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

【问题讨论】:

    标签: bash ubuntu jq webhooks slack


    【解决方案1】:

    好吧,我想通了。我会留下这个,以防它对任何人有帮助。

    请参阅this post 以获取转义降价字符串的说明

    # 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
    

    【讨论】:

    • 你使用 printf 的部分救了我。没有它,换行符只是作为字符发送。谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 2017-09-24
    • 2017-11-05
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多