-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack_mail.bsh
executable file
·111 lines (95 loc) · 3.69 KB
/
slack_mail.bsh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
jqcmd=""
if [ $# -eq 2 ]; then
jqcmd=$2
echo "jq command path is $2"
else
jqcmd=jq
fi
resources_path=$1
# Check if the config file exists
if [ ! -f $resources_path/config.json ]; then
echo "Config file not found, please read README.md on github"
exit 1;
fi
# Check if the token file exists
if [ ! -f $resources_path/resources/token.json ]; then
echo "Token file not found, please authorize first."
exit 2;
fi
gmail_mess="https://www.googleapis.com/gmail/v1/users/me/messages"
function read_messages() {
access_token=$(cat $resources_path/resources/token.json | $jqcmd -r '.access_token')
request="$gmail_mess?access_token=$access_token&includeSpamTrash=false&q=in:inbox%20is:unread"
response=$(curl -s $request)
# Check if there are emails, if not exit
if echo $response | $jqcmd -e 'has("resultSizeEstimate")' >/dev/null; then
resultSize=$(echo $response | $jqcmd -r '.resultSizeEstimate')
if [ $resultSize = 0 ]; then
exit 0;
fi
fi
# Check if access token has expired
if echo $response | $jqcmd -e 'has("error")' >/dev/null; then
# Request a new access token
gmail_url="https://www.googleapis.com/oauth2/v4/token"
refresh_token=$(cat $resources_path/resources/token.json | $jqcmd -r '.refresh_token')
form_data="refresh_token=$refresh_token&client_id=$client_id&client_secret=$client_secret&grant_type=refresh_token"
r_response=$(curl -s --data $form_data $gmail_url)
access_token=$(echo $r_response | $jqcmd '.access_token')
token_type=$(echo $r_response | $jqcmd '.token_type')
expires_in=$(echo $r_response | $jqcmd '.expires_in')
token_file='{"access_token":'$access_token',"token_type":'$token_type',"expires_in":'$expires_in',"refresh_token": "'$refresh_token'"}'
echo $token_file > "$resources_path/resources/token.json"
echo $(read_messages)
else
# Return the messages id
echo $response | $jqcmd -r '.messages | .[].id'
fi
}
# Function for checking if the email notification was sent
function has_notified() {
for u in $unread
do if [ $1 = $u ]; then
echo true
fi done
echo false
}
# Function for sending and formatting the slack post
function slack_post() {
access_token=$(cat $resources_path/resources/token.json | $jqcmd -r '.access_token')
request="$gmail_mess/$1?access_token=$access_token&format=minimal"
snippet=$(curl -s $request | $jqcmd '.snippet')
snippet=$(echo $snippet | sed 's/'/_/g')
# Send the snippet to slack
json='{"text":"*You received a new email!*",
"attachments":[{"text":'$snippet',
"fallback": "Something went wrong, cannot display the email"},
{"text":"Read here: https://mail.google.com/mail/#inbox/'$1'"}]}'
curl -s -X POST -H 'Content-type: application/json' --data "$json" $slack
}
# Read the config json
client_id=$(cat $resources_path/config.json | $jqcmd -r '.client_id')
client_secret=$(cat $resources_path/config.json | $jqcmd -r '.client_secret')
slack=$(cat $resources_path/config.json | $jqcmd -r '.slack_hook')
echo "Loaded config with client_id: $client_id"
echo "client_secret: $client_secret"
echo "slack hook: $slack"
# Load the previous unread emails
touch "$resources_path/resources/unread_m.json" || { echo "Cannot write to file" >&2; exit 1;}
unread=$(cat $resources_path/resources/unread_m.json | $jqcmd -r ".messages[]")
# Try to send the email on slack
messages=$(read_messages)
for mess in $messages
do if ! $(has_notified $mess) ; then
slack_post $mess
fi done
# Save the current unread message for the next check
unread="{\"messages\":["
for m in $messages
do
unread+="\"$m\","
done
unread+="\"EOF\"]}"
echo $unread > $resources_path/resources/unread_m.json
echo "saved unread messages in $resources_path/resources/unread_m.json"