-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_react.py
executable file
·377 lines (304 loc) · 11.1 KB
/
slack_react.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python
# coding: utf-8
import argparse
import datetime
import json
import logging
import os
import re
import shutil
import sys
from typing import Dict, List
import slack_sdk
from appdirs import user_cache_dir
from rich.logging import RichHandler
APP_NAME = "slack-react"
LOGGER = logging.getLogger(__name__)
# mapping of alphabets to their corresponding emoji names
EMOJI_MAPPING = {
"a": ["a", "alphabet-white-a", "alphabet-yellow-a"],
"b": ["b", "alphabet-white-b", "alphabet-yellow-b"],
"c": ["alphabet-white-c", "alphabet-yellow-c"],
"d": ["alphabet-white-d", "alphabet-yellow-d"],
"e": ["alphabet-white-e", "alphabet-yellow-e", "e-mail"],
"f": ["alphabet-white-f", "alphabet-yellow-f"],
"g": ["alphabet-white-g", "alphabet-yellow-g"],
"h": ["alphabet-white-h", "alphabet-yellow-h"],
"i": ["alphabet-white-i", "alphabet-yellow-i"],
"j": ["alphabet-white-j", "alphabet-yellow-j"],
"k": ["alphabet-white-k", "alphabet-yellow-k"],
"l": ["alphabet-white-l", "alphabet-yellow-l"],
"m": ["alphabet-white-m", "alphabet-yellow-m"],
"n": ["alphabet-white-n", "alphabet-yellow-n"],
"o": ["alphabet-white-o", "alphabet-yellow-o", "o"],
"p": ["alphabet-white-p", "alphabet-yellow-p"],
"q": ["alphabet-white-q", "alphabet-yellow-q"],
"r": ["alphabet-white-r", "alphabet-yellow-r"],
"s": ["alphabet-white-s", "alphabet-yellow-s"],
"t": ["alphabet-white-t", "alphabet-yellow-t"],
"u": ["alphabet-white-u", "alphabet-yellow-u"],
"v": ["alphabet-white-v", "alphabet-yellow-v"],
"w": ["alphabet-white-w", "alphabet-yellow-w"],
"x": ["alphabet-white-x", "alphabet-yellow-x"],
"y": ["alphabet-white-y", "alphabet-yellow-y"],
"z": ["alphabet-white-z", "alphabet-yellow-z"],
"0": ["zero", "zero"],
"1": ["one", "one"],
"2": ["two", "two"],
"3": ["three", "three"],
"4": ["four", "four"],
"5": ["five", "five"],
"6": ["six", "six"],
"7": ["seven", "seven"],
"8": ["eight", "eight"],
"9": ["nine", "nine"],
"?": ["alphabet-white-question", "alphabet-yellow-question"],
"!": [
"exclamation",
"bangbang",
"gray_exclamation",
"alphabet-white-exclamation",
"alphabet-yellow-exclamation",
],
"-": ["heavy_minus_sign", "wavy_dash"],
" ": [
"black_small_square",
"white_small_square",
"small_orange_diamond",
"small_blue_diamond",
"black_medium_square",
"white_medium_square",
],
}
def parse_args():
# create the top-level parser
parser = argparse.ArgumentParser()
parser.add_argument(
"-d", "--debug", action="store_true", default=False, help="Debug mode"
)
parser.add_argument("-c", "--channel", help="Channel name")
parser.add_argument(
"-t",
"--token",
default=os.environ.get("SLACK_USER_OAUTH_TOKEN"),
help="Slack OAUTH Token",
)
parser.add_argument("-m", "--message", help="Slack message to react to")
parser.add_argument(
"-r",
"--remove",
action="store_true",
help="Remove reactions from message",
)
# add the positional argument for the message
parser.add_argument(
"reaction", nargs="?", help="the message to be converted to emojis"
)
return parser.parse_args()
def message_to_emoji_list(message):
# create a list to hold the emojis
emojis = []
# create a dictionary to hold the index of the next emoji to use for
# each character
next_emoji_index = {}
# iterate over each character in the message
for char in message:
# convert the character to lowercase
char = char.lower()
# if the character is in the mapping
if char not in EMOJI_MAPPING:
LOGGER.debug(
"Skipping character '%s' since there is no mapping for it",
char,
)
continue
# if the character is not in next_emoji_index, this is the first
# time we've seen it
if char not in next_emoji_index:
next_emoji_index[char] = 0
# get the next emoji for this character
emoji = EMOJI_MAPPING[char][next_emoji_index[char]]
LOGGER.debug("Converted '%s' to '%s'", char, emoji)
# add the emoji to the list
emojis.append(emoji)
# update the index of the next emoji to use for this character
next_emoji_index[char] = (next_emoji_index[char] + 1) % len(
EMOJI_MAPPING[char]
)
return emojis
def invalidate_cache():
cache_dir = user_cache_dir(APP_NAME)
if os.path.exists(cache_dir) and os.path.isdir(cache_dir):
shutil.rmtree(cache_dir)
def get_cache_age(
filename: str = "channel_cache.json",
):
# Determine the full path to the cache file
cache_dir = user_cache_dir(APP_NAME)
metadata_filepath = os.path.join(
cache_dir, filename.replace(".json", ".metadata.json")
)
try:
with open(metadata_filepath, "r") as f:
metadata = json.load(f)
created = metadata.get("created")
if not created:
return
return datetime.datetime.now() - datetime.datetime.fromtimestamp(
created
)
except (FileNotFoundError, json.JSONDecodeError):
return
def is_cache_valid(filename: str = "channel_cache.json", max_age: int = 12):
age = get_cache_age(filename)
if not age:
return False
return age < datetime.timedelta(hours=max_age)
def update_channel_cache(
content: List[Dict],
filename: str = "channel_cache.json",
):
# Determine the full path to the cache file
cache_dir = user_cache_dir(APP_NAME)
os.makedirs(cache_dir, exist_ok=True)
filepath = os.path.join(cache_dir, filename)
with open(filepath, "w") as f:
json.dump(content, f)
ts = int(datetime.datetime.now().timestamp())
metadata = {"created": ts}
metadata_filepath = os.path.join(
cache_dir, filename.replace(".json", ".metadata.json")
)
with open(metadata_filepath, "w") as f:
json.dump(metadata, f)
def load_channel_cache(filename: str = "channel_cache.json") -> List[Dict]:
# Determine the full path to the cache file
cache_dir = user_cache_dir(APP_NAME)
filepath = os.path.join(cache_dir, filename)
try:
with open(filepath, "r") as f:
return json.load(f)
except FileNotFoundError:
return []
def smart_cache(
client: slack_sdk.WebClient,
filename: str = "channel_cache.json",
):
if is_cache_valid(filename):
LOGGER.info("Using cached channel list")
return load_channel_cache(filename)
else:
LOGGER.info("Cache invalid, fetching channel list")
invalidate_cache()
content = get_all_channels(client)
update_channel_cache(content, filename)
return content
def get_user_id(client):
response = client.auth_test()
# the user ID is in the 'user_id' field of the response
return response["user_id"]
def get_all_channels(
client: slack_sdk.WebClient, page_size: int = 500
) -> List[Dict]:
channels = []
types = ["private_channel", "public_channel", "mpim", "im"]
result = client.conversations_list(
types=types,
limit=page_size,
)
if not isinstance(result.get("channels"), list):
raise ValueError("Invalid response from Slack API")
channels.extend(result.get("channels", []))
next_cursor = result.get("response_metadata", {}).get("next_cursor")
while next_cursor:
result = client.conversations_list(
limit=page_size, types=types, cursor=next_cursor
)
next_cursor = result.get("response_metadata", {}).get("next_cursor")
channels.extend(result.get("channels", []))
return channels
def get_channel_id(client, channel_name):
# the channels are in the 'channels' field of the response
channels = smart_cache(client)
# iterate over the channels to find the one with the given name
for channel in channels:
if channel["name"] == channel_name:
return channel["id"]
def find_matching_message(client, channel_id, regex):
# get the history of the channel
response = client.conversations_history(channel=channel_id)
# TODO Get messages from threads
# the messages are in the 'messages' field of the response
messages = response["messages"]
# create a regex object
pattern = re.compile(regex)
# iterate over the messages in reverse order
for message in reversed(messages):
# check if the message matches the regex
if pattern.search(message["text"]):
# return the matching message
return message
# if no matching message was found, return None
return None
def remove_reactions(client, channel_id, timestamp):
LOGGER.info("Removing all reactions from message")
response = client.reactions_get(channel=channel_id, timestamp=timestamp)
# the reactions are in the 'message'->'reactions' field of the response
reactions = response["message"].get("reactions", [])
# iterate over the reactions and remove each one
for reaction in [
x.get("name")
for x in reactions
if get_user_id(client) in x.get("users", [])
]:
LOGGER.info("Removing reaction: '%s'", reaction)
client.reactions_remove(
channel=channel_id, timestamp=timestamp, name=reaction
)
def add_reactions(client, channel_id, timestamp, message):
LOGGER.info("Adding reactions to message so that is spells '%s'", message)
for reaction in message_to_emoji_list(message):
# react to a message
LOGGER.info("Adding reaction to message: '%s'", reaction)
client.reactions_add(
channel=channel_id, timestamp=timestamp, name=reaction
)
def main():
args = parse_args()
logging.basicConfig(
level=logging.DEBUG if args.debug else logging.INFO,
format="%(message)s",
datefmt="[%H:%M:%S]",
handlers=[RichHandler(rich_tracebacks=True)],
)
# create a client instance
client = slack_sdk.WebClient(token=args.token)
channel_id = get_channel_id(client, args.channel)
if not channel_id:
LOGGER.error(f"Channel '{args.channel}' not found")
return 1
response = client.conversations_history(channel=channel_id)
target_message = None
if args.message:
message = find_matching_message(client, channel_id, args.message)
if not message:
LOGGER.error("Message not found")
return 1
ts = message["ts"]
target_message = message["text"]
else:
# Default to last message
message = response.get("messages", [{"ts": None}])[0]
target_message = message["text"]
ts = message["ts"]
if not ts:
LOGGER.error("No messages found")
return 1
LOGGER.debug("Target message: %s", message)
LOGGER.info("Target message: %s", target_message)
remove_reactions(client, channel_id, ts)
if args.reaction and not args.remove:
add_reactions(client, channel_id, ts, args.reaction)
return 0
if __name__ == "__main__":
sys.exit(main())