Add support for user mentions inside inline query results

This commit is contained in:
Dan 2021-03-17 17:26:51 +01:00
parent 182768a5d3
commit 2eb7ab2f6e
8 changed files with 85 additions and 33 deletions

View File

@ -97,7 +97,7 @@ class AnswerInlineQuery(Scaffold):
return await self.send( return await self.send(
raw.functions.messages.SetInlineBotResults( raw.functions.messages.SetInlineBotResults(
query_id=int(inline_query_id), 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, cache_time=cache_time,
gallery=is_gallery or None, gallery=is_gallery or None,
private=is_personal or None, private=is_personal or None,

View File

@ -54,8 +54,23 @@ class InlineKeyboardMarkup(Object):
) )
async def write(self, client: "pyrogram.Client"): async def write(self, client: "pyrogram.Client"):
return raw.types.ReplyInlineMarkup( rows = []
rows=[raw.types.KeyboardButtonRow(
buttons=[await j.write(client) for j in i] for r in self.inline_keyboard:
) for i 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]
# )

View File

@ -18,6 +18,7 @@
from uuid import uuid4 from uuid import uuid4
import pyrogram
from pyrogram import types from pyrogram import types
from ..object import Object from ..object import Object
@ -66,5 +67,5 @@ class InlineQueryResult(Object):
self.input_message_content = input_message_content self.input_message_content = input_message_content
self.reply_markup = reply_markup self.reply_markup = reply_markup
async def write(self): async def write(self, client: "pyrogram.Client"):
pass pass

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Optional from typing import Optional, List
from pyrogram import raw import pyrogram
from pyrogram import types from pyrogram import raw, types, utils
from pyrogram.parser import Parser
from .inline_query_result import InlineQueryResult from .inline_query_result import InlineQueryResult
@ -60,6 +59,9 @@ class InlineQueryResultAnimation(InlineQueryResult):
Pass "html" to enable HTML-style parsing only. Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing. 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*): reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object. An InlineKeyboardMarkup object.
@ -76,6 +78,7 @@ class InlineQueryResultAnimation(InlineQueryResult):
description: str = None, description: str = None,
caption: str = "", caption: str = "",
parse_mode: Optional[str] = object, parse_mode: Optional[str] = object,
caption_entities: List["types.MessageEntity"] = None,
reply_markup: "types.InlineKeyboardMarkup" = None, reply_markup: "types.InlineKeyboardMarkup" = None,
input_message_content: "types.InputMessageContent" = None input_message_content: "types.InputMessageContent" = None
): ):
@ -87,10 +90,11 @@ class InlineQueryResultAnimation(InlineQueryResult):
self.description = description self.description = description
self.caption = caption self.caption = caption
self.parse_mode = parse_mode self.parse_mode = parse_mode
self.caption_entities = caption_entities
self.reply_markup = reply_markup self.reply_markup = reply_markup
self.input_message_content = input_message_content self.input_message_content = input_message_content
async def write(self): async def write(self, client: "pyrogram.Client"):
animation = raw.types.InputWebDocument( animation = raw.types.InputWebDocument(
url=self.animation_url, url=self.animation_url,
size=0, size=0,
@ -108,6 +112,10 @@ class InlineQueryResultAnimation(InlineQueryResult):
attributes=[] attributes=[]
) )
message, entities = (await utils.parse_text_entities(
client, self.caption, self.parse_mode, self.caption_entities
)).values()
return raw.types.InputBotInlineResult( return raw.types.InputBotInlineResult(
id=self.id, id=self.id,
type=self.type, type=self.type,
@ -116,11 +124,12 @@ class InlineQueryResultAnimation(InlineQueryResult):
thumb=thumb, thumb=thumb,
content=animation, content=animation,
send_message=( send_message=(
self.input_message_content.write(self.reply_markup) self.input_message_content.write(client, self.reply_markup)
if self.input_message_content if self.input_message_content
else raw.types.InputBotInlineMessageMediaAuto( else raw.types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None, reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
**await(Parser(None)).parse(self.caption, self.parse_mode) message=message,
entities=entities
) )
) )
) )

View File

@ -16,6 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram import types from pyrogram import types
@ -66,11 +67,11 @@ class InlineQueryResultArticle(InlineQueryResult):
self.description = description self.description = description
self.thumb_url = thumb_url self.thumb_url = thumb_url
async def write(self): async def write(self, client: "pyrogram.Client"):
return raw.types.InputBotInlineResult( return raw.types.InputBotInlineResult(
id=self.id, id=self.id,
type=self.type, 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, title=self.title,
description=self.description, description=self.description,
url=self.url, url=self.url,

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Optional from typing import Optional, List
from pyrogram import raw import pyrogram
from pyrogram import types from pyrogram import raw, types, utils
from pyrogram.parser import Parser
from .inline_query_result import InlineQueryResult from .inline_query_result import InlineQueryResult
@ -60,6 +59,9 @@ class InlineQueryResultPhoto(InlineQueryResult):
Pass "html" to enable HTML-style parsing only. Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing. 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*): reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
An InlineKeyboardMarkup object. An InlineKeyboardMarkup object.
@ -76,6 +78,7 @@ class InlineQueryResultPhoto(InlineQueryResult):
description: str = None, description: str = None,
caption: str = "", caption: str = "",
parse_mode: Optional[str] = object, parse_mode: Optional[str] = object,
caption_entities: List["types.MessageEntity"] = None,
reply_markup: "types.InlineKeyboardMarkup" = None, reply_markup: "types.InlineKeyboardMarkup" = None,
input_message_content: "types.InputMessageContent" = None input_message_content: "types.InputMessageContent" = None
): ):
@ -87,10 +90,11 @@ class InlineQueryResultPhoto(InlineQueryResult):
self.description = description self.description = description
self.caption = caption self.caption = caption
self.parse_mode = parse_mode self.parse_mode = parse_mode
self.caption_entities = caption_entities
self.reply_markup = reply_markup self.reply_markup = reply_markup
self.input_message_content = input_message_content self.input_message_content = input_message_content
async def write(self): async def write(self, client: "pyrogram.Client"):
photo = raw.types.InputWebDocument( photo = raw.types.InputWebDocument(
url=self.photo_url, url=self.photo_url,
size=0, size=0,
@ -108,6 +112,10 @@ class InlineQueryResultPhoto(InlineQueryResult):
attributes=[] attributes=[]
) )
message, entities = (await utils.parse_text_entities(
client, self.caption, self.parse_mode, self.caption_entities
)).values()
return raw.types.InputBotInlineResult( return raw.types.InputBotInlineResult(
id=self.id, id=self.id,
type=self.type, type=self.type,
@ -116,11 +124,12 @@ class InlineQueryResultPhoto(InlineQueryResult):
thumb=thumb, thumb=thumb,
content=photo, content=photo,
send_message=( 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 if self.input_message_content
else raw.types.InputBotInlineMessageMediaAuto( else raw.types.InputBotInlineMessageMediaAuto(
reply_markup=self.reply_markup.write() if self.reply_markup else None, reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
**await(Parser(None)).parse(self.caption, self.parse_mode) message=message,
entities=entities
) )
) )
) )

View File

@ -16,6 +16,8 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from ..object import Object from ..object import Object
"""- :obj:`~pyrogram.types.InputLocationMessageContent` """- :obj:`~pyrogram.types.InputLocationMessageContent`
@ -34,5 +36,5 @@ class InputMessageContent(Object):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def write(self, reply_markup): async def write(self, client: "pyrogram.Client", reply_markup):
raise NotImplementedError raise NotImplementedError

View File

@ -16,10 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Optional from typing import Optional, List
from pyrogram import raw import pyrogram
from pyrogram.parser import Parser from pyrogram import raw, types, utils
from .input_message_content import InputMessageContent from .input_message_content import InputMessageContent
@ -37,20 +37,35 @@ class InputTextMessageContent(InputMessageContent):
Pass "html" to enable HTML-style parsing only. Pass "html" to enable HTML-style parsing only.
Pass None to completely disable style parsing. 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*): disable_web_page_preview (``bool``, *optional*):
Disables link previews for links in this message. 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__() super().__init__()
self.message_text = message_text self.message_text = message_text
self.parse_mode = parse_mode self.parse_mode = parse_mode
self.entities = entities
self.disable_web_page_preview = disable_web_page_preview 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( return raw.types.InputBotInlineMessageText(
no_webpage=self.disable_web_page_preview or None, no_webpage=self.disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None, reply_markup=await reply_markup.write(client) if reply_markup else None,
**await(Parser(None)).parse(self.message_text, self.parse_mode) message=message,
entities=entities
) )