-
Notifications
You must be signed in to change notification settings - Fork 0
/
clap_utils.py
153 lines (133 loc) · 4.56 KB
/
clap_utils.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
import json
import logging
import spacy
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s",
)
# conver
def clap_usermap(name=None, user=None):
f = open("usermap.json")
usermap = json.load(f)
# lookup user, return name
if user:
name = usermap[user]
# lookup name, return user
if name:
user = [k for k, v in usermap.items() if v == name][0]
# return username wrapped in <@ and >
if name:
mention = f"<@{user}>"
return (name, user, mention)
def slack_msg_parser(body):
# finder
# print(body)
# ignore if the message is from a bot
# clatrap is B04PE7UMAB1
if "bot_id" in body["event"]:
logging.info(f"Bot detected:: {body['event']['bot_id']}")
bot = True
else:
bot = False
try:
if body["event"]["message_changed"]:
logging.info("janky subtype detected")
subtype = True
except:
subtype = False
# do something if message is an image
if "files" in body["event"]:
logging.info(f"Image detected:: {body['event']['files'][0]['url_private_download']}")
# get the url
image_url = body["event"]["files"][0]["url_private_download"]
else:
image_url = None
# do something if from_url is in body
if subtype: # jank
try:
link_url = body["event"]["message"]["attachments"][0]["from_url"]
logging.info(f"Link detected:: {link_url}")
except:
link_url = None
else:
link_url = None
# get sentby
if "user" in body["event"]:
name, user, mention = clap_usermap(user=body["event"]["user"])
sentby = name
logging.info(f"Sentby:: {sentby}")
else:
sentby = None
# do something if text in body
if "text" in body["event"] and not subtype:
# drop empty messages
if len(body["event"]["text"]) == 0:
text = None
else:
text = body["event"]["text"]
logging.info(f"Text detected:: {body['event']['text']}")
# split text into a list
text = text.split()
# replace all mentions with the user's name, but leave the other text intact
for i in text:
if i.startswith("<@"):
name, user, mention = clap_usermap(user=i[2:-1])
if name == "clapvontrapp":
# prune index of clapvontrapp
text.pop(text.index(i))
else:
text[text.index(i)] = name
# convert text back to a string
text = " ".join(text)
# capatalize the first letter of the message
if len(text) > 0:
if not text[0].isupper():
text = text[0].upper() + text[1:]
else:
text = None
logging.info(f"Text cleaned:: {text}")
else:
text = None
logging.info(f"Message parsed:: {sentby}, {text}, {image_url}, {link_url}, {bot}")
return (sentby, text, image_url, link_url, bot)
def get_subject(query):
if not query[0].isupper():
logging.info(f"add uppercase: {query}")
context_from_user = query[0].upper() + query[1:]
logging.info("Start Subject Detection")
# Load the English model
nlp = spacy.load("en_core_web_sm")
# Process the sentence
doc = nlp(query)
# generate valid users
valid_names = valid_users()
# Find the subject
for token in doc:
# 'nsubj' stands for nominal subject; 'nsubjpass' stands for passive nominal subject
logging.info(f"Subject Details:: {token.text, token.dep_}")
if token.dep_ in ("nsubj", "nsubjpass", "npadvmod", "dobj", "pobj"):
if token.text.lower() in valid_names:
logging.info(f"Subject Detected:: {token.text, token.dep_}")
return token.text
logging.info(f"Subject NOT Detected")
return None
def chat_history():
pass
def valid_users():
file_path = "usermap.json"
with open(file_path, "r") as file:
data = json.load(file)
values_list = list(data.values())
values_list = [name.lower() for name in values_list]
return values_list
if __name__ == "__main__":
try:
with open('slack_body.json') as f:
test_slack = json.load(f)
for i in test_slack:
i = {"event": i}
foo = slack_msg_parser(i)
print(foo)
except Exception as e:
print(e)
print('error')