From 3626f406b60d6c38ba375ed1b51c4c846f42e9c3 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Thu, 2 Nov 2023 00:27:22 +0100 Subject: [PATCH] limit to stampy addressed messages --- modules/module.py | 2 ++ modules/stampy_chat.py | 19 +++++++++++++++++-- utilities/flaskutils.py | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/module.py b/modules/module.py index 33f3291..e5c3602 100644 --- a/modules/module.py +++ b/modules/module.py @@ -243,6 +243,8 @@ def is_at_me(self, message: ServiceMessage) -> Union[str, Literal[False]]: r",? @?[sS](tampy)?(?P[.!?]*)$", r"\g", text ) at_me = True + elif re.search(r'^[sS]tamp[ys]?\?', text): + at_me = True if message.is_dm: # DMs are always at you diff --git a/modules/stampy_chat.py b/modules/stampy_chat.py index 92e609d..e472ee6 100644 --- a/modules/stampy_chat.py +++ b/modules/stampy_chat.py @@ -141,11 +141,26 @@ def _add_message(self, message: ServiceMessage) -> deque[ServiceMessage]: self._messages[message.channel].append(message) return self._messages[message.channel] + def make_query(self, messages): + if not messages: + return '', messages + + current = messages[-1] + query, history = '', list(messages) + while message := (history and history.pop()): + if message.author != current.author: + break + query = message.content + ' ' + query + current = message + return query, history + def process_message(self, message: ServiceMessage) -> Response: history = self._add_message(message) - history.append(message) - query = message.content + if not self.is_at_me(message): + return Response() + + query, history = self.make_query(history) nlp = top_nlp_search(query) if nlp.get('score', 0) > STAMPY_ANSWER_MIN_SCORE and nlp.get('status') == 'Live on site': return Response(confidence=5, text=f'Check out {nlp.get("url")} ({nlp.get("title")})') diff --git a/utilities/flaskutils.py b/utilities/flaskutils.py index d4fde31..feeb3f4 100644 --- a/utilities/flaskutils.py +++ b/utilities/flaskutils.py @@ -92,4 +92,5 @@ def from_dict(data): id=str(time.time()), ) msg.modules = modules + msg.clean_content = msg.content return msg