From 2eb7ab2f6e8673336f12b4e470d8325739ea9c53 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 17 Mar 2021 17:26:51 +0100 Subject: [PATCH] Add support for user mentions inside inline query results --- pyrogram/methods/bots/answer_inline_query.py | 2 +- .../inline_keyboard_markup.py | 25 ++++++++++++---- .../types/inline_mode/inline_query_result.py | 3 +- .../inline_query_result_animation.py | 25 +++++++++++----- .../inline_query_result_article.py | 5 ++-- .../inline_mode/inline_query_result_photo.py | 25 +++++++++++----- .../input_message_content.py | 4 ++- .../input_text_message_content.py | 29 ++++++++++++++----- 8 files changed, 85 insertions(+), 33 deletions(-) diff --git a/pyrogram/methods/bots/answer_inline_query.py b/pyrogram/methods/bots/answer_inline_query.py index 973d7307..66bd739f 100644 --- a/pyrogram/methods/bots/answer_inline_query.py +++ b/pyrogram/methods/bots/answer_inline_query.py @@ -97,7 +97,7 @@ class AnswerInlineQuery(Scaffold): return await self.send( raw.functions.messages.SetInlineBotResults( query_id=int(inline_query_id), - results=[await r.write() for r in results], + results=[await r.write(self) for r in results], cache_time=cache_time, gallery=is_gallery or None, private=is_personal or None, diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py index 8b5ec13b..1571f1ba 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py @@ -54,8 +54,23 @@ class InlineKeyboardMarkup(Object): ) async def write(self, client: "pyrogram.Client"): - return raw.types.ReplyInlineMarkup( - rows=[raw.types.KeyboardButtonRow( - buttons=[await j.write(client) for j in i] - ) for i in self.inline_keyboard] - ) + rows = [] + + for r in self.inline_keyboard: + buttons = [] + + for b in r: + buttons.append(await b.write(client)) + + rows.append(raw.types.KeyboardButtonRow(buttons=buttons)) + + return raw.types.ReplyInlineMarkup(rows=rows) + + # There seems to be a Python issues with nested async comprehensions. + # See: https://bugs.python.org/issue33346 + # + # return raw.types.ReplyInlineMarkup( + # rows=[raw.types.KeyboardButtonRow( + # buttons=[await j.write(client) for j in i] + # ) for i in self.inline_keyboard] + # ) diff --git a/pyrogram/types/inline_mode/inline_query_result.py b/pyrogram/types/inline_mode/inline_query_result.py index decb77e1..919b7df5 100644 --- a/pyrogram/types/inline_mode/inline_query_result.py +++ b/pyrogram/types/inline_mode/inline_query_result.py @@ -18,6 +18,7 @@ from uuid import uuid4 +import pyrogram from pyrogram import types from ..object import Object @@ -66,5 +67,5 @@ class InlineQueryResult(Object): self.input_message_content = input_message_content self.reply_markup = reply_markup - async def write(self): + async def write(self, client: "pyrogram.Client"): pass diff --git a/pyrogram/types/inline_mode/inline_query_result_animation.py b/pyrogram/types/inline_mode/inline_query_result_animation.py index 2b9725d5..45af1ed3 100644 --- a/pyrogram/types/inline_mode/inline_query_result_animation.py +++ b/pyrogram/types/inline_mode/inline_query_result_animation.py @@ -16,11 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Optional +from typing import Optional, List -from pyrogram import raw -from pyrogram import types -from pyrogram.parser import Parser +import pyrogram +from pyrogram import raw, types, utils from .inline_query_result import InlineQueryResult @@ -60,6 +59,9 @@ class InlineQueryResultAnimation(InlineQueryResult): Pass "html" to enable HTML-style parsing only. Pass None to completely disable style parsing. + caption_entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in the caption, which can be specified instead of *parse_mode*. + reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*): An InlineKeyboardMarkup object. @@ -76,6 +78,7 @@ class InlineQueryResultAnimation(InlineQueryResult): description: str = None, caption: str = "", parse_mode: Optional[str] = object, + caption_entities: List["types.MessageEntity"] = None, reply_markup: "types.InlineKeyboardMarkup" = None, input_message_content: "types.InputMessageContent" = None ): @@ -87,10 +90,11 @@ class InlineQueryResultAnimation(InlineQueryResult): self.description = description self.caption = caption self.parse_mode = parse_mode + self.caption_entities = caption_entities self.reply_markup = reply_markup self.input_message_content = input_message_content - async def write(self): + async def write(self, client: "pyrogram.Client"): animation = raw.types.InputWebDocument( url=self.animation_url, size=0, @@ -108,6 +112,10 @@ class InlineQueryResultAnimation(InlineQueryResult): attributes=[] ) + message, entities = (await utils.parse_text_entities( + client, self.caption, self.parse_mode, self.caption_entities + )).values() + return raw.types.InputBotInlineResult( id=self.id, type=self.type, @@ -116,11 +124,12 @@ class InlineQueryResultAnimation(InlineQueryResult): thumb=thumb, content=animation, send_message=( - self.input_message_content.write(self.reply_markup) + self.input_message_content.write(client, self.reply_markup) if self.input_message_content else raw.types.InputBotInlineMessageMediaAuto( - reply_markup=self.reply_markup.write() if self.reply_markup else None, - **await(Parser(None)).parse(self.caption, self.parse_mode) + reply_markup=await self.reply_markup.write(client) if self.reply_markup else None, + message=message, + entities=entities ) ) ) diff --git a/pyrogram/types/inline_mode/inline_query_result_article.py b/pyrogram/types/inline_mode/inline_query_result_article.py index eadb0a15..0b5e6008 100644 --- a/pyrogram/types/inline_mode/inline_query_result_article.py +++ b/pyrogram/types/inline_mode/inline_query_result_article.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram from pyrogram import raw from pyrogram import types @@ -66,11 +67,11 @@ class InlineQueryResultArticle(InlineQueryResult): self.description = description self.thumb_url = thumb_url - async def write(self): + async def write(self, client: "pyrogram.Client"): return raw.types.InputBotInlineResult( id=self.id, type=self.type, - send_message=await self.input_message_content.write(self.reply_markup), + send_message=await self.input_message_content.write(client, self.reply_markup), title=self.title, description=self.description, url=self.url, diff --git a/pyrogram/types/inline_mode/inline_query_result_photo.py b/pyrogram/types/inline_mode/inline_query_result_photo.py index 7afd6659..f86c6718 100644 --- a/pyrogram/types/inline_mode/inline_query_result_photo.py +++ b/pyrogram/types/inline_mode/inline_query_result_photo.py @@ -16,11 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Optional +from typing import Optional, List -from pyrogram import raw -from pyrogram import types -from pyrogram.parser import Parser +import pyrogram +from pyrogram import raw, types, utils from .inline_query_result import InlineQueryResult @@ -60,6 +59,9 @@ class InlineQueryResultPhoto(InlineQueryResult): Pass "html" to enable HTML-style parsing only. Pass None to completely disable style parsing. + caption_entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in the caption, which can be specified instead of *parse_mode*. + reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*): An InlineKeyboardMarkup object. @@ -76,6 +78,7 @@ class InlineQueryResultPhoto(InlineQueryResult): description: str = None, caption: str = "", parse_mode: Optional[str] = object, + caption_entities: List["types.MessageEntity"] = None, reply_markup: "types.InlineKeyboardMarkup" = None, input_message_content: "types.InputMessageContent" = None ): @@ -87,10 +90,11 @@ class InlineQueryResultPhoto(InlineQueryResult): self.description = description self.caption = caption self.parse_mode = parse_mode + self.caption_entities = caption_entities self.reply_markup = reply_markup self.input_message_content = input_message_content - async def write(self): + async def write(self, client: "pyrogram.Client"): photo = raw.types.InputWebDocument( url=self.photo_url, size=0, @@ -108,6 +112,10 @@ class InlineQueryResultPhoto(InlineQueryResult): attributes=[] ) + message, entities = (await utils.parse_text_entities( + client, self.caption, self.parse_mode, self.caption_entities + )).values() + return raw.types.InputBotInlineResult( id=self.id, type=self.type, @@ -116,11 +124,12 @@ class InlineQueryResultPhoto(InlineQueryResult): thumb=thumb, content=photo, send_message=( - await self.input_message_content.write(self.reply_markup) + await self.input_message_content.write(client, self.reply_markup) if self.input_message_content else raw.types.InputBotInlineMessageMediaAuto( - reply_markup=self.reply_markup.write() if self.reply_markup else None, - **await(Parser(None)).parse(self.caption, self.parse_mode) + reply_markup=await self.reply_markup.write(client) if self.reply_markup else None, + message=message, + entities=entities ) ) ) diff --git a/pyrogram/types/input_message_content/input_message_content.py b/pyrogram/types/input_message_content/input_message_content.py index 3d23b9ea..99bed67d 100644 --- a/pyrogram/types/input_message_content/input_message_content.py +++ b/pyrogram/types/input_message_content/input_message_content.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import pyrogram + from ..object import Object """- :obj:`~pyrogram.types.InputLocationMessageContent` @@ -34,5 +36,5 @@ class InputMessageContent(Object): def __init__(self): super().__init__() - def write(self, reply_markup): + async def write(self, client: "pyrogram.Client", reply_markup): raise NotImplementedError diff --git a/pyrogram/types/input_message_content/input_text_message_content.py b/pyrogram/types/input_message_content/input_text_message_content.py index ed943bfd..a053d818 100644 --- a/pyrogram/types/input_message_content/input_text_message_content.py +++ b/pyrogram/types/input_message_content/input_text_message_content.py @@ -16,10 +16,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import Optional +from typing import Optional, List -from pyrogram import raw -from pyrogram.parser import Parser +import pyrogram +from pyrogram import raw, types, utils from .input_message_content import InputMessageContent @@ -37,20 +37,35 @@ class InputTextMessageContent(InputMessageContent): Pass "html" to enable HTML-style parsing only. Pass None to completely disable style parsing. + entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in message text, which can be specified instead of *parse_mode*. + disable_web_page_preview (``bool``, *optional*): Disables link previews for links in this message. """ - def __init__(self, message_text: str, parse_mode: Optional[str] = object, disable_web_page_preview: bool = None): + def __init__( + self, + message_text: str, + parse_mode: Optional[str] = object, + entities: List["types.MessageEntity"] = None, + disable_web_page_preview: bool = None + ): super().__init__() self.message_text = message_text self.parse_mode = parse_mode + self.entities = entities self.disable_web_page_preview = disable_web_page_preview - async def write(self, reply_markup): + async def write(self, client: "pyrogram.Client", reply_markup): + message, entities = (await utils.parse_text_entities( + client, self.message_text, self.parse_mode, self.entities + )).values() + return raw.types.InputBotInlineMessageText( no_webpage=self.disable_web_page_preview or None, - reply_markup=reply_markup.write() if reply_markup else None, - **await(Parser(None)).parse(self.message_text, self.parse_mode) + reply_markup=await reply_markup.write(client) if reply_markup else None, + message=message, + entities=entities )