From 1f6202c68d0fde553adedf24c70f894723d568e9 Mon Sep 17 00:00:00 2001 From: Marciel Torres Date: Tue, 13 Feb 2024 22:53:08 -0300 Subject: [PATCH] Improving main structure using ideas from bolt-python-starter-template (#11) --- src/bot.py | 9 ++------- src/listeners/messages/regex_message.py | 20 ++++++++++++++++++++ src/listeners/register.py | 5 +++++ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 src/listeners/messages/regex_message.py create mode 100644 src/listeners/register.py diff --git a/src/bot.py b/src/bot.py index 6910a7b..99e0364 100644 --- a/src/bot.py +++ b/src/bot.py @@ -6,8 +6,7 @@ from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler from slack_bolt.async_app import AsyncApp -from src.messages.constants import DEFAULT_WARNING_MESSAGE -from src.rules.pattern import pattern +from src.listeners.register import register_listeners logConfig("./logging.conf", disable_existing_loggers=False) logger = getLogger(__name__) @@ -17,13 +16,9 @@ app = AsyncApp(token=SLACK_BOT_TOKEN) -@app.message(pattern.compiled_pattern) -async def say_hello_regex(say, message, client): - user = message['user'] - await say(text=DEFAULT_WARNING_MESSAGE.format(name=f'<@{user}>'), thread_ts=message.get('ts')) - async def main(): try: + await register_listeners(app) handler = AsyncSocketModeHandler(app, SLACK_APP_TOKEN) await handler.start_async() except Exception as ex: diff --git a/src/listeners/messages/regex_message.py b/src/listeners/messages/regex_message.py new file mode 100644 index 0000000..ae5eafb --- /dev/null +++ b/src/listeners/messages/regex_message.py @@ -0,0 +1,20 @@ +from logging import getLogger + +from slack_bolt import App + +from src.messages.constants import DEFAULT_WARNING_MESSAGE +from src.rules.pattern import pattern + +logger = getLogger(__name__) + + +async def register(app: App): + app.message(pattern.compiled_pattern)(regex_message_callback) + + +async def regex_message_callback(message, say): + try: + user = message['user'] + await say(text=DEFAULT_WARNING_MESSAGE.format(name=f'<@{user}>'), thread_ts=message.get('ts')) + except Exception as e: + logger.error(e) diff --git a/src/listeners/register.py b/src/listeners/register.py new file mode 100644 index 0000000..45d19f4 --- /dev/null +++ b/src/listeners/register.py @@ -0,0 +1,5 @@ +from src.listeners.messages.regex_message import register as register_regex_message + + +async def register_listeners(app): + await register_regex_message(app)