1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

removed TextFilterKey in example, instead TextMatchFilter was modified

This commit is contained in:
abdullaev388 2022-02-12 17:07:59 +05:00
parent 3cd86d0e93
commit 5b1483f646
4 changed files with 38 additions and 30 deletions

View File

@ -9,16 +9,9 @@ import asyncio
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
from telebot import types from telebot import types
from telebot.custom_filters import TextFilter from telebot.custom_filters import TextFilter
from telebot.asyncio_filters import AdvancedCustomFilter from telebot.asyncio_filters import TextMatchFilter
bot = AsyncTeleBot("1254795383:AAE7gbj1aas4lEDHB1eVuZZhSGPWcH1B5ds") bot = AsyncTeleBot("")
class TextFilterKey(AdvancedCustomFilter):
key = 'text'
async def check(self, message, config: TextFilter):
return config.check(message)
@bot.message_handler(text=TextFilter(equals='hello')) @bot.message_handler(text=TextFilter(equals='hello'))
@ -99,5 +92,5 @@ async def poll_question_handler_ignore_case(poll: types.Poll):
if __name__ == '__main__': if __name__ == '__main__':
bot.add_custom_filter(TextFilterKey()) bot.add_custom_filter(TextMatchFilter())
asyncio.run(bot.polling()) asyncio.run(bot.polling())

View File

@ -6,18 +6,11 @@ with (message_handler, callback_query_handler, poll_handler)
""" """
from telebot import TeleBot, types from telebot import TeleBot, types
from telebot.custom_filters import TextFilter, AdvancedCustomFilter from telebot.custom_filters import TextFilter, TextMatchFilter
bot = TeleBot("") bot = TeleBot("")
class TextFilterKey(AdvancedCustomFilter):
key = 'text'
def check(self, message, config: TextFilter):
return config.check(message)
@bot.message_handler(text=TextFilter(equals='hello')) @bot.message_handler(text=TextFilter(equals='hello'))
def hello_handler(message: types.Message): def hello_handler(message: types.Message):
bot.send_message(message.chat.id, message.text) bot.send_message(message.chat.id, message.text)
@ -96,5 +89,5 @@ def poll_question_handler_ignore_case(poll: types.Poll):
if __name__ == '__main__': if __name__ == '__main__':
bot.add_custom_filter(TextFilterKey()) bot.add_custom_filter(TextMatchFilter())
bot.infinity_polling() bot.infinity_polling()

View File

@ -1,5 +1,8 @@
from abc import ABC from abc import ABC
from telebot.custom_filters import TextFilter
class SimpleCustomFilter(ABC): class SimpleCustomFilter(ABC):
""" """
Simple Custom Filter base class. Simple Custom Filter base class.
@ -42,8 +45,13 @@ class TextMatchFilter(AdvancedCustomFilter):
key = 'text' key = 'text'
async def check(self, message, text): async def check(self, message, text):
if type(text) is list:return message.text in text if isinstance(text, TextFilter):
else: return text == message.text return text.check(message)
elif type(text) is list:
return message.text in text
else:
return text == message.text
class TextContainsFilter(AdvancedCustomFilter): class TextContainsFilter(AdvancedCustomFilter):
""" """
@ -60,6 +68,7 @@ class TextContainsFilter(AdvancedCustomFilter):
async def check(self, message, text): async def check(self, message, text):
return text in message.text return text in message.text
class TextStartsFilter(AdvancedCustomFilter): class TextStartsFilter(AdvancedCustomFilter):
""" """
Filter to check whether message starts with some text. Filter to check whether message starts with some text.
@ -70,9 +79,11 @@ class TextStartsFilter(AdvancedCustomFilter):
""" """
key = 'text_startswith' key = 'text_startswith'
async def check(self, message, text): async def check(self, message, text):
return message.text.startswith(text) return message.text.startswith(text)
class ChatFilter(AdvancedCustomFilter): class ChatFilter(AdvancedCustomFilter):
""" """
Check whether chat_id corresponds to given chat_id. Check whether chat_id corresponds to given chat_id.
@ -82,9 +93,11 @@ class ChatFilter(AdvancedCustomFilter):
""" """
key = 'chat_id' key = 'chat_id'
async def check(self, message, text): async def check(self, message, text):
return message.chat.id in text return message.chat.id in text
class ForwardFilter(SimpleCustomFilter): class ForwardFilter(SimpleCustomFilter):
""" """
Check whether message was forwarded from channel or group. Check whether message was forwarded from channel or group.
@ -99,6 +112,7 @@ class ForwardFilter(SimpleCustomFilter):
async def check(self, message): async def check(self, message):
return message.forward_from_chat is not None return message.forward_from_chat is not None
class IsReplyFilter(SimpleCustomFilter): class IsReplyFilter(SimpleCustomFilter):
""" """
Check whether message is a reply. Check whether message is a reply.
@ -114,7 +128,6 @@ class IsReplyFilter(SimpleCustomFilter):
return message.reply_to_message is not None return message.reply_to_message is not None
class LanguageFilter(AdvancedCustomFilter): class LanguageFilter(AdvancedCustomFilter):
""" """
Check users language_code. Check users language_code.
@ -127,8 +140,11 @@ class LanguageFilter(AdvancedCustomFilter):
key = 'language_code' key = 'language_code'
async def check(self, message, text): async def check(self, message, text):
if type(text) is list:return message.from_user.language_code in text if type(text) is list:
else: return message.from_user.language_code == text return message.from_user.language_code in text
else:
return message.from_user.language_code == text
class IsAdminFilter(SimpleCustomFilter): class IsAdminFilter(SimpleCustomFilter):
""" """
@ -147,6 +163,7 @@ class IsAdminFilter(SimpleCustomFilter):
result = await self._bot.get_chat_member(message.chat.id, message.from_user.id) result = await self._bot.get_chat_member(message.chat.id, message.from_user.id)
return result.status in ['creator', 'administrator'] return result.status in ['creator', 'administrator']
class StateFilter(AdvancedCustomFilter): class StateFilter(AdvancedCustomFilter):
""" """
Filter to check state. Filter to check state.
@ -154,8 +171,10 @@ class StateFilter(AdvancedCustomFilter):
Example: Example:
@bot.message_handler(state=1) @bot.message_handler(state=1)
""" """
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
key = 'state' key = 'state'
async def check(self, message, text): async def check(self, message, text):
@ -176,12 +195,13 @@ class StateFilter(AdvancedCustomFilter):
else: else:
user_state = await self.bot.current_states.get_state(message.chat.id,message.from_user.id) user_state = await self.bot.current_states.get_state(message.chat.id, message.from_user.id)
if user_state == text: if user_state == text:
return True return True
elif type(text) is list and user_state in text: elif type(text) is list and user_state in text:
return True return True
class IsDigitFilter(SimpleCustomFilter): class IsDigitFilter(SimpleCustomFilter):
""" """
Filter to check whether the string is made up of only digits. Filter to check whether the string is made up of only digits.

View File

@ -128,7 +128,9 @@ class TextMatchFilter(AdvancedCustomFilter):
key = 'text' key = 'text'
def check(self, message, text): def check(self, message, text):
if type(text) is list: if isinstance(text, TextFilter):
return text.check(message)
elif type(text) is list:
return message.text in text return message.text in text
else: else:
return text == message.text return text == message.text