1

The bot parses data from the site and writes everything to the list. I want to show the next photo from the above list every time I click "next" button.

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputMedia


poster = ['any url photo', 'any url photo', 'any url photo']


next_btn = InlineKeyboardButton(text='next', callback_data='next')

neBTN = InlineKeyboardMarkup()
neBTN.add(next_btn)


@dp.message_handler(commands=["start"])
async def photo(message: types.Message):
    file_path = poster[1]
    reply_markup = neBTN

    await bot.send_photo(
        message.chat.id,
        photo=file_path,
        reply_markup=reply_markup,
        caption="Test caption!",
    )


@dp.callback_query_handler(text="next")
async def photo_update(query: types.CallbackQuery):
    file_path = poster[2]
    reply_markup = neBTN
    file = InputMedia(media=file_path, caption="Updated caption :)")

    await query.message.edit_media(file, reply_markup=reply_markup)

I just started working closely with the Python programming language and decided to try to create a bot, but I ran into a problem that I can not solve. I tried to go through the for loop, but it turns out somehow badly, by clicking all the photos quickly change to the last one from the list.

1 Answers1

0

Try to use InputMediaPhoto() instead of InputMedia().

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto
from dotenv import load_dotenv

# Import module to create an iterable object
from itertools import cycle
import os

load_dotenv()
TOKEN = os.getenv("TOKEN", "")
bot = Bot(TOKEN)
dp = Dispatcher(bot)

poster = [
    "https://pic.rutubelist.ru/video/17/b1/17b100a0bcbc6e5e8d11101cde21aca7.jpg",
    "https://funik.ru/wp-content/uploads/2018/10/6db3f15d0a21589aaa1b.jpg",
    "https://psihoman.ru/uploads/posts/2022-02/1645693727_1645693779.jpg",
]
next_btn = InlineKeyboardButton(text="next", callback_data="next")
# Thats create an iterable object of our list of links or paths
cycle_poster = cycle(poster)

neBTN = InlineKeyboardMarkup()
neBTN.add(next_btn)


@dp.message_handler(commands=["start"])
async def photo(message: types.Message):
    # iteration
    file_path = next(cycle_poster)
    reply_markup = neBTN

    await bot.send_photo(
        message.chat.id,
        photo=file_path,
        reply_markup=reply_markup,
        caption="Test caption!",
    )


@dp.callback_query_handler(text="next")
async def photo_update(query: types.CallbackQuery):
    file_path = next(cycle_poster)
    reply_markup = neBTN
    file = InputMediaPhoto(media=file_path, caption="Updated caption :)")

    await query.message.edit_media(file, reply_markup=reply_markup)


if __name__ == "__main__":
    executor.start_polling(skip_updates=True, dispatcher=dp)
  • That's not the point at all. The problem lies, so to speak, in "switching" between the list items at the touch of a button. To take the next item in the list. – Pizza_Chicago Jan 13 '23 at 16:38