如何在 aiogram 状态期间检测内联 Python 按钮?

huangapple go评论57阅读模式
英文:

how to detect the Inline python button during the state aiogram

问题

以下是您要翻译的代码部分:

from aiogram import Bot, Dispatcher, executor, types
from aiogram.dispatcher import FSMContext
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.dispatcher.filters.state import State, StatesGroup

storage = MemoryStorage()
bot = Bot(token=config.bot_token)
dp = Dispatcher(bot, storage=storage)

class hi(StatesGroup):
    hello = State()

@dp.message_handler(commands='start')
async def start(message: types.Message):

    repl = InlineKeyboardMarkup(row_width=1).add(InlineKeyboardButton(text='Cancel', callback_data='Cancel'))
    await bot.send_message(message.from_user.id, 'how are you?', reply_markup=repl)
    await hi.hello.set()

@dp.message_handler(state=hi.hello)
async def efe(message: types.Message, state: FSMContext):
    await bot.send_message(message.from_user.id, f'me too {message.text}')
    await state.finish()

@dp.callback_query_handler(text='Cancel')
async def ponn(message: types.CallbackQuery, state: FSMContext):
    await state.finish()
    await bot.send_message(message.from_user.id, 'Canceled')

if __name__ == '__main__':

    print('bot work')
    executor.start_polling(dp, skip_updates=True)
英文:

there is a code:


from aiogram import Bot, Dispatcher, executor, types
from aiogram.dispatcher import FSMContext
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.dispatcher.filters.state import State, StatesGroup

storage = MemoryStorage()
bot = Bot(token=config.bot_token)
dp = Dispatcher(bot, storage=storage)

class hi(StatesGroup):
    hello = State()

@dp.message_handler(commands='start')
async def start(message: types.Message):

    repl = InlineKeyboardMarkup(row_width=1).add(InlineKeyboardButton(text='Cancel', callback_data='Cancel'))
    await bot.send_message(message.from_user.id, 'how are you?', reply_markup=repl)
    await hi.hello.set()


@dp.message_handler(state=hi.hello)
async def efe(message: types.Message, state: FSMContext):
    await bot.send_message(message.from_user.id, f'me too {message.text}')
    await state.finish()

@dp.callback_query_handler(text='Cancel')
async def ponn(message: types.CallbackQuery, state: FSMContext):
    await state.finish()
    await bot.send_message(message.from_user.id, f'Canceled')


if __name__ == '__main__':

    print('bot work')
    executor.start_polling(dp, skip_updates=True)

in short, in this code the /start command launches state.set() and + inline keyboard is attached to the message. after entering the start command, the bot will wait for the text from the user, and then display it. but if at the stage when the bot is waiting for the text the user presses the inline button, the bot will display a message that it has stopped waiting for the text and will stop waiting for it

That would be fine, but the handler calback does not see the inline button pressed during state

in this code the /start command launches state.set() and + inline keyboard is attached to the message. after entering the start command, the bot will wait for the text from the user, and then display it. but if at the stage when the bot is waiting for the text the user presses the inline button, the bot will display a message that it has stopped waiting for the text and will stop waiting for it

答案1

得分: 1

@dp.callback_query_handler(text='Cancel', state=hi.hello)
async def ponn(message: types.CallbackQuery, state: FSMContext):
await state.finish()
await bot.send_message(message.from_user.id, 'Canceled')

try like this

英文:
@dp.callback_query_handler(text='Cancel', state=hi.hello)
async def ponn(message: types.CallbackQuery, state: FSMContext):
    await state.finish()
    await bot.send_message(message.from_user.id, f'Canceled')

try like this

huangapple
  • 本文由 发表于 2023年3月21日 03:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定