I
I
i_ikigai2020-06-29 22:42:07
Python
i_ikigai, 2020-06-29 22:42:07

How to get the text of the last message in aiogram or by ID?

I want to understand how to receive data from the user using the example of processing expressions
, there is a button

@dp.message_handler()
async def process_command_1(message: types.Message):
    if message.text == 'Математические операции':
        await message.answer("Напишите выражение и нажмите кнопку", reply_markup=kb.inline_kb)

inline_res = InlineKeyboardButton('Рассчитать', callback_data='res')

Click handling
@dp.callback_query_handler(lambda c: c.data == 'res')
async def process_callback_button1(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    await bot.send_message(callback_query.from_user.id, f'{callback_query.message.text}')

Receives the text "Write an expression and click the button", I would like to get the text from the user instead and, for example, just process it via eval, but I can't figure out how to process the next message and not the current one

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavlks, 2020-07-09
@Pavlks

Here you already need to work with states. States - in English. I learned here link

from aiogram.dispatcher.filters.state import State, StatesGroup

class Mydialog(StatesGroup):
    otvet = State()  # Will be represented in storage as 'Mydialog:otvet'

#Здесь мы начинаем общение с клиентом и включаем состояния
@dp.message_handler()
async def cmd_dialog(message: types.Message):
    await Mydialog.otvet.set()  # вот мы указали начало работы состояний (states)

    await message.reply("Человечишка, напиши мне свое жалкое мнение")

# А здесь получаем ответ, указывая состояние и передавая сообщение пользователя
@dp.message_handler(state=Mydialog.otvet)
async def process_message(message: types.Message, state: FSMContext):

    async with state.proxy() as data:
        data['text'] = message.text
        user_message = data['text']

        # дальше ты обрабатываешь сообщение, ведешь рассчеты и выдаешь ему ответ.
       otvet_klienty = 'bla, bla, bla'

        await bot.send_message(
            message.from_user.id,
            otvet_klienty ,
            reply_markup=markup,
            parse_mode='HTML',
        )

    # Finish conversation
    await state.finish()  # закончили работать с сотояниями

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question