diff --git a/aiogram/client/bot.py b/aiogram/client/bot.py index fe5423c2..91dd91fa 100644 --- a/aiogram/client/bot.py +++ b/aiogram/client/bot.py @@ -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 `_ 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 `_ 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 ยป `. 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, diff --git a/aiogram/client/session/aiohttp.py b/aiogram/client/session/aiohttp.py index 4d9dff06..3f35a0e0 100644 --- a/aiogram/client/session/aiohttp.py +++ b/aiogram/client/session/aiohttp.py @@ -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: diff --git a/aiogram/client/session/middlewares/request_logging.py b/aiogram/client/session/middlewares/request_logging.py index af7b9d6e..ea9ffab9 100644 --- a/aiogram/client/session/middlewares/request_logging.py +++ b/aiogram/client/session/middlewares/request_logging.py @@ -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) diff --git a/aiogram/methods/add_sticker_to_set.py b/aiogram/methods/add_sticker_to_set.py index 836f18e3..75b8387a 100644 --- a/aiogram/methods/add_sticker_to_set.py +++ b/aiogram/methods/add_sticker_to_set.py @@ -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) diff --git a/aiogram/methods/base.py b/aiogram/methods/base.py index 699c10c7..8f9db0d4 100644 --- a/aiogram/methods/base.py +++ b/aiogram/methods/base.py @@ -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"] diff --git a/aiogram/methods/create_new_sticker_set.py b/aiogram/methods/create_new_sticker_set.py index bc1f0fb9..651c6672 100644 --- a/aiogram/methods/create_new_sticker_set.py +++ b/aiogram/methods/create_new_sticker_set.py @@ -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) diff --git a/aiogram/methods/send_animation.py b/aiogram/methods/send_animation.py index d382edf2..7528d8be 100644 --- a/aiogram/methods/send_animation.py +++ b/aiogram/methods/send_animation.py @@ -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) diff --git a/aiogram/methods/send_audio.py b/aiogram/methods/send_audio.py index c83c1818..6557a5c1 100644 --- a/aiogram/methods/send_audio.py +++ b/aiogram/methods/send_audio.py @@ -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) diff --git a/aiogram/methods/send_document.py b/aiogram/methods/send_document.py index 919f52f1..9b81b88f 100644 --- a/aiogram/methods/send_document.py +++ b/aiogram/methods/send_document.py @@ -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) diff --git a/aiogram/methods/send_video.py b/aiogram/methods/send_video.py index 001c8ccd..8075b1fb 100644 --- a/aiogram/methods/send_video.py +++ b/aiogram/methods/send_video.py @@ -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) diff --git a/aiogram/methods/send_video_note.py b/aiogram/methods/send_video_note.py index 9de6661f..3db3a142 100644 --- a/aiogram/methods/send_video_note.py +++ b/aiogram/methods/send_video_note.py @@ -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) diff --git a/aiogram/methods/set_sticker_set_thumbnail.py b/aiogram/methods/set_sticker_set_thumbnail.py index 8cc87c2f..829aa192 100644 --- a/aiogram/methods/set_sticker_set_thumbnail.py +++ b/aiogram/methods/set_sticker_set_thumbnail.py @@ -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) diff --git a/aiogram/methods/upload_sticker_file.py b/aiogram/methods/upload_sticker_file.py index 501d3161..d0fcbc44 100644 --- a/aiogram/methods/upload_sticker_file.py +++ b/aiogram/methods/upload_sticker_file.py @@ -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)