【问题标题】:Changing the photo in the message by pressing the inline button Aiogram通过按内联按钮 Aiogram 更改消息中的照片
【发布时间】:2023-01-17 11:57:24
【问题描述】:

机器人解析来自站点的数据并将所有内容写入列表。每次单击“下一步”按钮时,我都想显示上面列表中的下一张照片。

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)

我刚刚开始与 Python 编程语言密切合作,并决定尝试创建一个机器人,但我遇到了一个我无法解决的问题。我试图通过 for 循环,但结果有点糟糕,通过单击所有照片快速更改为列表中的最后一张。

【问题讨论】:

    标签: python telegram-bot aiogram


    【解决方案1】:

    尝试使用 InputMediaPhoto() 而不是 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)
    

    【讨论】:

    • 这根本不是重点。可以说,问题在于通过按一下按钮在列表项之间“切换”。获取列表中的下一项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2016-09-11
    相关资源
    最近更新 更多