Home>

Hello everyone! How can I transfer data from callback to state (the output should be a dictionary)

@dp.message_handler(state=Message_User.message_product_id,
content_types=types.ContentTypes.TEXT)
async def state_3(message: types.Message, state: Message_User):
    try:int(message.text)
    except:
        await message.reply("Please repeat ID:")
        return
    await state.update_data(prID=message.text.title())
    markup= types.InlineKeyboardMarkup()
    markup.add(types.InlineKeyboardButton("Key", callback_data='product_tip_event_key'))
    markup.add(types.InlineKeyboardButton("Account", callback_data='product_tip_event_account'))
    markup.add(types.InlineKeyboardButton("Gift", callback_data='product_tip_event_gift'))
    await message.answer(text='Select product type', reply_markup=markup)
    #await Message_User.message_product_tip.set()
#ACTIVATION
@dp.callback_query_handler(lambda event: event.data.startswith('product_tip_event_'))
async def call_send_message(event: types.callback_query.CallbackQuery, state: Message_User):
    await state.update_data(prTip=event.data.replace('product_tip_event_',''))
    await message.answer(text='Enter activation type:')
    await Message_User.message_product_activation.set()

So you kind of add it like that. What exactly is the problem?

oleksandrigo2022-02-14 00:42:13

For some reason, it does not go to the call_send_message function

Mimic2022-02-14 00:42:13
  • Answer # 1

    Try to change it

    @dp.callback_query_handler(lambda event: event.data.startswith('product_tip_event_'))
    async def call_send_message(event: types.callback_query.CallbackQuery, state: Message_User):
    

    To this

    @dp.callback_query_handler(text_startswith='product_tip_event_', state=Message_User)
    async def call_send_message(event: types.CallbackQuery, state: FSMContext):
    

    You are my savior. Thanks a lot! The only thing is to change the state FSMContext from the function to Message_User. (the one I use)

    Mimic2022-02-14 00:42:13

    Alas, you are wrong. I set the FSM context correctly, since we need hints from it. I put the user message in the decorator above, it is there as a filter, which says that this callback handler will only work if the current state is equal to your message to the user. And the fact that inside the function is the state itself, and it should be inherited from the FSM context.

    oleksandrigo2022-02-14 00:42:13

    But why then with Message_user in the function everything works as planned?

    Mimic2022-02-14 00:42:13

    Because the fsm context is just a hint for an autocomplete idea

    oleksandrigo2022-02-13 06:30:10