Update Figured it out:
"text":"Total Emails Sent Last 24 Hours:'${total}'",
Thanks for the help.
Update: Per the suggestion in the comments, if I change the text portion of the JSON to
"text":"Total Emails Sent Last 24 Hours:${total}"
the invalid payload
goes away, however, the output in Slack is showing the $total
as a literal string:
Total Emails Sent Last 24 Hours:${total}
End Update
I'm attempting to automate a notification to check how many emails were sent in the last 24 hours via my AWS SES relays. It should post to a Slack channel, however, I keep getting invalid payload
upon executing the script. If I do not include the variable, and just put plain text, it shows up.
I've looked at this question, this question, and I'm unsure if this is relevant, because I am able to send the request without the variable by using plain text.
I changed my $total
variable to just "This is only a test"
, and this was the result:
The script runs an Ansible
playbook, writes to a file, then sums the outputs into a variable, and appends that value to the end of the file. Here is the script and playbook (tokens are fake of course):
Playbook:
---
# tasks file for get_ses_stats
- name: Get SES Send Statistics
shell: /usr/bin/aws ses get-send-quota
register: result
- debug: var=result.stdout_lines
Bash script:
#!/bin/bash
file="/tmp/ses_stats.txt"
#Remove the file to start over
[ -f $file ] && rm $file
#cd to Ansible playbook location
cd /home/myUserFolder/PhpStormProjects/ansible
#Run playbook and write to file
ansible-playbook get_ses_stats.yml | tee -a $file
echo -e "\n" >> $file
#Get the total, store in variable
total=$(cat /tmp/ses_stats.txt | grep -A 2 'SentLast24' | awk '{print $6}' | sed 's/SentLast24Hours//;s/\n/ /' | bc | sed 's!\.0*$!!' | awk 'NF{sum+=$1} END {print sum}')
#Append variable to end of the file
echo $total >> $file
#Post data to Slack
curl -X POST -H 'Content-type: application/json' --data '{
"attachments": [
{
"text":"Total Emails Sent Last 24 Hours:"'"${total}"'", <---- WHAT IS WRONG?
"mrkdwn_in":["text"],
"color":"#bfefff",
"title":"[AWS] Campaign Report"
}
]}' https://hooks.slack.com/services/FOO/BAR/FOOBARFOOBARFOOBAR