forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-slack-wfh-status.sh
executable file
·76 lines (63 loc) · 2.06 KB
/
set-slack-wfh-status.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
#!/bin/bash
# API: https://api.slack.com/methods/users.profile.set
# Parameters
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Set WFH Status in Slack
# @raycast.mode silent
# Optional parameters:
# @raycast.packageName Slack
# @raycast.icon images/slack-logo.png
# Documentation:
# @raycast.description Set your status in Slack to WFH or WFO depending on your WiFi
# @raycast.author alongat
# @raycast.authorURL https://github.com/alongat
# Configuration
# To create a new API token, do the following:
# 1. Create a new Slack app at https://api.slack.com/authentication/basics
# 2. Add `users.profile:write` to the user token scopes
# 3. Install the app to your workplace
# 4. Insert your OAuth access token below
# 5. Fill you OFFICE_WIFI + HOME_WIFI - this will set different emoji when WFH vs WFO
# SLACK Token
API_TOKEN="XXXX"
# Minutes until the the status will expire (Empty string will not expire the status)
STATUS_EXPIRATION_IN_MINUTES="540" # 9 hours
CURRENT_UNIX_TIME=$(date +%s)
STATUS_EXPIRATION=$(($CURRENT_UNIX_TIME + ($STATUS_EXPIRATION_IN_MINUTES * 60)))
# Define home and office WiFi
HOME_WIFI="HOME-WIFI"
OFFICE_WIFI="OFFICE-WIFI"
# Main program
if [[ -z "$API_TOKEN" ]]
then
echo "No API token provided"
exit 1
fi
wifi_name=`/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk -F: '/ SSID/{print $2}'`
if [[ "$wifi_name" == " $HOME_WIFI" ]]; then
STATUS_EMOJI=":house_with_garden:"
TEXT="WFH"
elif [[ "$wifi_name" == " $OFFICE_WIFI" ]]; then
STATUS_EMOJI=":office:"
TEXT="WFO"
fi
RESPONSE=$(
curl \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $API_TOKEN" \
--request POST \
--data "{ 'profile': { 'status_text': '$TEXT', 'status_emoji': '$STATUS_EMOJI', 'status_expiration': $STATUS_EXPIRATION } }" \
--silent \
--show-error \
--fail \
"https://slack.com/api/users.profile.set"
)
if [[ "$RESPONSE" =~ "error" ]]
then
echo "Failed to set status in Slack"
exit 1
else
echo "Status set to $STATUS_EMOJI : $TEXT"
exit 0
fi