Repository navigation
Return False from text filters when the message has no text - #2645
Merged
Merged
Conversation
TextContainsFilter, TextStartsFilter and IsDigitFilter read message.text
without checking that it exists. Photos, stickers and every other non-text
message have text=None, so a handler using one of these filters raises
instead of being skipped:
@bot.message_handler(text_contains='account')
# a photo arrives -> TypeError: argument of type 'NoneType' is not iterable
The dispatcher doesn't catch it either - _check_filter calls the filter
directly - so the exception reaches the polling loop and the update is lost.
TextFilter already handles this: it takes obj.text or obj.caption and
returns False when there is nothing to match. Apply the same rule here.
Collaborator
|
👍 |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The asynchronous counterparts still raise on missing text, leaving the stated sync-and-async fix incomplete.
1 open issue
What changed in this PR
This PR prevents three synchronous text filters from raising exceptions when a message has no text, allowing handlers to skip non-text messages safely.
Changes:
- Add
Noneguards toTextContainsFilter,TextStartsFilter, andIsDigitFilter. - Add regression tests for photos without text and preserve coverage of successful text matching.
| File | Description |
|---|---|
tests/test_custom_filters.py |
Tests non-text rejection and normal text matching. |
telebot/custom_filters.py |
Returns False from three filters when message text is None. |
Review effort: Balanced
Comment on lines
+250
to
+251
| if message.text is None: | ||
| return False |
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
TextContainsFilter,TextStartsFilterandIsDigitFilterreadmessage.textwithout checking that it exists. Photos, stickers and every other non-text message havetext = None:The dispatcher doesn't swallow it either -
_check_filter()calls the filter directly - so a@bot.message_handler(text_contains='...')handler raises on every photo instead of being skipped.TextFilteralready gets this right: it takesobj.text or obj.captionand returnsFalsewhen there is nothing to match. The three filters now do the same.Describe your tests
New
tests/test_custom_filters.py; the module had no tests before.Three cases build a photo message through
Message.de_jsonand check each filter returnsFalseinstead of raising. A fourth keeps the normal text-message path covered.Without the change: 3 failed, 1 passed. With it: 4 passed. Full suite: 288 passed, 2 skipped.
Python version: 3.14.9
OS: macOS
Checklist: