I have a monitor API (for example: www.example.com/monitor ) well return system status in JSON format, for example:
{
"foo" : 1000,
"bar" : 100
}
I want to use curl to get system status from this API and pipe to Slack
The following is my current script.
status=$(curl "http://www.xample.com/monitor/status" )
slackWebHook="https://hooks.slack.com/services/xxxxx/xxxxx/xxxxx"
function sentSlack () {
json_template='{
channel: $channel,
username: $username,
text: $text,
icon_emoji: $icon_emoji,
attachments: [$attachments]
}'
jq -n --arg channel "#unicorn_log" \
--arg username "Kuasa Search Report" \
--arg text "${msg}"\
--arg icon_emoji "chart_with_upwards_trend" \
--arg attachments "${status}" "$json_template" |
curl -i -X POST --data-urlencode "$(</dev/stdin)" ${slackWebHook}
}
sentSlack
I always get invalid_payload , because the status be escaped.
{
\"foo\" : 1000,
\"bar\" : 100
}
How can I pass JSON to Slack payload correctly?
答案 0 :(得分:1)
--arg key value
treats value as a string. Since a string containing literal "
s needs to have escapes added to be valid in JSON, that's what jq
does.
--argjson key value
, by contrast, parses value
as JSON text; which appears to be what you want to do here.
Change --arg attachments "$status"
to --argjson attachments "$status"
.