diff --git a/examples/id_filter_example.py b/examples/id_filter_example.py new file mode 100644 index 0000000..dbd621b --- /dev/null +++ b/examples/id_filter_example.py @@ -0,0 +1,16 @@ +import telebot + +bot = telebot.TeleBot('TOKEN') + + +# Chat id can be private or supergroups. +@bot.message_handler(chat_id=[12345678], commands=['admin']) # chat_id checks id corresponds to your list or not. +def admin_rep(message): + bot.send_message(message.chat.id, "You are allowed to use this command.") + +@bot.message_handler(commands=['admin']) +def not_admin(message): + bot.send_message(message.chat.id, "You are not allowed to use this command") + + +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 new file mode 100644 index 0000000..da0d380 --- /dev/null +++ b/examples/text_filter_example.py @@ -0,0 +1,17 @@ +import telebot + +bot = telebot.TeleBot('TOKEN') + + +# Check if message starts with @admin tag +@bot.message_handler(text_startswith="@admin") +def start_filter(message): + bot.send_message(message.chat.id, "Looks like you are calling admin, wait...") + +# Check if text is hi or hello +@bot.message_handler(text=['hi','hello']) +def text_filter(message): + bot.send_message(message.chat.id, "Hi, {name}!".format(name=message.from_user.first_name)) + + +bot.polling(non_stop=True) \ No newline at end of file diff --git a/telebot/__init__.py b/telebot/__init__.py index e379435..5c85f28 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 = {} + self.custom_filters = {'text': util.TextFilter(), 'text_contains': util.TextContains(), 'chat_id': util.UserFilter(), 'text_startswith': util.TextStarts()} if apihelper.ENABLE_MIDDLEWARE: self.typed_middleware_handlers = { diff --git a/telebot/util.py b/telebot/util.py index 8607ff2..56fa63f 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -485,3 +485,63 @@ class AdvancedCustomFilter(ABC): Perform a check. """ 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