Pass Bot instance explicitly to the URLInputFile

This commit is contained in:
Alex Root Junior 2023-07-08 12:07:12 +03:00
parent 31c151d4a0
commit 08b263dec3
No known key found for this signature in database
GPG key ID: 074C1D455EBEA4AC

View file

@ -4,10 +4,21 @@ import io
import os
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, AsyncGenerator, AsyncIterator, Dict, Iterator, Optional, Union
from typing import (
TYPE_CHECKING,
Any,
AsyncGenerator,
AsyncIterator,
Dict,
Optional,
Union,
)
import aiofiles
if TYPE_CHECKING:
from aiogram.client.bot import Bot
DEFAULT_CHUNK_SIZE = 64 * 1024 # 64 kb
@ -114,6 +125,7 @@ class URLInputFile(InputFile):
filename: Optional[str] = None,
chunk_size: int = DEFAULT_CHUNK_SIZE,
timeout: int = 30,
bot: "Bot" = None,
):
"""
Represents object for streaming files from internet
@ -122,6 +134,9 @@ class URLInputFile(InputFile):
:param headers: HTTP Headers
:param filename: Filename to be propagated to telegram.
:param chunk_size: Uploading chunk size
:param timeout: Timeout for downloading
:param bot: Bot instance to use HTTP session from.
If not specified, will be used current bot from context.
"""
super().__init__(filename=filename, chunk_size=chunk_size)
if headers is None:
@ -130,11 +145,15 @@ class URLInputFile(InputFile):
self.url = url
self.headers = headers
self.timeout = timeout
self.bot = bot
async def read(self, chunk_size: int) -> AsyncGenerator[bytes, None]:
from aiogram.client.bot import Bot
bot = self.bot
if bot is None:
from aiogram.client.bot import Bot
bot = Bot.get_current(no_error=False)
bot = Bot.get_current(no_error=False)
stream = bot.session.stream_content(
url=self.url,
headers=self.headers,