Skip to content

Conversation

@avelytchko
Copy link
Contributor

@avelytchko avelytchko commented Nov 23, 2025

  • 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"

Summary by CodeRabbit

  • Bug Fixes
    • Improved bot mention detection to use exact prefix matching with proper word boundaries, reducing false positives when trigger words appear within other text or phrases.

✏️ Tip: You can customize this high-level summary in your review settings.

- 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"
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 23, 2025

Walkthrough

The is_bot_mentioned() function in src/main.py was refactored to use strict prefix-and-boundary checking instead of substring containment. The function now trims and lowercases the message, returning true only when a bot trigger word is the leading token with no alphabetic character following it, reducing false positives.

Changes

Cohort / File(s) Summary
Bot mention detection logic
src/main.py
Reworked is_bot_mentioned() to perform strict prefix-and-boundary validation instead of substring matching, trimming and lowercasing input to check if a trigger word is the leading token without a trailing alphabetic character.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Key focus areas:
    • Verify the prefix-and-boundary logic correctly identifies bot mentions and rejects mid-message occurrences of trigger words
    • Ensure edge cases are handled (empty messages, trigger word at message end, messages with special characters)
    • Confirm test coverage reflects the new stricter detection behavior

Possibly related PRs

  • PR #91: Also modifies is_bot_mentioned() in src/main.py but implements opposite detection logic (broadens vs. tightens trigger word matching).

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: converting bot trigger detection from substring matching to strict prefix-based detection with boundary checks, reducing false positives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1ae41d and 30a04b4.

📒 Files selected for processing (1)
  • src/main.py (1 hunks)

Comment on lines +109 to +116
# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Consider stricter word boundary checking to avoid false positives.

The current boundary check uses isalpha(), which means messages like "ботяра123" or "bot_health_check" would trigger the bot since digits and underscores are not alphabetic. This could lead to unintended activations.

If you want strict word boundaries, consider checking for whitespace or end-of-string instead:

     bot_trigger_words = ["ботяра", "bot_health"]
     # 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():
+            # Check if it's the whole message or followed by whitespace
+            if len(cleaned_text) == len(word) or cleaned_text[len(word)].isspace():
                 return True
     return False

Alternatively, if you want to allow punctuation but not alphanumeric characters:

     bot_trigger_words = ["ботяра", "bot_health"]
     # 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():
+            # Check if it's the whole message or followed by non-alphanumeric character
+            if len(cleaned_text) == len(word) or not cleaned_text[len(word)].isalnum():
                 return True
     return False
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# 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
# 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 the whole message or followed by whitespace
if len(cleaned_text) == len(word) or cleaned_text[len(word)].isspace():
return True
return False
Suggested change
# 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
# 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 the whole message or followed by non-alphanumeric character
if len(cleaned_text) == len(word) or not cleaned_text[len(word)].isalnum():
return True
return False
🤖 Prompt for AI Agents
In src/main.py around lines 109 to 116, the trigger-word boundary check using
cleaned_text[len(word)].isalpha() allows digits/underscores to pass and causes
false positives (e.g., "bot123" or "bot_health"); replace that check with a
stricter condition: treat the trigger as valid only if it's at end-of-string or
the following character is whitespace or a punctuation character (use
string.punctuation), e.g., check for len(cleaned_text)==len(word) or
cleaned_text[len(word)].isspace() or cleaned_text[len(word)] in
string.punctuation; ensure to import string at the top of the file.

@ovchynnikov ovchynnikov merged commit c086cdf into ovchynnikov:main Nov 23, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants