Skip to content

Commit

Permalink
ci: mypy and ruff checks
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Dec 23, 2024
1 parent 942a6bd commit 79a3f65
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 3 additions & 21 deletions src/commands/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,33 +22,15 @@ 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

# 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")

Expand Down
24 changes: 24 additions & 0 deletions src/utils/telegram.py
Original file line number Diff line number Diff line change
@@ -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'"""
Expand Down Expand Up @@ -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

0 comments on commit 79a3f65

Please sign in to comment.