[Python] aiogram. can the bot send a reaction to a message in a personal chat, after using the...

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    The bot sends the photo from the personal chat to the group and attaches the InlineKeyboardButton accept to the photo. after pressing the InlineKeyboardButton accept in the group chat, the bot should set the reaction to the previously forwarded photo in the personal chat. can this be done? my code does not work correctly: it puts the reaction under the photo in the group, and not in the personal chat. I need to somehow show in personal chat that the photo was approved in the group chat. tried other ways, but I'm not doing something right, I get an error: aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: message to react not found. Any help is appreciated! Thanks!

    `# keyboard with buttons (works correctly)
    approval_keyboard = InlineKeyboardMarkup(inline_keyboard=[
    [InlineKeyboardButton(text='✅ accept ', callback_data='accept'),
    InlineKeyboardButton(text='❌ reject ', callback_data='reject')]`


    `# sending photos to a group (works correctly)
    @router.message(F.photo)
    async def photo(message: Message):
    if message.chat.type == 'private':
    images_id[message.photo[-1].file_unique_id + message.caption] = message.message_id #!
    await bot.send_photo(supergroup_id, message_thread_id=177,
    photo=message.photo[-1].file_id,
    caption=message.caption,
    reply_markup=approval_keyboard)`


    # setting the reaction under the photo (here is my problem)
    @router.callback_query(F.data == 'accept')
    async def accept_button(callback: CallbackQuery):
    await bot.set_message_reaction(
    callback.message.reply_to_message.chat.id,
    images_id.pop(callback.message.photo[-1].file_unique_id + callback.message.caption), #!
    #callback.message.message_id, - wrong code
    reaction=[{"type": "emoji", "emoji": ""}])


    MY SOLUTION:

    When sending a photo to a bot, I create an entry in the dictionary "images_id":

    images_id[message.photo[-1].file_unique_id + message.caption] = message.message_id


    After clicking the accept button, I access the dictionary using the pop() method to get the value and remove it from the dictionary.

    images_id.pop(callback.message.photo[-1].file_unique_id + callback.message.caption)


    "+ message.caption" - you can not add it, it was just important to me.

    updated the original code. please do not immediately delete my question if possible. i'm quite new to python and aiogram. I will be very glad if there is a more correct and concise solution. Thank you.

    Continue reading...

Compartilhe esta Página