无法构建自定义 Slack 通知

问题描述

经过大量研究,我发现了这个 .sh 脚本,它允许我向 Slack 频道发送非常简单的个性化通知(我未能将其发送给特定成员)。 我面临的问题是格式化通知消息的问题。 Slack 站点上提供的用于格式化通知标签无法正常工作(我不知道如何使用它们!)。 必须在 Rundeck 作业每次执行结束时“从头开始”构建通知。 Rundeck 的“失败”和“成功通知不适合用户

这是 sh 脚本:

#!/bin/bash
​#Usage: slackpost <channel> <message>
slackhost="https://hooks.slack.com/services"
token="XXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXX"
slack_username="Someone-who-loves-you"
slack_icon="smile"​
channel=$1
if [[ $channel == "" ]]
then
    echo "No channel specified"
    exit 1
fi
text="$2"

if [[ $text == "" ]]
then
    echo "No text specified"
    exit 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"#$channel\",\"username\":\"${slack_username}\",\"icon_emoji\":\":${slack_icon}:\",\"text\": \"$escapedText\"}"
curl -s -d "payload=$json" "$slackhost/$token"

变量 $text 填充了 .txt 文件 (myFileText) 的内容

myFileText 内容如下:

text="",{
    blocks=[
        {
            "type": "section","text": {
                "type": "mrkdwn","text": "Hello World! <@UNVD64N02> :tada: \n\n - a \n-b"
            }
        }
    ]
}
{
    "blocks": [
        {
            "type": "section","text": "Hello,Assistant to the Regional Manager John Doe! *Martin Scott* wants to kNow where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
            }
        }
    ]
}

.sh 脚本运行如下:

sh slack_post.sh "channel-receiving-the-notification" "$(< myFileText)"

结果我有

invalid_payload

任何想法都会有所帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)