forked from MichaelSteurer/slack-status-icon-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_updater.py
66 lines (49 loc) · 1.63 KB
/
profile_updater.py
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
import json
import os
import requests
import sys
import urllib.parse
import yaml
URL = None
ICONS = [":slightly_smiling_face:", ":upside_down_face:"]
def set_credentials():
token = ""
root_directory = os.path.dirname(os.path.realpath(__file__))
with open(root_directory + "/credentials.yaml", 'r') as ymlfile:
token = yaml.load(ymlfile).get("token")
if not token:
print("ERROR: Token not set (see credentials.yaml)")
sys.exit(1)
global URL
URL = "https://slack.com/api/{method}?token=" + token
authenticated = check_authentication()
if not authenticated:
print("ERROR: Token '{}' is wrong (see credentials.yaml)").format(token)
sys.exit(1)
def main():
set_credentials()
current_icon = get_status_icon()
current_index = ICONS.index(current_icon) if current_icon in ICONS else 0
next_index = (current_index + 1) % len(ICONS)
next_icon = ICONS[next_index]
set_status_icon(next_icon)
def check_authentication():
url = URL.format(method="auth.test")
response = requests.get(url)
content = json.loads(response.content.decode())
return content.get("ok")
def set_status_icon(icon):
url = URL.format(method="users.profile.set")
profile = {
"status_emoji": icon
}
url += '&profile=' + urllib.parse.quote(json.dumps(profile))
requests.get(url)
return get_status_icon()
def get_status_icon():
url = URL.format(method="users.profile.get")
response = requests.get(url)
response = json.loads(response.content.decode())
return response.get("profile", {}).get("status_emoji")
if __name__ == "__main__":
main()