E
E
enabl32021-04-28 12:04:45
Python
enabl3, 2021-04-28 12:04:45

How to get response from inline button in aiogram?

Hello everyone, please advise.
As in the aiogram library, if the user must either enter text or click on a button. Handle button click?

I send this message by clicking on the "complaint" button, where you need to either describe the complaint or click on the "Cancel" button to exit.

# States
class Step(StatesGroup):
claim = State()

elif call.data == "claim":
        await call.message.edit_text(text="Жалоба")
        await Step.claim.set()
        await call.message.answer("Опиши подробно свою жалобу", reply_markup=keyboards.cancelKeyboard)


and here I process the received message:
how to add another handler for the "cancel" button here, since by clicking on it, a watch hangs on it and nothing happens.
I know that nothing is written about it in the code below, but I tried it in different ways - nothing happened.
@dp.message_handler(state=Step.claim)
async def process_message(message: Message, state: FSMContext):
    async with state.proxy() as data:
        data['claim'] = message.text
        claim = data['claim']
        await bot.send_message(CLAIMS_ID, claim)
        await message.reply(text="Жалоба отправлена")
        await state.finish()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
me, 2021-04-28
@dabudi

I would probably do something like this handling the complaint

import telebot
bot = telebot.TeleBot('токен') 



@bot.message_handler(commands=['start'])
def start(message):
    keyboard = types.InlineKeyboardMarkup()
    keyboard.add(types.InlineKeyboardButton(text='Оставить жалобу', callback_data='ZHALOBA'))            
    bot.send_message(message.chat.id, 'Главное меню', reply_markup=keyboard) 


@bot.callback_query_handler(func=lambda call: True)
def ans(call):
    if call.data == 'ZHALOBA':
        func1(call.message, call)

def func1(message, call):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(*[types.KeyboardButton(name) for name in ['Назад']])
    bot.send_message(message.chat.id, "Отправьте мне текст для жалобы", reply_markup=keyboard)
@bot.message_handler(content_types=['text'])
def test(message):
    if message.text == 'Назад':
        start(message)
        
    elif message.text != None:
        zhaloba = message.text

if __name__ == '__main__':
    while True:
        try:
            bot.polling(none_stop=True)
        except:
            time.sleep(5)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question