Сохраненное видео не отправляется через Aiogram
Код:
import asyncio
import os
import config
import sys
import logging
from aiogram.enums import ParseMode, ChatAction
from aiogram.filters import CommandStart, Command
from aiogram.types import Message
from aiogram import Bot, Dispatcher
from pytube import YouTube
bot = Bot(token=config.BOT_TOKEN)
dp = Dispatcher()
@dp.message(CommandStart())
async def command_start_handler(message: Message):
await message.answer('Для скачивания видео пропишите /download "ссылка"')
@dp.message(Command('download'))
async def command_download_handler(message: Message):
try:
video_url = message.text.split()[1]
save_path = "./video"
await message.bot.send_chat_action(chat_id=message.chat.id, action=ChatAction.UPLOAD_VIDEO)
yt = YouTube(video_url)
stream = yt.streams.filter(resolution='720p', progressive=True).first()
video_file = stream.download(output_path=save_path)
await message.answer(f"Видео '{yt.title}' успешно загружено!")
with open(video_file, 'rb') as v:
await bot.send_video(chat_id=message.chat.id, video=v)
os.remove(video_file)
await message.answer("Видео успешно отправлено и удалено с сервера.")
except Exception as e:
await message.answer(f"Произошла ошибка: {str(e)}")
async def main():
global dp
bot = Bot(config.BOT_TOKEN, parse_mode=ParseMode.HTML)
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
Ошибка:
TelegramBadRequest: Telegram server says - Bad Request: can't parse entities: Unsupported start tag "_io.bufferedreader" at byte offset 173
Traceback (most recent call last):
File "C:\Users\super\PycharmProjects\videobot\main.py", line 32, in command_download_handler
await bot.send_video(chat_id=message.chat.id, video=v)
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\client\bot.py", line 3129, in send_video
call = SendVideo(
^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\pydantic\main.py", line 164, in __init__
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for SendVideo
video.is-instance[InputFile]
Input should be an instance of InputFile [type=is_instance_of, input_value=<_io.BufferedReader name=... и ракушки.mp4'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.5/v/is_instance_of
video.str
Input should be a valid string [type=string_type, input_value=<_io.BufferedReader name=... и ракушки.mp4'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.5/v/string_type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 309, in _process_update
response = await self.feed_update(bot, update, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 158, in feed_update
response = await self.update.wrap_outer_middleware(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
return await handler(event, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 27, in __call__
return await handler(event, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\fsm\middleware.py", line 41, in __call__
return await handler(event, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
return await wrapped_inner(event, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
return await wrapped()
^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\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\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 128, in propagate_event
return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 123, in _wrapped
return await self._propagate_event(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\router.py", line 148, in _propagate_event
response = await observer.trigger(event, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
return await wrapped_inner(event, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
return await wrapped()
^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\main.py", line 38, in command_download_handler
await message.answer(f"Произошла ошибка: {str(e)}")
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\methods\base.py", line 84, in emit
return await bot(self)
^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\client\bot.py", line 492, in __call__
return await self.session(self, method, timeout=request_timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\client\session\base.py", line 254, in __call__
return cast(TelegramType, await middleware(bot, method))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\client\session\aiohttp.py", line 178, in make_request
response = self.check_response(
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\super\PycharmProjects\videobot\.venv\Lib\site-packages\aiogram\client\session\base.py", line 120, in check_response
raise TelegramBadRequest(method=method, message=description)
aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: can't parse entities: Unsupported start tag "_io.bufferedreader" at byte offset 173