问题
So I am trying to send a message to a specific slack channel using a secrets.json
file to specify the payloads for both messages and the web-hook URL. I am able to post things to the message but it does not load the message as a payload even though the payload variable is set correctly. How can I load message payloads within the curl command I am specifying?
Here is the code:
richardbarret@1152-MBP ~/Git/SalesforceCLI/Integration/Slack master ● ./automated_messages.sh ✔ 1488 10:48:42
https://hooks.slack.com/services/XXXXXXXXX/XXXXXXX/XXXXXX
Webhook Variable is Working.
invalid_payload% richardbarret@1152-MBP ~/Git/SalesforceCLI/Integration/Slack master ● cat automated_messages.sh ✔ 1489 10:48:59
#!/bin/bash
# ===========================================================
# Created By: Richard Barrett
# Organization: Mirantis
# Department: Customer Success Operation
# Purpose: Send Message to Slack Channel
# Date: 03/20/2020
# ===========================================================
# System Variables
webhook_url=$(cat secrets.json | jq ".slack_config.slack_target_url" | tr -d \")
echo $webhook_url
printf "Webhook Variable is Working.\n"
message_1=$(cat secrets.json | jq ".slack_messages.message_1" | tr -d \")
message_2=$(cat secrets.json | jq ".slack_messages.message_2" | tr -d \")
message_3=$(cat secrets.json | jq ".slack_messages.message_3" | tr -d \")
# Use Messages in this command syntax
# Example
# curl -X POST -H 'Content-type: application/json' --data '{"text":"TEST TEXT BODY"}' $webhook_url
# General Message:
curl -X POST -H 'Content-type: application/json' --data '{"text":"$message_1"}' $webhook_url
# Messages for Handover:
#curl -X POST -H 'Content-type: application/json' --data '{"text":"Handovers: https://mirantis.my.salesforce.com/XXXXXXXXXXXXXXX"}' $webhook_url
# Message for All Change Requests:
#curl -X POST -H 'Content-type: application/json' --data '{"text":"All Change Requests: https://mirantis.my.salesforce.com/XXXXXXXXXXXX"}' $webhook_url
# Message for Change Requests in Ready to Execute
# curl -X POST -H 'Content-type: application/json' --data '{"text":"All CRs in Ready to Execute:"}' $webhook_url
I fixed the Invalid Payload
because I forgot to use the "text":
option within the --data
so I know that is not the issue. When I run the above script all that get's posted it the following:
So I tried deleting the quotations around $message_1
and got the message:
Webhook Variable is Working.
invalid_payload%
I deleted the Webhook response because it has a slack token. The webhook works, but the messages are not loading in the payload curl for the --data for some odd reason.
The secrets.json
looks like this:
{
"slack_config": {
"slack_target_url": "https://hooks.slack.com/services/XXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXX"
},
"slack_messages": {
"message_1": "SLACK_MESSAGE_1 Testing Secrets Calls from secrets.json File",
"message_3": "SLACK_MESSAGE_3 Testing Secrets Calls from secrets.json File",
"message_2": "SLACK_MESSAGE_2 Testing Secrets Calls from secrets.json File"
}
}
回答1:
So many thanks to one of my buddies who is an SRE, he was able to point me in the right direction. When trying to read something from a secrets.json
it wasn't specifying the $message_1. But now if you replace the
--data '{"text":"$message_1"}'
with
--data "{\"text\":\"$message_1\"}"
you can then call the messages from within another file without exposing them. This makes any links you may need to put in the message more secure as well as they are not hardcoded to the message.
来源:https://stackoverflow.com/questions/60871796/trying-to-load-a-message-payload-from-a-json-file-in-bash-to-send-to-slack-chann