0

In general, I recently found api for rule34. I want to make the integration of photos into the bot from there. Starts looking for a photo, and sometimes an error pops up:

aiogram.exceptions.TelegramBadRequest: aiogram.exceptions.TelegramBadRequest: Telegram server says Bad Request: wrong file identifier/HTTP URL specified

The handler looks like this:

@user_hl_router.callback_query(Text(text='forward'))
async def forward(call: CallbackQuery):
    if db[call.from_user.id]['page'] > 0:
        db[call.from_user.id]['page'] += 1
        db[call.from_user.id]['page'] = is_photo(db[call.from_user.id]['current_tags'],
                                                 db[call.from_user.id]['page'],
                                                 call.data)
        await call.message.answer_photo(r34_api.search(db[call.from_user.id]['current_tags'],
                                                       db[call.from_user.id]['page'],
                                                       limit=1)[0].image,
                                                       reply_markup=create_pagination_keyboard('backward',
                                                                                               'page',
                                                                                               'forward'))

is_photo() function:

def is_photo(tags: list, page: int, btn_status: str):
    while r34.search(tags=tags, page_id=page,
                     limit=1)[0].content_type != 'image':
        if btn_status == 'backward':
            page -= 1

        elif btn_status == 'forward':
            page += 1

    return page

Library for rule34:

rule34Py

User dictionary:

    if msg.from_user.id not in db.keys():
        db[int(msg.from_user.id)] = {'name': msg.from_user.first_name,
                                     'notification_tag': list,
                                     'current_tags': [],
                                     'page': 1,
                                     'current_menu': 1}

And if possible, you can help make the bot either delete the previous message and send a new one instead, or (at best), replace the old photo with a new one

I tried to catch the exception and execute the code until the photo is sent, but it does not want to be caught:

@user_hl_router.callback_query(Text(text='forward'))
async def forward(call: CallbackQuery):
    if db[call.from_user.id]['page'] > 0:
        error_code = 1
        db[call.from_user.id]['page'] += 1
        db[call.from_user.id]['page'] = is_photo(db[call.from_user.id]['current_tags'],
                                                 db[call.from_user.id]['page'],
                                                 call.data)
        await call.message.delete()
        while error_code != 0:
            try:
                await call.message.answer_photo(r34_api.search(db[call.from_user.id]['current_tags'],
                                                             db[call.from_user.id]['page'],
                                                             limit=1)[0].image,
                                                             reply_markup=create_pagination_keyboard('backward',
                                                                                                     'page',
                                                                                                     'forward'))
                error_code = 0
            except exceptions.TelegramBadRequest:
                await call.message.answer_photo(r34_api.search(db[call.from_user.id]['current_tags'],
                                                             db[call.from_user.id]['page'],
                                                             limit=1)[0].image,
                                                             reply_markup=create_pagination_keyboard('backward',
                                                                                                     'page',
                                                                                                     'forward'))

0 Answers0