-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-status-update
executable file
·99 lines (85 loc) · 2.28 KB
/
slack-status-update
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
#!/bin/bash -e
if [ "$(uname -a)" == "Darwin" ]; then
if ! command -v gdate >/dev/null; then
echo "gdate required, install via 'brew install coreutils'"
exit 1
fi
_date=gdate
else
_date=date
fi
CMD_LINK=$(readlink "${BASH_SOURCE[0]}")
CMD_DIR="$( cd "$(dirname "${CMD_LINK}")" >/dev/null 2>&1 && pwd )"
# shellcheck source=/dev/null
test -r "${CMD_DIR}/.env" && source "${CMD_DIR}/.env"
if [ -z "${SLACK_TOKEN}" ]; then echo "SLACK_TOKEN env var required!"; exit 1; fi
if [ -z "${SLACK_USER_ID}" ]; then echo "SLACK_USER_ID env var required!"; exit 1; fi
DND_ENABLED="false"
STATUS_EXP_MIN=60
function usage {
echo "Usage: $0 [lunch|workout|focus|meeting] -m 60 (optional, minutes status active) -d (optional, if supplied, enabled Do Not Disturb)"
echo " $0 clear"
exit 1
}
function rand_food {
food=(":pizza:" ":hamburger:" ":green_salad:" ":sandwich:" ":burrito:")
echo "${food[$RANDOM % ${#food[@]}]}"
}
STATUS="${1}"
if [ -z "${STATUS}" ]; then usage; fi
shift
while getopts ':m:d' options; do
case $options in
m) STATUS_EXP_MIN="${OPTARG}";;
d) DND_ENABLED="true";;
?) usage;;
esac
done
shift $((OPTIND - 1))
case "${STATUS}" in
clear)
status_text=""
status_emoji=""
;;
lunch)
status_text="Lunch"
status_emoji="$(rand_food)"
;;
workout)
status_text="Workout"
status_emoji=":weight_lifter:"
;;
focus)
status_text="Focus time"
status_emoji=":headphones:"
DND_ENABLED=true
;;
meeting)
status_text="Meeting"
status_emoji=":calendar:"
;;
*)
usage
;;
esac
status_exp=$(( $($_date +%s) + $(( STATUS_EXP_MIN * 60 )) ))
body=$(echo '{
"profile": {
"status_text": "'"${status_text}"'",
"status_emoji": "'"${status_emoji}"'",
"status_expiration": '"${status_exp}"'
}
}' | jq -r '.')
curl -s -X POST \
-H "Content-type: application/json; charset=utf-8" \
-H "Authorization: Bearer ${SLACK_TOKEN}" \
-H "X-Slack-User: ${SLACK_USER_ID}" \
-d "${body}" \
https://slack.com/api/users.profile.set | jq .
if [ "${DND_ENABLED}" == "true" ]; then
curl -s \
-H "Content-type: application/x-www-form-urlencoded" \
-H "X-Slack-User: ${SLACK_USER_ID}" \
-d "token=${SLACK_TOKEN}&num_minutes=${STATUS_EXP_MIN}" \
https://slack.com/api/dnd.setSnooze | jq .
fi