Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

callback - Python. Problem with aiogram, decorator in decorator

The @ dp.callback_query_handler decorator keeps the first message, which makes it impossible to work with the second message. Why does it keep iterating over and over? How to do that would not save or how to split decorators (which would be in one line), but get access i_need_this_info. And also I need message.message_id

After first click enter image description here

After second click enter image description here

import logging

from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor


API_TOKEN = '12345:YOUR_TOKEN'

logging.basicConfig(level=logging.INFO)

bot = Bot(token=API_TOKEN)

dp = Dispatcher(bot)


@dp.message_handler()
async def first(message=types.Message):

    markup = types.InlineKeyboardMarkup(resize_keyboard=True, selective=True)
    markup.add(types.InlineKeyboardButton('Фото ??', callback_data='btn1'))
    markup.add(types.InlineKeyboardButton('Video ??', callback_data='btn2'))
    i_need_this_info = await message.answer('get choice',             reply_markup=markup)
    print(message.message_id)

    @dp.callback_query_handler(lambda c: c.data)
    async def second(callback_query: types.CallbackQuery):
        code = callback_query.data.replace('btn', '')
        print(message.message_id)

        if code == '1':
            await message.answer("first")
        elif code == '2':
            await message.answer("second")

        await bot.delete_message(message.from_user.id,     i_need_this_info.message_id)


if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)
question from:https://stackoverflow.com/questions/66045785/python-problem-with-aiogram-decorator-in-decorator

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I`m just create the template list

import logging

from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor


logging.basicConfig(level=logging.INFO)

API_TOKEN = "12345:YOUR_TOKEN"

bot = Bot(token=API_TOKEN)

dp = Dispatcher(bot)


class Template:

    def __init__(self):
        self.tmp = []

    def set(self, arg: object):
        self.tmp.append(arg)

    def get(self):
        return self.tmp.pop()


message_tmp = Template()
i_need_this_info_tmp = Template()


@dp.message_handler()
async def first(message=types.Message):
    markup = types.InlineKeyboardMarkup(resize_keyboard=True, selective=True)
    markup.add(types.InlineKeyboardButton('Фото ??', callback_data='btn1'))
    markup.add(types.InlineKeyboardButton('Video ??', callback_data='btn2'))
    i_need_this_info = await message.answer('get choice', reply_markup=markup)
    i_need_this_info_tmp.set(i_need_this_info)
    print(message.message_id)

    message_tmp.set(message)


@dp.callback_query_handler(lambda call: True)
async def second(callback_query: types.CallbackQuery):
    code = callback_query.data.replace('btn', '')
    message = message_tmp.get()
    i_need_this_info = i_need_this_info_tmp.get()
    print(message.message_id)

    if code == '1':
        await message.answer("first")
    elif code == '2':
        await message.answer("second")

    await bot.delete_message(message.from_user.id, i_need_this_info.message_id)


if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...