daily pings test (content -> minimized to adjacent words, pings->put into a thread)
Browse files
app.py
CHANGED
@@ -85,7 +85,7 @@ async def on_message(message):
|
|
85 |
|
86 |
# notification bot
|
87 |
print("on_message")
|
88 |
-
|
89 |
huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265)
|
90 |
bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341)
|
91 |
if huggingfolks_role not in message.author.roles: # no need for ping if we're already discussing
|
@@ -94,10 +94,11 @@ async def on_message(message):
|
|
94 |
content = message.content.lower()
|
95 |
|
96 |
for trigger, slack_mention in TRIGGERS.items():
|
97 |
-
if all(word in content for word in trigger):
|
|
|
98 |
daily_pings.append({
|
99 |
'author': str(message.author),
|
100 |
-
'content':
|
101 |
'channel': message.channel.name,
|
102 |
'url': message.jump_url,
|
103 |
'mention': slack_mention,
|
@@ -106,7 +107,7 @@ async def on_message(message):
|
|
106 |
print(f"daily pings:{daily_pings}")
|
107 |
break
|
108 |
|
109 |
-
|
110 |
|
111 |
|
112 |
# Check if the message is in a thread
|
@@ -122,6 +123,14 @@ async def on_message(message):
|
|
122 |
await bot.process_commands(message)
|
123 |
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
@bot.event
|
126 |
async def on_thread_create(thread):
|
127 |
# (discord) must be the child thread of the CORRECT forum channel(s) (not just any thread, or any forum channel)
|
@@ -230,27 +239,29 @@ def send_daily_pings():
|
|
230 |
if daily_pings:
|
231 |
print("sending daily pings...")
|
232 |
# combine into one message
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
channel=SLACK_CHANNEL_ID,
|
237 |
-
text=combined_message,
|
238 |
unfurl_links=False,
|
239 |
-
unfurl_media=False
|
240 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
daily_pings = [] # reset after posting
|
243 |
|
244 |
# pings -------------------------------------------------------------------------------------------
|
245 |
-
|
246 |
-
"""
|
247 |
-
|
248 |
executor = ThreadPoolExecutor(max_workers=1)
|
249 |
scheduler = BackgroundScheduler(executors={'default': executor})
|
250 |
-
scheduler.add_job(send_daily_pings, trigger='interval',
|
251 |
scheduler.start()
|
252 |
-
"""
|
253 |
-
|
254 |
|
255 |
|
256 |
# runs discord bot in thread = helps avoid blocking calls
|
|
|
85 |
|
86 |
# notification bot
|
87 |
print("on_message")
|
88 |
+
|
89 |
huggingfolks_role = discord.utils.get(message.guild.roles, id=897376942817419265)
|
90 |
bots_role = discord.utils.get(message.guild.roles, id=1258328471609016341)
|
91 |
if huggingfolks_role not in message.author.roles: # no need for ping if we're already discussing
|
|
|
94 |
content = message.content.lower()
|
95 |
|
96 |
for trigger, slack_mention in TRIGGERS.items():
|
97 |
+
if all(word in content for word in trigger):
|
98 |
+
adjacent_words = extract_adjacent_words(message.content, trigger, 3)
|
99 |
daily_pings.append({
|
100 |
'author': str(message.author),
|
101 |
+
'content': adjacent_words,
|
102 |
'channel': message.channel.name,
|
103 |
'url': message.jump_url,
|
104 |
'mention': slack_mention,
|
|
|
107 |
print(f"daily pings:{daily_pings}")
|
108 |
break
|
109 |
|
110 |
+
|
111 |
|
112 |
|
113 |
# Check if the message is in a thread
|
|
|
123 |
await bot.process_commands(message)
|
124 |
|
125 |
|
126 |
+
def extract_adjacent_words(content, trigger, num_words=3):
|
127 |
+
pattern = r'\b(\w+\s+){0,' + str(num_words) + '}' + r'\b' + r'\b'.join(map(re.escape, trigger)) + r'\b' + r'(\s+\w+){0,' + str(num_words) + '}\b'
|
128 |
+
match = re.search(pattern, content, re.IGNORECASE)
|
129 |
+
if match:
|
130 |
+
return f"...{match.group(0)}..."
|
131 |
+
return content
|
132 |
+
|
133 |
+
|
134 |
@bot.event
|
135 |
async def on_thread_create(thread):
|
136 |
# (discord) must be the child thread of the CORRECT forum channel(s) (not just any thread, or any forum channel)
|
|
|
239 |
if daily_pings:
|
240 |
print("sending daily pings...")
|
241 |
# combine into one message
|
242 |
+
main_message = slack_client.chat_postMessage(
|
243 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
244 |
+
text=f"DAILY PINGS FOR {datetime.now().strftime('%d/%m/%Y')}",
|
|
|
|
|
245 |
unfurl_links=False,
|
246 |
+
unfurl_media=False
|
247 |
+
)
|
248 |
+
main_ts = main_message['ts']
|
249 |
+
for ping in daily_pings:
|
250 |
+
slack_client.chat_postMessage(
|
251 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
252 |
+
text=f"{ping['mention']} (for the keyword -> '{ping['trigger']}')\nFrom {ping['author']} in channel #{ping['channel']}: {ping['content']}\n{ping['url']}",
|
253 |
+
thread_ts=main_ts,
|
254 |
+
unfurl_links=False,
|
255 |
+
unfurl_media=False
|
256 |
+
)
|
257 |
|
258 |
daily_pings = [] # reset after posting
|
259 |
|
260 |
# pings -------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
261 |
executor = ThreadPoolExecutor(max_workers=1)
|
262 |
scheduler = BackgroundScheduler(executors={'default': executor})
|
263 |
+
scheduler.add_job(send_daily_pings, trigger='interval', seconds=10)
|
264 |
scheduler.start()
|
|
|
|
|
265 |
|
266 |
|
267 |
# runs discord bot in thread = helps avoid blocking calls
|