-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_slack.py
64 lines (53 loc) · 2 KB
/
import_slack.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
from src.config import slack_client, db
from src.fastapi import get_slack_users
thread = db.threads
async def import_messages():
users = await get_slack_users()
user_ids = [x["id"] for x in users["members"]]
messages = await slack_client.conversations_history(channel="C05PGS91WNS")
for message in messages.data["messages"]:
event = message
if event.get("subtype") == "channel_join":
continue
user_id = event["user"]
user_name = next(x["name"] for x in users["members"] if x["id"] == user_id)
text = event["text"]
# Change user ids in text to names
for _id in user_ids:
if _id in text:
name = next(x["name"] for x in users["members"] if x["id"] == _id)
text = text.replace(f"<@{_id}>", f"{name}")
if event.get("thread_ts") is None or event["thread_ts"] == event["ts"]:
await thread.insert_one(
{
"slack_thread_id": event["ts"],
"user_ids": [user_id],
"channel": "C05PGS91WNS",
"messages": [
{
"text": text,
"user_id": user_id,
"name": user_name,
}
],
}
)
else:
rec = await thread.find_one({"slack_thread_id": event["thread_ts"]})
filter_push = {
"messages": {
"text": text,
"user_id": user_id,
"name": user_name,
},
}
update = {"$push": filter_push}
if user_id not in rec["user_ids"]:
filter_push["user_ids"] = user_id
await thread.update_one(
{"slack_thread_id": event["thread_ts"]},
update,
)
if __name__ == "__main__":
import asyncio
asyncio.run(import_messages())