Fix input media. And allow to edit media

This commit is contained in:
jrootjunior 2019-11-14 16:39:34 +02:00
parent 8a4b2a16dd
commit 6c0bd76a63
4 changed files with 19 additions and 11 deletions

View file

@ -78,6 +78,9 @@ from ..types import (
InlineKeyboardMarkup,
InlineQueryResult,
InputFile,
InputMedia,
InputMediaPhoto,
InputMediaVideo,
LabeledPrice,
MaskPosition,
Message,
@ -673,7 +676,7 @@ class Bot(BaseBot):
async def send_media_group(
self,
chat_id: Union[int, str],
media: Union[str, InputFile],
media: List[Union[InputMediaPhoto, InputMediaVideo]],
disable_notification: Optional[bool] = None,
reply_to_message_id: Optional[int] = None,
) -> List[Message]:
@ -685,7 +688,7 @@ class Bot(BaseBot):
:param chat_id: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:type chat_id: :obj:`Union[int, str]`
:param media: A JSON-serialized array describing photos and videos to be sent, must include 210 items
:type media: :obj:`Union[str, InputFile]`
:type media: :obj:`List[Union[InputMediaPhoto, InputMediaVideo]]`
:param disable_notification: Sends the messages silently. Users will receive a notification with no sound.
:type disable_notification: :obj:`Optional[bool]`
:param reply_to_message_id: If the messages are a reply, ID of the original message
@ -1501,7 +1504,7 @@ class Bot(BaseBot):
async def edit_message_media(
self,
media: Union[str, InputFile],
media: InputMedia,
chat_id: Optional[Union[int, str]] = None,
message_id: Optional[int] = None,
inline_message_id: Optional[str] = None,
@ -1513,7 +1516,7 @@ class Bot(BaseBot):
Source: https://core.telegram.org/bots/api#editmessagemedia
:param media: A JSON-serialized object for a new media content of the message
:type media: :obj:`Union[str, InputFile]`
:type media: :obj:`InputMedia`
:param chat_id: Required if inline_message_id is not specified. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
:type chat_id: :obj:`Optional[Union[int, str]]`
:param message_id: Required if inline_message_id is not specified. Identifier of the message to edit

View file

@ -1,7 +1,8 @@
import secrets
from typing import Any, Dict, Optional, Union
from ..types import InlineKeyboardMarkup, InputFile, Message
from .base import Request, TelegramMethod
from ..types import InlineKeyboardMarkup, InputMedia, Message, InputFile
class EditMessageMedia(TelegramMethod[Union[Message, bool]]):
@ -13,7 +14,7 @@ class EditMessageMedia(TelegramMethod[Union[Message, bool]]):
__returning__ = Union[Message, bool]
media: Union[str, InputFile]
media: InputMedia
"""A JSON-serialized object for a new media content of the message"""
chat_id: Optional[Union[int, str]] = None
@ -29,11 +30,15 @@ class EditMessageMedia(TelegramMethod[Union[Message, bool]]):
"""A JSON-serialized object for a new inline keyboard."""
def build_request(self) -> Request:
data: Dict[str, Any] = self.dict(
exclude={"media",}
)
data: Dict[str, Any] = self.dict()
files: Dict[str, InputFile] = {}
self.prepare_file(data=data, files=files, name="media", value=self.media)
self.prepare_media_file(data=data, files=files)
return Request(method="editMessageMedia", data=data, files=files)
def prepare_media_file(self, data: Dict[str, Any], files: Dict[str, InputFile]) -> None:
if isinstance(data["media"]["media"], InputFile):
tag = secrets.token_urlsafe(10)
files[tag] = data["media"].pop("media") # type: ignore
data["media"]["media"] = f"attach://{tag}"

View file

@ -70,7 +70,7 @@ class BaseSession(abc.ABC):
loop.create_task(self.close())
def prepare_value(self, value: Any) -> Union[str, int, bool]:
if isinstance(value, (bool, str, int)):
if isinstance(value, str):
return value
if isinstance(value, (list, dict)):
return self.json_dumps(self.clean_json(value))