Answer the question
In order to leave comments, you need to log in
How to pass the message(telebot) value to the decorator?
I want to check if the user is subscribed to the channel when he writes each message. I get this code, but it doesn't work either. How to check this with a decorator and is it possible in principle to check this in every message without crutches?
@client.message_handler(commands = ['start'])
def hello(message):
@check_decorator(message)
def _hello_(message) -> None:
with sqlite3.connect("users.db") as con:
cur = con.cursor()
info = cur.execute('SELECT * FROM users WHERE userid=?', (int(message.chat.id), ))
if info.fetchone() is None:
user_info = (int(message.chat.id), message.from_user.first_name, message.from_user.last_name, undefined_status)
cur.execute(f"""INSERT INTO users VALUES(?, ?, ?, ?);""", user_info)
con.commit()
sti = open('static/welcome.webp', 'rb')
client.send_sticker(message.chat.id, sti)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton(register)
markup.add(item1)
client.send_message(message.chat.id, hello_message.format(message.from_user, client.get_me()), parse_mode='html', reply_markup=markup)
def check_decorator(function, message):
def check_subscribe() -> None:
if client.get_chat_member(channel_chat_id, message.chat.id).status in need_status:
function(message)
else:
client.send_message(message.chat.id, subscribe_error_msg)
return check_subscribe
Answer the question
In order to leave comments, you need to log in
declare a function that takes a message object as input and returns True/False
def check(message):
return client.get_chat_member(channel_chat_id, message.chat.id).status in need_status
@client.message_handler(commands = ['start'], func=check)
@client.message_handler(commands = ['start'], func=lambda m: client.get_chat_member(channel_chat_id, m.chat.id).status in need_status)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question