Add parameter to pass raw entities when sending message (#1577)

This commit is contained in:
Tulir Asokan 2020-10-02 23:06:48 +03:00 committed by GitHub
parent 4321153b06
commit bf29cddbc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 11 deletions

View File

@ -551,6 +551,7 @@ class MessageMethods:
*, *,
reply_to: 'typing.Union[int, types.Message]' = None, reply_to: 'typing.Union[int, types.Message]' = None,
parse_mode: typing.Optional[str] = (), parse_mode: typing.Optional[str] = (),
formatting_entities: typing.Optional[typing.List[types.TypeMessageEntity]] = None,
link_preview: bool = True, link_preview: bool = True,
file: 'typing.Union[hints.FileLike, typing.Sequence[hints.FileLike]]' = None, file: 'typing.Union[hints.FileLike, typing.Sequence[hints.FileLike]]' = None,
force_document: bool = False, force_document: bool = False,
@ -598,6 +599,9 @@ class MessageMethods:
property for allowed values. Markdown parsing will be used by property for allowed values. Markdown parsing will be used by
default. default.
formatting_entities (`list`, optional):
A list of message formatting entities. When provided, the ``parse_mode`` is ignored.
link_preview (`bool`, optional): link_preview (`bool`, optional):
Should the link preview be shown? Should the link preview be shown?
@ -699,7 +703,7 @@ class MessageMethods:
entity, file, caption=message, reply_to=reply_to, entity, file, caption=message, reply_to=reply_to,
parse_mode=parse_mode, force_document=force_document, parse_mode=parse_mode, force_document=force_document,
buttons=buttons, clear_draft=clear_draft, silent=silent, buttons=buttons, clear_draft=clear_draft, silent=silent,
schedule=schedule schedule=schedule, formatting_entities=formatting_entities
) )
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
@ -721,7 +725,7 @@ class MessageMethods:
silent=silent, silent=silent,
reply_to=reply_to, reply_to=reply_to,
buttons=markup, buttons=markup,
entities=message.entities, formatting_entities=message.entities,
schedule=schedule schedule=schedule
) )
@ -739,7 +743,8 @@ class MessageMethods:
) )
message = message.message message = message.message
else: else:
message, msg_ent = await self._parse_message_text(message, parse_mode) if formatting_entities is None:
message, formatting_entities = await self._parse_message_text(message, parse_mode)
if not message: if not message:
raise ValueError( raise ValueError(
'The message cannot be empty unless a file is provided' 'The message cannot be empty unless a file is provided'
@ -748,7 +753,7 @@ class MessageMethods:
request = functions.messages.SendMessageRequest( request = functions.messages.SendMessageRequest(
peer=entity, peer=entity,
message=message, message=message,
entities=msg_ent, entities=formatting_entities,
no_webpage=not link_preview, no_webpage=not link_preview,
reply_to_msg_id=utils.get_message_id(reply_to), reply_to_msg_id=utils.get_message_id(reply_to),
clear_draft=clear_draft, clear_draft=clear_draft,
@ -900,6 +905,7 @@ class MessageMethods:
text: str = None, text: str = None,
*, *,
parse_mode: str = (), parse_mode: str = (),
formatting_entities: typing.Optional[typing.List[types.TypeMessageEntity]] = None,
link_preview: bool = True, link_preview: bool = True,
file: 'hints.FileLike' = None, file: 'hints.FileLike' = None,
force_document: bool = False, force_document: bool = False,
@ -939,6 +945,9 @@ class MessageMethods:
property for allowed values. Markdown parsing will be used by property for allowed values. Markdown parsing will be used by
default. default.
formatting_entities (`list`, optional):
A list of message formatting entities. When provided, the ``parse_mode`` is ignored.
link_preview (`bool`, optional): link_preview (`bool`, optional):
Should the link preview be shown? Should the link preview be shown?
@ -999,7 +1008,8 @@ class MessageMethods:
message = entity message = entity
entity = entity.peer_id entity = entity.peer_id
text, msg_entities = await self._parse_message_text(text, parse_mode) if formatting_entities is None:
text, formatting_entities = await self._parse_message_text(text, parse_mode)
file_handle, media, image = await self._file_to_media(file, file_handle, media, image = await self._file_to_media(file,
force_document=force_document) force_document=force_document)
@ -1008,7 +1018,7 @@ class MessageMethods:
id=entity, id=entity,
message=text, message=text,
no_webpage=not link_preview, no_webpage=not link_preview,
entities=msg_entities, entities=formatting_entities,
media=media, media=media,
reply_markup=self.build_reply_markup(buttons) reply_markup=self.build_reply_markup(buttons)
) )
@ -1030,7 +1040,7 @@ class MessageMethods:
id=utils.get_message_id(message), id=utils.get_message_id(message),
message=text, message=text,
no_webpage=not link_preview, no_webpage=not link_preview,
entities=msg_entities, entities=formatting_entities,
media=media, media=media,
reply_markup=self.build_reply_markup(buttons), reply_markup=self.build_reply_markup(buttons),
schedule_date=schedule schedule_date=schedule

View File

@ -102,6 +102,7 @@ class UploadMethods:
thumb: 'hints.FileLike' = None, thumb: 'hints.FileLike' = None,
allow_cache: bool = True, allow_cache: bool = True,
parse_mode: str = (), parse_mode: str = (),
formatting_entities: typing.Optional[typing.List[types.TypeMessageEntity]] = None,
voice_note: bool = False, voice_note: bool = False,
video_note: bool = False, video_note: bool = False,
buttons: 'hints.MarkupLike' = None, buttons: 'hints.MarkupLike' = None,
@ -215,6 +216,9 @@ class UploadMethods:
property for allowed values. Markdown parsing will be used by property for allowed values. Markdown parsing will be used by
default. default.
formatting_entities (`list`, optional):
A list of message formatting entities. When provided, the ``parse_mode`` is ignored.
voice_note (`bool`, optional): voice_note (`bool`, optional):
If `True` the audio will be sent as a voice note. If `True` the audio will be sent as a voice note.
@ -355,10 +359,8 @@ class UploadMethods:
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
reply_to = utils.get_message_id(reply_to) reply_to = utils.get_message_id(reply_to)
# Not document since it's subject to change. if formatting_entities is not None:
# Needed when a Message is passed to send_message and it has media. msg_entities = formatting_entities
if 'entities' in kwargs:
msg_entities = kwargs['entities']
else: else:
caption, msg_entities =\ caption, msg_entities =\
await self._parse_message_text(caption, parse_mode) await self._parse_message_text(caption, parse_mode)