1

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:

Slack Screenshot

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
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
  • What happens when you change "text":"Total Emails Sent Last 24 Hours:"'"${total}"'" to "text":"Total Emails Sent Last 24 Hours: ${total}" ? – Raman Sailopal Dec 17 '20 at 15:35
  • I will show the output in a question update so I can format it properly. At least it didn't give me "invalid payload" this time, so that's progress. – DevOpsSauce Dec 17 '20 at 15:48
  • Given your playbook is so small, why not take advantage of [`uri:`](https://docs.ansible.com/ansible/2.10/collections/ansible/builtin/uri_module.html) and [`| to_json`](https://docs.ansible.com/ansible/2.10/user_guide/playbooks_filters.html#formatting-data-yaml-and-json) in your playbook to construct a legal JSON payload and send it for free? – mdaniel Dec 17 '20 at 15:49
  • Because the output is far too large, and I sum the totals for 4 different servers. That's why I put the output in a file, then grabbed the "SentLast24Hours" portion of each, and added them together, then append that value to the end of the file. – DevOpsSauce Dec 17 '20 at 15:52
  • 1
    OK, try "text":"Total Emails Sent Last 24 Hours:'"${total}"' – Raman Sailopal Dec 17 '20 at 15:52
  • Invalid payload – DevOpsSauce Dec 17 '20 at 15:53
  • This worked though. "text":"Total Emails Sent Last 24 Hours:'${total}'", – DevOpsSauce Dec 17 '20 at 15:54

0 Answers0