From 3b67e66d0d85f7105571e2d798fa1be90c53b02d Mon Sep 17 00:00:00 2001 From: youdie006 Date: Wed, 28 Oct 2026 16:52:35 +0900 Subject: [PATCH] Return False from async text filters when the message has no text #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. --- telebot/asyncio_filters.py | 8 ++++++++ tests/test_custom_filters.py | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/telebot/asyncio_filters.py b/telebot/asyncio_filters.py index 83aefefe3..0d0fed746 100644 --- a/telebot/asyncio_filters.py +++ b/telebot/asyncio_filters.py @@ -147,6 +147,8 @@ async def check(self, obj: Union[types.Message, types.CallbackQuery, types.Inlin text = obj.question elif isinstance(obj, types.Message): text = obj.text or obj.caption + if text is None: + return False elif isinstance(obj, types.CallbackQuery): text = obj.data elif isinstance(obj, types.InlineQuery): @@ -239,6 +241,8 @@ async def check(self, message, text): elif isinstance(text, list) or isinstance(text, tuple): text = [i for i in text if isinstance(i, str)] + if message.text is None: + return False return any([i in message.text for i in text]) @@ -260,6 +264,8 @@ async def check(self, message, text): """ :meta private: """ + if message.text is None: + return False return message.text.startswith(text) @@ -447,4 +453,6 @@ async def check(self, message): """ :meta private: """ + if message.text is None: + return False return message.text.isdigit() diff --git a/tests/test_custom_filters.py b/tests/test_custom_filters.py index 3d8386e6b..6c5012d04 100644 --- a/tests/test_custom_filters.py +++ b/tests/test_custom_filters.py @@ -2,11 +2,12 @@ sys.path.append('../') +import asyncio import json import pytest -from telebot import types +from telebot import asyncio_filters, types from telebot.custom_filters import IsDigitFilter, TextContainsFilter, TextStartsFilter @@ -50,3 +51,21 @@ def test_text_filters_still_match_text_messages(text_message): assert TextContainsFilter().check(text_message, "23") is True assert TextStartsFilter().check(text_message, "12") is True assert IsDigitFilter().check(text_message) is True + + +def test_async_text_filters_skip_message_without_text(photo_message): + assert asyncio.run(asyncio_filters.TextContainsFilter().check(photo_message, "23")) is False + assert asyncio.run(asyncio_filters.TextStartsFilter().check(photo_message, "12")) is False + assert asyncio.run(asyncio_filters.IsDigitFilter().check(photo_message)) is False + assert asyncio.run(asyncio_filters.TextFilter(contains=["23"]).check(photo_message)) is False + assert asyncio.run(asyncio_filters.TextFilter(equals="12345", ignore_case=True).check(photo_message)) is False + + +def test_async_text_filters_still_match_text_messages(text_message): + assert asyncio.run(asyncio_filters.TextContainsFilter().check(text_message, "23")) is True + assert asyncio.run(asyncio_filters.TextStartsFilter().check(text_message, "12")) is True + assert asyncio.run(asyncio_filters.IsDigitFilter().check(text_message)) is True + captioned = make_message( + photo=[{"file_id": "f", "file_unique_id": "u", "width": 1, "height": 1}], caption="12345" + ) + assert asyncio.run(asyncio_filters.TextFilter(contains=["23"]).check(captioned)) is True