From 30a04b46b8a356e4b64cc62613923d76d3b2fb09 Mon Sep 17 00:00:00 2001 From: avelytchko <919635+avelytchko@users.noreply.github.com> Date: Sun, 23 Nov 2025 20:01:03 +0100 Subject: [PATCH] Fix bot trigger to respond only when mentioned at start of message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed is_bot_mentioned() to use startswith() instead of 'in' operator - Bot now only responds when "ботяра" or "bot_health" is at the beginning - Prevents false triggers when trigger words appear mid-sentence - Preserves special characters like underscore in "bot_health" --- src/main.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 8e70c71..4a6e716 100644 --- a/src/main.py +++ b/src/main.py @@ -106,9 +106,14 @@ def is_bot_mentioned(message_text: str) -> bool: bool: True if the bot is mentioned, False otherwise. """ bot_trigger_words = ["ботяра", "bot_health"] - # Remove all non-letter characters - cleaned_text = ''.join(char for char in message_text.lower() if char.isalpha() or char.isspace()) - return any(word in cleaned_text for word in bot_trigger_words) + # Remove leading/trailing whitespace and convert to lowercase + cleaned_text = message_text.strip().lower() + for word in bot_trigger_words: + if cleaned_text.startswith(word): + # Check if it's followed by space, punctuation, or is the whole message + if len(cleaned_text) == len(word) or not cleaned_text[len(word)].isalpha(): + return True + return False def clean_url(message_text: str) -> str: