-
Notifications
You must be signed in to change notification settings - Fork 0
/
inform_users_about_new_post.sh
executable file
·95 lines (79 loc) · 3.56 KB
/
inform_users_about_new_post.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
#
# Send an email to all members, that have the subscribe flag set, to inform them about the newest post on https://ksick.dev
print_usage () {
echo "This script sends an email to all members, that have the subscribe flag set, to inform them about the newest post on https://ksick.dev."
echo "* Arguments:"
echo " - ghost_api_key: Ghost uses a key to secure its API, you can find more information about it and how to generate it here: https://ghost.org/docs/api/v3/content/#key"
echo " - ghost_export_file: a file containing all information about a Ghost blog. Can be exported in the admin panel at \"Labs\" -> \"Export your content\""
echo "* Requirements:"
echo " - jq"
echo " - curl"
}
#######################################
# Reads in all members, that have the subscribe flag set, from the passed export file from ghost and generates a JSON containing their mail addresses
# and unsubscribe URLs. This JSON defines to whom the mail should be sent.
# Globals:
# destinations
# Arguments:
# ghost_export_file: the file containing information about the members
#######################################
get_all_destinations() {
jq_command='.db[0].data.members[] | select(.subscribed == 1)'
jq_command+='| { "Destination": { "ToAddresses": [.email] }, "ReplacementTemplateData": ("{ \"unsubscribeUrl\": \"https://ksick.dev/unsubscribe/?uuid=" + .uuid + "\"}") }'
destinations=$(cat $ghost_export_file | jq "$jq_command" | jq --slurp '.')
}
#######################################
# Fetches thew newest post from https://ksick.dev and generates a JSON containing information about the post. This JSON defines the variables that will
# be replaced in the template.
# Globals:
# default_template_data
# Arguments:
# ghost_api_key
#######################################
get_default_template_data() {
title_key="title"
url_key="url"
content_key="html"
post_data=$(curl -X GET "https://ksick.dev/ghost/api/v3/content/posts/?key=${ghost_api_key}&fields=${title_key},${url_key},${content_key}&limit=1")
post_title=$(jq -r ".posts[0].${title_key}" <<< $post_data | tr '"' '\"')
post_excerpt=$(echo $(jq -r ".posts[0].${content_key}" <<< $post_data) | awk -v FS="(<p>|</p>)" '{print $2}' | tr '"' '\"')
post_url=$(jq -r ".posts[0].${url_key}" <<< $post_data | tr '"' '\"')
default_template_data=$(jq -nc \
--arg title "$post_title" \
--arg excerpt "$post_excerpt" \
--arg url "$post_url" \
'{postTitle: $title, postExcerpt: $excerpt, postUrl: $url}' \
)
}
# check if input argument is given
if [[ $# -ne 2 ]] ; then
print_usage
exit
fi
# input arguments
ghost_api_key=$1
ghost_export_file=$2
# the destinations the mail should be sent to
destinations
# information about the newest blog post
default_template_data
# fetch all necessarey information
get_all_destinations
get_default_template_data
# generate a JSON containing information about the bulk email that will be sent out
bulk_mail_json=$(jq -nR \
--arg source "ksick.dev <[email protected]>" \
--arg template "NewPostTemplate" \
--arg config_set_name "NewPostConfig" \
--argjson destinations "$destinations" \
--arg default_template_data "$default_template_data" \
'{Source: $source, Template: $template, ConfigurationSetName: $config_set_name, Destinations: $destinations, DefaultTemplateData: $default_template_data}' \
)
template_file="bulk_mail_template.json"
# write the JSON to the template file
echo "$bulk_mail_json" > $template_file
# send out the email via AWS SES
aws ses send-bulk-templated-email --cli-input-json file://$template_file
# delete template file
rm $template_file