Search code examples
pythontelegramaiogram

How to make the bot send a random photo to aiogram?


I need to force the telegram bot on aiogram to send a random photo to the chat, how can I do this. Thanks in advance for your reply


Solution

  • You can send photo from your files. You also can send photo by file_id, the principle is the same:

        # import aiogram and random
        @dp.message_handler(commands=["photo"])
        async def sendphoto(msg):
            arr = ["image1path", "image2path", ...] # or imagefileid
            photo = open(random.choice(arr), "rb")
            await bot.send_photo(msg.from_user.id, photo)