mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Updated __init__.py
This commit is contained in:
parent
f2202b44fe
commit
5c80f11261
46
examples/custom_filters.py
Normal file
46
examples/custom_filters.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import telebot
|
||||||
|
from telebot import util
|
||||||
|
|
||||||
|
bot = telebot.TeleBot('TOKEN')
|
||||||
|
|
||||||
|
|
||||||
|
# AdvancedCustomFilter is for list, string filter values
|
||||||
|
class MainFilter(util.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):
|
||||||
|
key='is_admin'
|
||||||
|
@staticmethod
|
||||||
|
def check(message: telebot.types.Message):
|
||||||
|
if bot.get_chat_member(message.chat.id,message.from_user.id).status in ['administrator','creator']:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@bot.message_handler(is_admin=True, commands=['admin']) # Check if user is admin
|
||||||
|
def admin_rep(message):
|
||||||
|
bot.send_message(message.chat.id, "Hi admin")
|
||||||
|
|
||||||
|
@bot.message_handler(is_admin=False, commands=['admin']) # If user is not admin
|
||||||
|
def not_admin(message):
|
||||||
|
bot.send_message(message.chat.id, "You are not admin")
|
||||||
|
|
||||||
|
@bot.message_handler(text=['hi']) # Response to hi message
|
||||||
|
def welcome_hi(message):
|
||||||
|
bot.send_message(message.chat.id, 'You said hi')
|
||||||
|
|
||||||
|
@bot.message_handler(text=['bye']) # Response to bye message
|
||||||
|
def bye_user(message):
|
||||||
|
bot.send_message(message.chat.id, 'You said bye')
|
||||||
|
|
||||||
|
|
||||||
|
# Do not forget to register filters
|
||||||
|
bot.add_custom_filter(MainFilter)
|
||||||
|
bot.add_custom_filter(IsAdmin)
|
||||||
|
|
||||||
|
bot.polling(skip_pending=True) # Skip old updates
|
@ -3140,9 +3140,9 @@ class TeleBot:
|
|||||||
filter_check = self.custom_filters.get(message_filter)
|
filter_check = self.custom_filters.get(message_filter)
|
||||||
if not filter_check:
|
if not filter_check:
|
||||||
return False
|
return False
|
||||||
elif isinstance(filter_check, util.SimpleCustomFilter):
|
elif isinstance(filter_check(), util.SimpleCustomFilter):
|
||||||
return filter_value == filter_check.check(message)
|
return filter_value == filter_check.check(message)
|
||||||
elif isinstance(filter_check, util.AdvancedCustomFilter):
|
elif isinstance(filter_check(), util.AdvancedCustomFilter):
|
||||||
return filter_check.check(message, filter_value)
|
return filter_check.check(message, filter_value)
|
||||||
else:
|
else:
|
||||||
logger.error("Custom filter: wrong type. Should be SimpleCustomFilter or AdvancedCustomFilter.")
|
logger.error("Custom filter: wrong type. Should be SimpleCustomFilter or AdvancedCustomFilter.")
|
||||||
|
Loading…
Reference in New Issue
Block a user