Skip to content

Return False from async text filters when the message has no text - #2650

Merged
Badiboy merged 1 commit into
eternnoir:masterfrom
youdie006:async-text-filters-no-text
Oct 31, 2026
Merged

Badiboy merged 1 commit into
eternnoir:masterfrom
youdie006:async-text-filters-no-text

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

Description

#2645 made the sync TextContainsFilter, TextStartsFilter and IsDigitFilter return False when the message has no text, and the sync TextFilter already did. The asyncio_filters versions still raise on such a message (TypeError: argument of type 'NoneType' is not iterable, AttributeError: 'NoneType' object has no attribute 'startswith', ...). In AsyncTeleBot that exception skips the rest of the handlers for the update:

@bot.message_handler(content_types=["photo"], text=asyncio_filters.TextFilter(contains=["#cat"]))
async def on_cat_photo(message): ...

@bot.message_handler(content_types=["photo"])
async def on_any_photo(message): ...

A photo without a caption goes to the exception handler with a TypeError instead of reaching on_any_photo; TeleBot with the same handlers calls on_any_photo. This adds the same None checks to the four async filters.

Describe your tests

Two tests in tests/test_custom_filters.py, next to the sync ones from #2645, using its fixtures; the first fails without the change. cd tests && py.test: 309 passed, 73 skipped (the token-dependent tests).

Python version: 3.11 and 3.14

OS: Linux

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

Written with AI assistance (Claude); I have reviewed the change.

eternnoir#2645 added the 'no text' guard to the sync TextContainsFilter, TextStartsFilter and IsDigitFilter, and an older change added it to the sync TextFilter. The asyncio_filters versions still raise on a message with neither text nor caption, which aborts the remaining handlers for that update.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the async text-filter changes.

No open issues 🎉

What changed in this PR

Aligns async text filters with their synchronous counterparts so messages without text return False instead of raising exceptions, allowing dispatch to continue to later handlers.

Changes:

  • Adds None checks to four async text filters while preserving TextFilter caption matching.
  • Adds regression tests for messages without text and successful text or caption matches.
File Description
tests/​test_custom_filters.py Tests async filters with missing text and valid text or captions.
telebot/​asyncio_filters.py Returns False when the four async text filters have no text to match.

Review effort: Balanced

@Badiboy

Badiboy commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Thank you.

@Badiboy
Badiboy merged commit 7bd7ccf into eternnoir:master Oct 31, 2026
7 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.

3 participants