@kolyart2022

Как исправить ошибку с отправкой фото aiogram, python?

Пробую отправить фото в Telegram, используя библиотеку aiogram, но не получается, выходит ошибка.

Код:
import asyncio
from aiogram import Bot, types, Dispatcher
from aiogram.filters.command import Command
from aiogram.enums import ParseMode

import config
import keyboard

bot = Bot(token = config.TOKEN)
dp = Dispatcher()

@dp.message(Command('start'))
async def startMessage(message: types.Message):
	await bot.send_photo(message.chat.id, open('pictures/cover.jpg', 'rb'))


async def main():
	await dp.start_polling(bot)

if __name__ == '__main__':
	asyncio.run(main())


Ошибка:

Cause exception while process update id=343042211 by bot id=7197311239
ValidationError: 2 validation errors for SendPhoto
photo.is-instance[InputFile]
  Input should be an instance of InputFile [type=is_instance_of, input_value=<_io.BufferedReader name='pictures/cover.jpg'>, input_type=BufferedReader]
    For further information visit https://errors.pydantic.dev/2.7/v/is_instance_of
photo.str
  Input should be a valid string [type=string_type, input_value=<_io.BufferedReader name='pictures/cover.jpg'>, input_type=BufferedReader]
    For further information visit https://errors.pydantic.dev/2.7/v/string_type
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 49, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\fsm\middleware.py", line 42, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\router.py", line 142, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\router.py", line 137, in _wrapped
    return await self._propagate_event(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\router.py", line 162, in _propagate_event
    response = await observer.trigger(event, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\cactusmoney\main.py", line 14, in startMessage
    await bot.send_photo(message.chat.id, open('pictures/cover.jpg', 'rb'))
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\aiogram\client\bot.py", line 2918, in send_photo
    call = SendPhoto(
           ^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\pydantic\main.py", line 176, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for SendPhoto
photo.is-instance[InputFile]
  Input should be an instance of InputFile [type=is_instance_of, input_value=<_io.BufferedReader name='pictures/cover.jpg'>, input_type=BufferedReader]
    For further information visit https://errors.pydantic.dev/2.7/v/is_instance_of
photo.str
  Input should be a valid string [type=string_type, input_value=<_io.BufferedReader name='pictures/cover.jpg'>, input_type=BufferedReader]
    For further information visit https://errors.pydantic.dev/2.7/v/string_type
  • Вопрос задан
  • 517 просмотров
Пригласить эксперта
Ответы на вопрос 2
@Everything_is_bad
как насчет сначала почитать доки https://docs.aiogram.dev/en/latest/api/methods/sen... а только потом прибегать сюда?
Ответ написан
shiroyama
@shiroyama
import asyncio
from aiogram import Bot, types, Dispatcher
from aiogram.filters.command import Command
from aiogram.enums import ParseMode
from aiogram.types import FSInputFile
import config
import keyboard

bot = Bot(token = config.TOKEN)
dp = Dispatcher()

@dp.message(Command('start'))
async def startMessage(message: types.Message):
        photo = FSInputFile('pictures/cover.jpg')
        await message.reply_photo(photo=photo)


async def main():
	await dp.start_polling(bot)

if __name__ == '__main__':
	asyncio.run(main())
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы