From 79a3f65d83558f78b2dabebffdf113bc8013ecea Mon Sep 17 00:00:00 2001 From: Reza Rahemtola Date: Mon, 23 Dec 2024 17:13:10 +0900 Subject: [PATCH] ci: mypy and ruff checks --- .github/workflows/ci.yaml | 42 +++++++++++++++++++++++++++++++++++++++ src/commands/message.py | 24 +++------------------- src/utils/telegram.py | 24 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..f0681d8 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,42 @@ +name: CI + +on: + push: + +jobs: + mypy: + name: "mypy" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install poetry + run: pipx install poetry + - uses: actions/setup-python@v5 + with: + python-version-file: 'pyproject.toml' + cache: 'poetry' + - name: Install dependencies + run: poetry install + - uses: tsuyoshicho/action-mypy@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-check + target: "./src" + execute_command: 'poetry run mypy' + fail_on_error: true + + ruff: + name: "ruff" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install poetry + run: pipx install poetry + - uses: actions/setup-python@v5 + with: + python-version-file: 'pyproject.toml' + cache: 'poetry' + - name: Install dependencies + run: pip install ruff + - name: Run Ruff + run: ruff check --output-format=github diff --git a/src/commands/message.py b/src/commands/message.py index 943d78a..31d6d58 100644 --- a/src/commands/message.py +++ b/src/commands/message.py @@ -6,7 +6,7 @@ from src.utils.telegram import ( get_formatted_message_content, get_formatted_username, - get_mentions_in_message, + should_reply_to_message, ) # Max number of messages we will pass @@ -22,7 +22,7 @@ async def text_message_handler(message: telebot_types.Message): span = config.LOGGER.get_span(message) span.info("Received text message") - reply: telebot_types.Message | None = None + reply: telebot_types.Message | None try: chat_id = message.chat.id @@ -30,25 +30,7 @@ async def text_message_handler(message: telebot_types.Message): # Add the message to the chat history await config.DATABASE.add_message(message, span=span) - # Determine if the message is meant for the bot - should_reply = False - if message.chat.type == "private": - # Always reply to DMs - should_reply = True - else: - if ( - message.reply_to_message is not None - and message.reply_to_message.from_user.username - == config.BOT_INFO.username - ): - # The message is a reply to a message that is the bot - should_reply = True - - mentions = get_mentions_in_message(message) - if f"@{config.BOT_INFO.username}" in mentions: - # Message is mentioning the bot - should_reply = True - + should_reply = should_reply_to_message(message) if should_reply is False: span.info("Message not intended for the bot") diff --git a/src/utils/telegram.py b/src/utils/telegram.py index 803751b..0d6ed99 100644 --- a/src/utils/telegram.py +++ b/src/utils/telegram.py @@ -1,5 +1,7 @@ from telebot.types import Message, User +from src.config import config + def get_mentions_in_message(message: Message) -> list[str]: """Returns an array of mentions inside the text of a message. Each mention is in the format '@username'""" @@ -34,3 +36,25 @@ def get_formatted_message_content(message: Message) -> str: sender = f"{sender} (in reply to {reply_to_username})" return f"{sender}\n{message.text}" + + +def should_reply_to_message(message: Message) -> bool: + """ + Determines if a message is intended for our bot or not + """ + if message.chat.type == "private": + # Always reply to DMs + return True + else: + if ( + message.reply_to_message is not None + and message.reply_to_message.from_user.username == config.BOT_INFO.username + ): + # The message is a reply to a message that is the bot + return True + + mentions = get_mentions_in_message(message) + if f"@{config.BOT_INFO.username}" in mentions: + # Message is mentioning the bot + return True + return False