Fixed files uploading

This commit is contained in:
Alex Root Junior 2023-03-11 01:07:22 +02:00
parent b8b97b9ed9
commit d134c9019b
No known key found for this signature in database
GPG key ID: 074C1D455EBEA4AC
13 changed files with 65 additions and 52 deletions

View file

@ -121,7 +121,6 @@ from ..methods import (
SetStickerKeywords,
SetStickerMaskPosition,
SetStickerPositionInSet,
SetStickerSetThumb,
SetStickerSetThumbnail,
SetStickerSetTitle,
SetWebhook,
@ -3312,32 +3311,6 @@ class Bot(ContextInstanceMixin["Bot"]):
)
return await self(call, request_timeout=request_timeout)
async def set_sticker_set_thumb(
self,
name: str,
user_id: int,
thumb: Optional[Union[InputFile, str]] = None,
request_timeout: Optional[int] = None,
) -> bool:
"""
Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Video thumbnails can be set only for video sticker sets only. Returns :code:`True` on success.
Source: https://core.telegram.org/bots/api#setstickersetthumb
:param name: Sticker set name
:param user_id: User identifier of the sticker set owner
:param thumb: A **PNG** image with the thumbnail, must be up to 128 kilobytes in size and have width and height exactly 100px, or a **TGS** animation with the thumbnail up to 32 kilobytes in size; see `https://core.telegram.org/stickers#animated-sticker-requirements <https://core.telegram.org/stickers#animated-sticker-requirements>`_`https://core.telegram.org/stickers#animated-sticker-requirements <https://core.telegram.org/stickers#animated-sticker-requirements>`_ for animated sticker technical requirements, or a **WEBM** video with the thumbnail up to 32 kilobytes in size; see `https://core.telegram.org/stickers#video-sticker-requirements <https://core.telegram.org/stickers#video-sticker-requirements>`_`https://core.telegram.org/stickers#video-sticker-requirements <https://core.telegram.org/stickers#video-sticker-requirements>`_ for video sticker technical requirements. Pass a *file_id* as a String to send a file that already exists on the Telegram servers, pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. :ref:`More information on Sending Files » <sending-files>`. Animated sticker set thumbnails can't be uploaded via HTTP URL.
:param request_timeout: Request timeout
:return: Returns :code:`True` on success.
"""
call = SetStickerSetThumb(
name=name,
user_id=user_id,
thumb=thumb,
)
return await self(call, request_timeout=request_timeout)
async def set_webhook(
self,
url: str,

View file

@ -23,6 +23,7 @@ from aiogram.methods import Request, TelegramMethod
from ...exceptions import TelegramNetworkError
from ...methods.base import TelegramType
from ...types import InputFile
from .base import UNSET, BaseSession
if TYPE_CHECKING:

View file

@ -35,3 +35,27 @@ class RequestLogging(BaseRequestMiddleware):
bot.id,
)
return await make_request(bot, method)
class VerboseRequestLogging(BaseRequestMiddleware):
def __init__(self, ignore_methods: Optional[List[Type[TelegramMethod[Any]]]] = None):
"""
Middleware for logging outgoing requests
:param ignore_methods: methods to ignore in logging middleware
"""
self.ignore_methods = ignore_methods if ignore_methods else []
async def __call__(
self,
make_request: NextRequestMiddlewareType[TelegramType],
bot: "Bot",
method: TelegramMethod[TelegramType],
) -> Response[TelegramType]:
if type(method) not in self.ignore_methods:
loggers.middlewares.info(
"Make request with method=%r by bot id=%d",
method,
bot.id,
)
return await make_request(bot, method)

View file

@ -1,9 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from typing import TYPE_CHECKING, Any, Dict
from ..types import InputFile, InputSticker, MaskPosition
from .base import Request, TelegramMethod, prepare_file
from ..types import InputFile, InputSticker
from .base import Request, TelegramMethod, prepare_input_sticker
if TYPE_CHECKING:
from ..client.bot import Bot
@ -26,11 +26,9 @@ class AddStickerToSet(TelegramMethod[bool]):
"""A JSON-serialized object with information about the added sticker. If exactly the same sticker had already been added to the set, then the set isn't changed."""
def build_request(self, bot: Bot) -> Request:
data: Dict[str, Any] = self.dict(exclude={"png_sticker", "tgs_sticker", "webm_sticker"})
data: Dict[str, Any] = self.dict()
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="png_sticker", value=self.png_sticker)
prepare_file(data=data, files=files, name="tgs_sticker", value=self.tgs_sticker)
prepare_file(data=data, files=files, name="webm_sticker", value=self.webm_sticker)
prepare_input_sticker(input_sticker=data["sticker"], files=files)
return Request(method="addStickerToSet", data=data, files=files)

View file

@ -100,10 +100,10 @@ class TelegramMethod(abc.ABC, BaseModel, Generic[TelegramType]):
def prepare_file(name: str, value: Any, data: Dict[str, Any], files: Dict[str, Any]) -> None:
if not value:
return
if name == "thumb":
if name == "thumbnail":
tag = secrets.token_urlsafe(10)
files[tag] = value
data["thumb"] = f"attach://{tag}"
data["thumbnail"] = f"attach://{tag}"
elif isinstance(value, InputFile):
files[name] = value
else:
@ -122,6 +122,22 @@ def prepare_input_media(data: Dict[str, Any], files: Dict[str, InputFile]) -> No
input_media["media"] = f"attach://{tag}"
def prepare_input_sticker(input_sticker: Dict[str, Any], files: Dict[str, InputFile]) -> None:
if (
"sticker" in input_sticker
and input_sticker["sticker"]
and isinstance(input_sticker["sticker"], InputFile)
):
tag = secrets.token_urlsafe(10)
files[tag] = input_sticker.pop("sticker")
input_sticker["sticker"] = f"attach://{tag}"
def prepare_input_stickers(data: Dict[str, Any], files: Dict[str, InputFile]) -> None:
for input_sticker in data["stickers"]:
prepare_input_sticker(input_sticker, files=files)
def prepare_media_file(data: Dict[str, Any], files: Dict[str, InputFile]) -> None:
if (
data["media"]

View file

@ -1,9 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from ..types import InputFile, InputSticker, MaskPosition
from .base import Request, TelegramMethod, prepare_file
from ..types import InputFile, InputSticker
from .base import Request, TelegramMethod, prepare_input_sticker, prepare_input_stickers
if TYPE_CHECKING:
from ..client.bot import Bot
@ -34,11 +34,9 @@ class CreateNewStickerSet(TelegramMethod[bool]):
"""Pass :code:`True` if stickers in the sticker set must be repainted to the color of text when used in messages, the accent color if used as emoji status, white on chat photos, or another appropriate color based on context; for custom emoji sticker sets only"""
def build_request(self, bot: Bot) -> Request:
data: Dict[str, Any] = self.dict(exclude={"png_sticker", "tgs_sticker", "webm_sticker"})
data: Dict[str, Any] = self.dict()
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="png_sticker", value=self.png_sticker)
prepare_file(data=data, files=files, name="tgs_sticker", value=self.tgs_sticker)
prepare_file(data=data, files=files, name="webm_sticker", value=self.webm_sticker)
prepare_input_stickers(data=data, files=files)
return Request(method="createNewStickerSet", data=data, files=files)

View file

@ -71,6 +71,6 @@ class SendAnimation(TelegramMethod[Message]):
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="animation", value=self.animation)
prepare_file(data=data, files=files, name="thumb", value=self.thumb)
prepare_file(data=data, files=files, name="thumbnail", value=self.thumbnail)
return Request(method="sendAnimation", data=data, files=files)

View file

@ -70,6 +70,6 @@ class SendAudio(TelegramMethod[Message]):
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="audio", value=self.audio)
prepare_file(data=data, files=files, name="thumb", value=self.thumb)
prepare_file(data=data, files=files, name="thumbnail", value=self.thumbnail)
return Request(method="sendAudio", data=data, files=files)

View file

@ -65,6 +65,6 @@ class SendDocument(TelegramMethod[Message]):
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="document", value=self.document)
prepare_file(data=data, files=files, name="thumb", value=self.thumb)
prepare_file(data=data, files=files, name="thumbnail", value=self.thumbnail)
return Request(method="sendDocument", data=data, files=files)

View file

@ -73,6 +73,6 @@ class SendVideo(TelegramMethod[Message]):
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="video", value=self.video)
prepare_file(data=data, files=files, name="thumb", value=self.thumb)
prepare_file(data=data, files=files, name="thumbnail", value=self.thumbnail)
return Request(method="sendVideo", data=data, files=files)

View file

@ -55,6 +55,6 @@ class SendVideoNote(TelegramMethod[Message]):
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="video_note", value=self.video_note)
prepare_file(data=data, files=files, name="thumb", value=self.thumb)
prepare_file(data=data, files=files, name="thumbnail", value=self.thumbnail)
return Request(method="sendVideoNote", data=data, files=files)

View file

@ -3,7 +3,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from ..types import InputFile
from .base import Request, TelegramMethod
from .base import Request, TelegramMethod, prepare_file
if TYPE_CHECKING:
from ..client.bot import Bot
@ -28,4 +28,7 @@ class SetStickerSetThumbnail(TelegramMethod[bool]):
def build_request(self, bot: Bot) -> Request:
data: Dict[str, Any] = self.dict()
files: Dict[str, InputFile] = {}
prepare_file("thumbnail", value=self.thumbnail, data=data, files=files)
return Request(method="setStickerSetThumbnail", data=data)

View file

@ -26,9 +26,9 @@ class UploadStickerFile(TelegramMethod[File]):
"""Format of the sticker, must be one of 'static', 'animated', 'video'"""
def build_request(self, bot: Bot) -> Request:
data: Dict[str, Any] = self.dict(exclude={"png_sticker"})
data: Dict[str, Any] = self.dict(exclude={"sticker"})
files: Dict[str, InputFile] = {}
prepare_file(data=data, files=files, name="png_sticker", value=self.png_sticker)
prepare_file(data=data, files=files, name="sticker", value=self.sticker)
return Request(method="uploadStickerFile", data=data, files=files)