From 3a86916e7251bb69b3572adc47348f6cc5520731 Mon Sep 17 00:00:00 2001 From: abdullaev388 Date: Wed, 16 Feb 2022 12:43:23 +0500 Subject: [PATCH] example of TextFilter starts_with and ends_with usage simultaneously --- .../custom_filters/advanced_text_filter.py | 16 +++++++++++++++- examples/custom_filters/advanced_text_filter.py | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/examples/asynchronous_telebot/custom_filters/advanced_text_filter.py b/examples/asynchronous_telebot/custom_filters/advanced_text_filter.py index df8b2dd..1200a6c 100644 --- a/examples/asynchronous_telebot/custom_filters/advanced_text_filter.py +++ b/examples/asynchronous_telebot/custom_filters/advanced_text_filter.py @@ -8,7 +8,7 @@ import asyncio from telebot import types from telebot.async_telebot import AsyncTeleBot -from telebot.asyncio_filters import TextMatchFilter, TextFilter +from telebot.asyncio_filters import TextMatchFilter, TextFilter, IsReplyFilter bot = AsyncTeleBot("") @@ -108,6 +108,20 @@ async def multiple_ends_with_handler(message: types.Message): await bot.send_message(message.chat.id, message.text) +# !ban /ban .ban !бан /бан .бан +@bot.message_handler(is_reply=True, + text=TextFilter(starts_with=('!', '/', '.'), ends_with=['ban', 'бан'], ignore_case=True)) +async def ban_command_handler(message: types.Message): + if len(message.text) == 4 and message.chat.type != 'private': + try: + await bot.ban_chat_member(message.chat.id, message.reply_to_message.from_user.id) + await bot.reply_to(message.reply_to_message, 'Banned.') + except Exception as err: + print(err.args) + return + + if __name__ == '__main__': bot.add_custom_filter(TextMatchFilter()) + bot.add_custom_filter(IsReplyFilter()) asyncio.run(bot.polling()) diff --git a/examples/custom_filters/advanced_text_filter.py b/examples/custom_filters/advanced_text_filter.py index 41ce3d5..2b01685 100644 --- a/examples/custom_filters/advanced_text_filter.py +++ b/examples/custom_filters/advanced_text_filter.py @@ -6,7 +6,7 @@ with (message_handler, callback_query_handler, poll_handler) """ from telebot import TeleBot, types -from telebot.custom_filters import TextFilter, TextMatchFilter +from telebot.custom_filters import TextFilter, TextMatchFilter, IsReplyFilter bot = TeleBot("") @@ -106,6 +106,20 @@ def multiple_ends_with_handler(message: types.Message): bot.send_message(message.chat.id, message.text) +# !ban /ban .ban !бан /бан .бан +@bot.message_handler(is_reply=True, + text=TextFilter(starts_with=('!', '/', '.'), ends_with=['ban', 'бан'], ignore_case=True)) +def ban_command_handler(message: types.Message): + if len(message.text) == 4 and message.chat.type != 'private': + try: + bot.ban_chat_member(message.chat.id, message.reply_to_message.from_user.id) + bot.reply_to(message.reply_to_message, 'Banned.') + except Exception as err: + print(err.args) + return + + if __name__ == '__main__': bot.add_custom_filter(TextMatchFilter()) + bot.add_custom_filter(IsReplyFilter()) bot.infinity_polling()