M
M
MAMA2002021-07-12 11:27:22
Python
MAMA200, 2021-07-12 11:27:22

Aigoram telegram bot. Sends photos when not needed. How to fix?

UPD:
They suggested that it is necessary to use the Finite State Machine (FSM) here.
First time I hear about it (I'm only learning).

I am finishing my telegram bot.
When the button is pressed, it should display the message "XXXXX", after which the user sends a photo to the bot, which he forwards to another chat. This completes the cycle.

However, everything works crookedly.
see below*

There is a piece of code:

b1 = InlineKeyboardButton('XXXXXXXXXX', callback_data='adm',)
b2 = InlineKeyboardButton('XXXXXXXXXX', callback_data='art')
Back = InlineKeyboardButton('XXXXXXXX', callback_data='back')

KBB = InlineKeyboardMarkup().add(Back)
KB = InlineKeyboardMarkup().add(b1).add(b2)



@dp.message_handler(commands=['start'])
async def firstmessage(msg: types.Message):
    dir1 = 'XXXXXXXX'
    with open(dir1, 'rb') as pic1:
        await bot.send_photo(msg.from_user.id, pic1, caption=XXXXX, reply_markup=KB)


@dp.callback_query_handler(lambda c: c.data == 'back')
async def back_to_menu(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    dir2 = 'XXXXXXXXXX'
    with open(dir2, 'rb') as pic2:
        await bot.send_photo(callback_query.from_user.id, pic2, caption='XXXXXX', reply_markup=KB)
   


@dp.callback_query_handler(lambda c: c.data == 'adm')
async def admcontact(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    await bot.send_message(callback_query.from_user.id, 'XXXXXXXXX', reply_markup=KBB)



@dp.callback_query_handler(lambda c: c.data == 'art')
async def artpred(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    await bot.send_message(callback_query.from_user.id, 'XXXXXXX', reply_markup=KBB)
    @dp.message_handler(content_types=["photo"])
    async def replying(pic):
        await bot.send_photo(XXXXXXXXXXX, pic.photo[-1].file_id)
        await bot.send_message(pic.from_user.id, 'XXXXXXXX', reply_markup=KBB)


If the bot sends a photo BEFORE the line:
@dp.callback_query_handler(lambda c: c.data == 'art')
async def artpred(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    await bot.send_message(callback_query.from_user.id, 'XXXXXXX', reply_markup=KBB)
    @dp.message_handler(content_types=["photo"])
    async def replying(pic):
        await bot.send_photo(XXXXXXXXXXX, pic.photo[-1].file_id)
        await bot.send_message(pic.from_user.id, 'XXXXXXXX', reply_markup=KBB)

He doesn't respond to her. That's the way it should be.

If you press the b2 button (that is, activate this very piece), and then go back to another keyboard, then the bot will already send the sent photo when it is not necessary.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question