From 7d5e9e5111379c1de3afae2074a80bcc145ef093 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sun, 12 Sep 2021 20:22:26 +0500 Subject: [PATCH] Added file custom_filters Added file with custom filters. Updated the examples --- examples/id_filter_example.py | 7 +++- examples/text_filter_example.py | 4 +++ telebot/__init__.py | 2 +- telebot/custom_filters.py | 61 +++++++++++++++++++++++++++++++++ telebot/util.py | 58 ------------------------------- 5 files changed, 72 insertions(+), 60 deletions(-) create mode 100644 telebot/custom_filters.py diff --git a/examples/id_filter_example.py b/examples/id_filter_example.py index dbd621b..3c83841 100644 --- a/examples/id_filter_example.py +++ b/examples/id_filter_example.py @@ -1,6 +1,7 @@ import telebot +from telebot import custom_filters -bot = telebot.TeleBot('TOKEN') +bot = telebot.TeleBot('token') # Chat id can be private or supergroups. @@ -13,4 +14,8 @@ def not_admin(message): bot.send_message(message.chat.id, "You are not allowed to use this command") +# Do not forget to register +bot.add_custom_filter(custom_filters.UserFilter()) + + bot.polling(non_stop=True) \ No newline at end of file diff --git a/examples/text_filter_example.py b/examples/text_filter_example.py index da0d380..12b62f1 100644 --- a/examples/text_filter_example.py +++ b/examples/text_filter_example.py @@ -1,4 +1,5 @@ import telebot +from telebot import custom_filters bot = telebot.TeleBot('TOKEN') @@ -13,5 +14,8 @@ def start_filter(message): 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.polling(non_stop=True) \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index 5c85f28..e379435 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -185,7 +185,7 @@ class TeleBot: self.poll_answer_handlers = [] self.my_chat_member_handlers = [] self.chat_member_handlers = [] - self.custom_filters = {'text': util.TextFilter(), 'text_contains': util.TextContains(), 'chat_id': util.UserFilter(), 'text_startswith': util.TextStarts()} + self.custom_filters = {} if apihelper.ENABLE_MIDDLEWARE: self.typed_middleware_handlers = { diff --git a/telebot/custom_filters.py b/telebot/custom_filters.py new file mode 100644 index 0000000..00b0f75 --- /dev/null +++ b/telebot/custom_filters.py @@ -0,0 +1,61 @@ +from telebot import util + + + +class TextFilter(util.AdvancedCustomFilter): + """ + Filter to check Text message. + key: text + + Example: + @bot.message_handler(text=['account']) + """ + + key = 'text' + + def check(self, message, text): + if type(text) is list:return message.text in text + else: return text == message.text + +class TextContains(util.AdvancedCustomFilter): + """ + Filter to check Text message. + key: text + + Example: + # Will respond if any message.text contains word 'account' + @bot.message_handler(text_contains=['account']) + """ + + key = 'text_contains' + + 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): + """ + 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) diff --git a/telebot/util.py b/telebot/util.py index 56fa63f..7bcb257 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -486,62 +486,4 @@ class AdvancedCustomFilter(ABC): """ pass -class TextFilter(AdvancedCustomFilter): - """ - Filter to check Text message. - key: text - - Example: - @bot.message_handler(text=['account']) - """ - - key = 'text' - - def check(self, message, text): - if type(text) is list:return message.text in text - else: return text == message.text - -class TextContains(AdvancedCustomFilter): - """ - Filter to check Text message. - key: text - - Example: - # Will respond if any message.text contains word 'account' - @bot.message_handler(text_contains=['account']) - """ - - key = 'text_contains' - - def check(self, message, text): - return text in message.text - -class UserFilter(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(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) - \ No newline at end of file