-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (100 loc) · 4.13 KB
/
Copy pathmain.py
File metadata and controls
110 lines (100 loc) · 4.13 KB
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
from dotenv import load_dotenv
load_dotenv()
import os
import json
import time
from slack_sdk.errors import SlackApiError
def slack_request(func, **kwargs):
while True:
try:
return func(**kwargs)
except SlackApiError as e:
if e.response["error"] == "ratelimited":
retry_after = int(e.response.headers.get("Retry-After", 5))
print(f"Rate limited! Waiting {retry_after} seconds...")
time.sleep(retry_after)
else:
raise
from slack_sdk import WebClient
client = WebClient(token= os.environ.get("SLACK_BOT_TOKEN"))
all_messages = []
try:
with open("messages.json", "r") as f:
existing = json.load(f)
except FileNotFoundError:
existing = []
with open("trusted_users.json", "r") as u:
trusted_users = json.load(u)
def is_trusted(user_id, chanel_id):
if user_id in trusted_users.get("global", []):
return True
if user_id in trusted_users.get("chanel_id", []):
return True
return False
all_channels = []
channel_cursor = None
while True:
if channel_cursor:
channels_response = slack_request(client.conversations_list, cursor=channel_cursor)
else:
channels_response = slack_request(client.users_conversations)
all_channels.extend(channels_response["channels"])
time.sleep(1)
next_cursor = channels_response["response_metadata"]["next_cursor"]
if next_cursor:
channel_cursor = next_cursor
else:
break
for channel in all_channels:
channel_id = channel["id"]
cursor = None
while True:
try:
if cursor:
response = slack_request(client.conversations_history, channel=channel_id, cursor=cursor, oldest="1701388800")
else:
response = slack_request(client.conversations_history, channel=channel_id, oldest="1701388800")
except SlackApiError as e:
if e.response["error"] == "not_in_channel":
break
else:
raise
time.sleep(1)
for message in response["messages"]:
if "user" in message and "text" in message:
if is_trusted(message["user"], channel_id):
filtered = {"user": message["user"],
"text": message["text"],
"channel": channel_id,
"ts": message["ts"]
}
all_messages.append(filtered)
if message.get("reply_count", 0) > 0:
try:
thread_response = slack_request(client.conversations_replies, channel=channel_id, ts=message["ts"], oldest="1701388800")
for reply in thread_response["messages"][1:]:
if "user" in reply and "text" in reply:
if is_trusted(message["user"], channel_id):
filtered_reply = {
"user": reply["user"],
"text": reply["text"],
"channel": channel_id,
"ts": reply["ts"]
}
all_messages.append(filtered_reply)
except SlackApiError as e:
if e.response["error"] == "not_in_channel":
pass
else:
raise
next_cursor = (response.get("response_metadata") or {}).get("next_cursor")
if next_cursor:
cursor = next_cursor
else:
break
existing_ts = {msg["ts"] for msg in existing}
new_messages = [msg for msg in all_messages if msg["ts"] not in existing_ts]
all_saved = existing + new_messages
with open("messages.json", "w") as f:
json.dump(all_saved, f, indent=2)
print(f"Saved {len(new_messages)}")