-
Notifications
You must be signed in to change notification settings - Fork 5
Fix bot trigger to respond only when mentioned at start of message #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- 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"
WalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 FalseAlternatively, 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.
| # 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 |
| # 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.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.