From 4ced4d29f5c006be34b612ca27310d240218e9fe Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sun, 12 Sep 2021 19:36:23 +0300 Subject: [PATCH] Update custom filters readme and examples --- README.md | 10 +-- .../general_custom_filters.py} | 5 +- .../{ => custom_filters}/id_filter_example.py | 5 +- .../text_filter_example.py | 4 +- telebot/__init__.py | 6 +- telebot/custom_filters.py | 63 +++++++++++++------ telebot/util.py | 32 ---------- 7 files changed, 59 insertions(+), 66 deletions(-) rename examples/{custom_filters.py => custom_filters/general_custom_filters.py} (91%) rename examples/{ => custom_filters}/id_filter_example.py (86%) rename examples/{ => custom_filters}/text_filter_example.py (82%) diff --git a/README.md b/README.md index 849efad..919ff74 100644 --- a/README.md +++ b/README.md @@ -289,25 +289,25 @@ def start(message): There are other examples using middleware handler in the [examples/middleware](examples/middleware) directory. -### Custom filters +#### Custom filters Also, you can use built-in custom filters. Or, you can create your own filter. -[Example of custom filter](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/custom_filters.py) +[Example of custom filter](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/custom_filters/general_custom_filters.py) Also, we have examples on them. Check this links: You can check some built-in filters in source [code](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/telebot/custom_filters.py) -Example of [filtering by id](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/id_filter_example.py) +Example of [filtering by id](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/custom_filters/id_filter_example.py) -Example of [filtering by text](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/text_filter_example.py) +Example of [filtering by text](https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/custom_filters/text_filter_example.py) If you want to add some built-in filter, you are welcome to add it in custom_filters.py file. Here is example of creating filter-class: ```python -class IsAdmin(util.SimpleCustomFilter): +class IsAdmin(telebot.custom_filters.SimpleCustomFilter): # Class will check whether the user is admin or creator in group or not key='is_admin' @staticmethod diff --git a/examples/custom_filters.py b/examples/custom_filters/general_custom_filters.py similarity index 91% rename from examples/custom_filters.py rename to examples/custom_filters/general_custom_filters.py index 18c8c26..382b68c 100644 --- a/examples/custom_filters.py +++ b/examples/custom_filters/general_custom_filters.py @@ -1,18 +1,17 @@ import telebot -from telebot import util bot = telebot.TeleBot('TOKEN') # AdvancedCustomFilter is for list, string filter values -class MainFilter(util.AdvancedCustomFilter): +class MainFilter(telebot.custom_filters.AdvancedCustomFilter): key='text' @staticmethod def check(message, text): return message.text in text # SimpleCustomFilter is for boolean values, such as is_admin=True -class IsAdmin(util.SimpleCustomFilter): +class IsAdmin(telebot.custom_filters.SimpleCustomFilter): key='is_admin' @staticmethod def check(message: telebot.types.Message): diff --git a/examples/id_filter_example.py b/examples/custom_filters/id_filter_example.py similarity index 86% rename from examples/id_filter_example.py rename to examples/custom_filters/id_filter_example.py index 3c83841..06a6ce3 100644 --- a/examples/id_filter_example.py +++ b/examples/custom_filters/id_filter_example.py @@ -15,7 +15,8 @@ def not_admin(message): # Do not forget to register -bot.add_custom_filter(custom_filters.UserFilter()) +bot.add_custom_filter(custom_filters.ChatFilter()) -bot.polling(non_stop=True) \ No newline at end of file +bot.polling(non_stop=True) + diff --git a/examples/text_filter_example.py b/examples/custom_filters/text_filter_example.py similarity index 82% rename from examples/text_filter_example.py rename to examples/custom_filters/text_filter_example.py index 12b62f1..2476097 100644 --- a/examples/text_filter_example.py +++ b/examples/custom_filters/text_filter_example.py @@ -15,7 +15,7 @@ def text_filter(message): bot.send_message(message.chat.id, "Hi, {name}!".format(name=message.from_user.first_name)) # Do not forget to register filters -bot.add_custom_filter(custom_filters.TextFilter()) -bot.add_custom_filter(custom_filters.TextStarts()) +bot.add_custom_filter(custom_filters.TextMatchFilter()) +bot.add_custom_filter(custom_filters.TextStartsFilter()) bot.polling(non_stop=True) \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index e379435..790fff0 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -12,7 +12,7 @@ from typing import Any, Callable, List, Optional, Union # this imports are used to avoid circular import error import telebot.util import telebot.types - +from custom_filters import SimpleCustomFilter, AdvancedCustomFilter logger = logging.getLogger('TeleBot') @@ -3140,9 +3140,9 @@ class TeleBot: filter_check = self.custom_filters.get(message_filter) if not filter_check: return False - elif isinstance(filter_check, util.SimpleCustomFilter): + elif isinstance(filter_check, SimpleCustomFilter): return filter_value == filter_check.check(message) - elif isinstance(filter_check, util.AdvancedCustomFilter): + elif isinstance(filter_check, AdvancedCustomFilter): return filter_check.check(message, filter_value) else: logger.error("Custom filter: wrong type. Should be SimpleCustomFilter or AdvancedCustomFilter.") diff --git a/telebot/custom_filters.py b/telebot/custom_filters.py index 00b0f75..2491185 100644 --- a/telebot/custom_filters.py +++ b/telebot/custom_filters.py @@ -1,8 +1,36 @@ -from telebot import util +from abc import ABC + +class SimpleCustomFilter(ABC): + """ + Simple Custom Filter base class. + Create child class with check() method. + Accepts only message, returns bool value, that is compared with given in handler. + """ + + def check(self, message): + """ + Perform a check. + """ + pass +class AdvancedCustomFilter(ABC): + """ + Simple Custom Filter base class. + Create child class with check() method. + Accepts two parameters, returns bool: True - filter passed, False - filter failed. + message: Message class + text: Filter value given in handler + """ -class TextFilter(util.AdvancedCustomFilter): + def check(self, message, text): + """ + Perform a check. + """ + pass + + +class TextMatchFilter(AdvancedCustomFilter): """ Filter to check Text message. key: text @@ -17,7 +45,7 @@ class TextFilter(util.AdvancedCustomFilter): if type(text) is list:return message.text in text else: return text == message.text -class TextContains(util.AdvancedCustomFilter): +class TextContainsFilter(AdvancedCustomFilter): """ Filter to check Text message. key: text @@ -32,30 +60,27 @@ class TextContains(util.AdvancedCustomFilter): def check(self, message, text): return text in message.text -class UserFilter(util.AdvancedCustomFilter): - """ - Check whether chat_id corresponds to given chat_id. - - Example: - @bot.message_handler(chat_id=[99999]) - - """ - - key = 'chat_id' - def check(self, message, text): - return message.chat.id in text - - -class TextStarts(util.AdvancedCustomFilter): +class TextStartsFilter(AdvancedCustomFilter): """ Filter to check whether message starts with some text. Example: # Will work if message.text starts with 'Sir'. @bot.message_handler(text_startswith='Sir') - """ key = 'text_startswith' def check(self, message, text): return message.text.startswith(text) + +class ChatFilter(AdvancedCustomFilter): + """ + Check whether chat_id corresponds to given chat_id. + + Example: + @bot.message_handler(chat_id=[99999]) + """ + + key = 'chat_id' + def check(self, message, text): + return message.chat.id in text diff --git a/telebot/util.py b/telebot/util.py index 7bcb257..f871f09 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -5,7 +5,6 @@ import string import threading import traceback from typing import Any, Callable, List, Dict, Optional, Union -from abc import ABC # noinspection PyPep8Naming import queue as Queue @@ -456,34 +455,3 @@ def webhook_google_functions(bot, request): return 'Bot FAIL', 400 else: return 'Bot ON' - - -class SimpleCustomFilter(ABC): - """ - Simple Custom Filter base class. - Create child class with check() method. - Accepts only bool. - """ - - def check(self, message): - """ - Perform a check. - """ - pass - -class AdvancedCustomFilter(ABC): - """ - Simple Custom Filter base class. - Create child class with check() method. - Can accept to parameters. - message: Message class - text: Filter value given in handler - """ - - def check(self, message, text): - """ - Perform a check. - """ - pass - - \ No newline at end of file