-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathafk
executable file
·71 lines (61 loc) · 1.54 KB
/
afk
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
#!/bin/sh
dotenv () {
set -a
if [ -f .afkconfig ]; then
echo "[AFK] Reading .afkconfig from current folder..."
. .afkconfig
elif [ -f $HOME/.afkconfig ]; then
echo "[AFK] Reading .afkconfig from $HOME..."
. $HOME/.afkconfig
fi
set +a
}
can_start () {
if [[ -z "${AFK_BOT_TOKEN}" ]]; then
echo "[AFK] ERROR: Must provide AFK_BOT_TOKEN in environment" 1>&2
exit 1
fi
if [[ -z "${RECIPIENT_ID}" ]]; then
echo "[AFK] ERROR: Must provide RECIPIENT_ID in environment" 1>&2
exit 1
fi
}
send_message() {
HOSTNAME=$(hostname)
CURRENT_PATH=$(pwd)
echo "[AFK] Posting notification to Telegram Bot..."
curl -s --request POST \
--url https://api.telegram.org/bot$AFK_BOT_TOKEN/sendMessage \
--header 'content-type: application/json' \
--data "{
\"chat_id\":\"$RECIPIENT_ID\",
\"text\":\"<b>Hostname:</b> <pre>$HOSTNAME</pre>\n<b>Command outcome:</b> <em>$1</em>\n<b>Command:</b> <pre>$2</pre>\n<b>Path:</b> <pre>$CURRENT_PATH</pre>\",
\"parse_mode\":\"HTML\"
}" > /dev/null
}
dotenv
can_start
echo "[AFK] Starting AFK..."
if [ $# -eq 0 ]
then
echo "[AFK] Please input command.
Example usage:
afk [command]"
exit 1
fi
COMMAND="$*"
echo "[AFK] Command: $COMMAND"
echo "[AFK] ======================="
set -x
"$@"
RESULT=$?
set +x
echo "[AFK] ======================="
if [ $RESULT -eq 0 ]; then
echo "[AFK] Outcome: ✅ Success"
send_message "✅ Pass" "$COMMAND"
else
echo "[AFK] Outcome: ❌ Failed"
send_message "❌ Failed" "$COMMAND"
fi
echo "[AFK] Done with AFK."