M
M
MIKHAN_GO2021-06-27 20:41:05
Python
MIKHAN_GO, 2021-06-27 20:41:05

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


Traceback (most recent call last):
File "C:\Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\util.py", line 62, in run
task(* args, **kwargs)
File "d:/code/github.com/MIKHANGO/rueducationbot/bot.py", line 81, in hello
@check_decorator(message)
TypeError: check_decorator() missing 1 required positional argument: 'message'
"
Traceback (most recent call last):
File "d:/code/github.com/MIKHANGO/rueducationbot/bot.py", line 274, in
client. polling(none_stop = True, interval = 0)
File "C:\ Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\__init__.py", line 427, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C:\Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\__init__.py", line 451, in __threaded_polling
self .worker_pool.raise_exceptions()
File "C:\Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\util.py", line 111, in raise_exceptions
six.reraise(self .exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\six.py ", line 703, in reraise
raise value
File "C:\Users\MIKHAN_GO\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\util.py", line 62, in run
task( *args,**kwargs)
File "d:/code/github.com/MIKHANGO/rueducationbot/bot.py", line 81, in hello
@check_decorator(message)
TypeError: check_decorator() missing 1 required positional argument: 'message'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-06-27
@MIKHAN_GO

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

pass this function (not the result of the call, but the object) to the func parameter in the main handler
@client.message_handler(commands = ['start'], func=check)

Or via lambdas:
@client.message_handler(commands = ['start'], func=lambda m: client.get_chat_member(channel_chat_id, m.chat.id).status in need_status)

In general, nothing seems to prevent you from doing this check already inside the function. If the check did not pass - the corresponding message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question