Skip to content

Commit 2eb7ab2

Browse files
committed
Add support for user mentions inside inline query results
1 parent 182768a commit 2eb7ab2

File tree

8 files changed

+85
-33
lines changed

8 files changed

+85
-33
lines changed

pyrogram/methods/bots/answer_inline_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def answer_inline_query(
9797
return await self.send(
9898
raw.functions.messages.SetInlineBotResults(
9999
query_id=int(inline_query_id),
100-
results=[await r.write() for r in results],
100+
results=[await r.write(self) for r in results],
101101
cache_time=cache_time,
102102
gallery=is_gallery or None,
103103
private=is_personal or None,

pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,23 @@ def read(o):
5454
)
5555

5656
async def write(self, client: "pyrogram.Client"):
57-
return raw.types.ReplyInlineMarkup(
58-
rows=[raw.types.KeyboardButtonRow(
59-
buttons=[await j.write(client) for j in i]
60-
) for i in self.inline_keyboard]
61-
)
57+
rows = []
58+
59+
for r in self.inline_keyboard:
60+
buttons = []
61+
62+
for b in r:
63+
buttons.append(await b.write(client))
64+
65+
rows.append(raw.types.KeyboardButtonRow(buttons=buttons))
66+
67+
return raw.types.ReplyInlineMarkup(rows=rows)
68+
69+
# There seems to be a Python issues with nested async comprehensions.
70+
# See: https://bugs.python.org/issue33346
71+
#
72+
# return raw.types.ReplyInlineMarkup(
73+
# rows=[raw.types.KeyboardButtonRow(
74+
# buttons=[await j.write(client) for j in i]
75+
# ) for i in self.inline_keyboard]
76+
# )

pyrogram/types/inline_mode/inline_query_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from uuid import uuid4
2020

21+
import pyrogram
2122
from pyrogram import types
2223
from ..object import Object
2324

@@ -66,5 +67,5 @@ def __init__(
6667
self.input_message_content = input_message_content
6768
self.reply_markup = reply_markup
6869

69-
async def write(self):
70+
async def write(self, client: "pyrogram.Client"):
7071
pass

pyrogram/types/inline_mode/inline_query_result_animation.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Optional
19+
from typing import Optional, List
2020

21-
from pyrogram import raw
22-
from pyrogram import types
23-
from pyrogram.parser import Parser
21+
import pyrogram
22+
from pyrogram import raw, types, utils
2423
from .inline_query_result import InlineQueryResult
2524

2625

@@ -60,6 +59,9 @@ class InlineQueryResultAnimation(InlineQueryResult):
6059
Pass "html" to enable HTML-style parsing only.
6160
Pass None to completely disable style parsing.
6261
62+
caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
63+
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.
64+
6365
reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
6466
An InlineKeyboardMarkup object.
6567
@@ -76,6 +78,7 @@ def __init__(
7678
description: str = None,
7779
caption: str = "",
7880
parse_mode: Optional[str] = object,
81+
caption_entities: List["types.MessageEntity"] = None,
7982
reply_markup: "types.InlineKeyboardMarkup" = None,
8083
input_message_content: "types.InputMessageContent" = None
8184
):
@@ -87,10 +90,11 @@ def __init__(
8790
self.description = description
8891
self.caption = caption
8992
self.parse_mode = parse_mode
93+
self.caption_entities = caption_entities
9094
self.reply_markup = reply_markup
9195
self.input_message_content = input_message_content
9296

93-
async def write(self):
97+
async def write(self, client: "pyrogram.Client"):
9498
animation = raw.types.InputWebDocument(
9599
url=self.animation_url,
96100
size=0,
@@ -108,6 +112,10 @@ async def write(self):
108112
attributes=[]
109113
)
110114

115+
message, entities = (await utils.parse_text_entities(
116+
client, self.caption, self.parse_mode, self.caption_entities
117+
)).values()
118+
111119
return raw.types.InputBotInlineResult(
112120
id=self.id,
113121
type=self.type,
@@ -116,11 +124,12 @@ async def write(self):
116124
thumb=thumb,
117125
content=animation,
118126
send_message=(
119-
self.input_message_content.write(self.reply_markup)
127+
self.input_message_content.write(client, self.reply_markup)
120128
if self.input_message_content
121129
else raw.types.InputBotInlineMessageMediaAuto(
122-
reply_markup=self.reply_markup.write() if self.reply_markup else None,
123-
**await(Parser(None)).parse(self.caption, self.parse_mode)
130+
reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
131+
message=message,
132+
entities=entities
124133
)
125134
)
126135
)

pyrogram/types/inline_mode/inline_query_result_article.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import pyrogram
1920
from pyrogram import raw
2021
from pyrogram import types
2122

@@ -66,11 +67,11 @@ def __init__(
6667
self.description = description
6768
self.thumb_url = thumb_url
6869

69-
async def write(self):
70+
async def write(self, client: "pyrogram.Client"):
7071
return raw.types.InputBotInlineResult(
7172
id=self.id,
7273
type=self.type,
73-
send_message=await self.input_message_content.write(self.reply_markup),
74+
send_message=await self.input_message_content.write(client, self.reply_markup),
7475
title=self.title,
7576
description=self.description,
7677
url=self.url,

pyrogram/types/inline_mode/inline_query_result_photo.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Optional
19+
from typing import Optional, List
2020

21-
from pyrogram import raw
22-
from pyrogram import types
23-
from pyrogram.parser import Parser
21+
import pyrogram
22+
from pyrogram import raw, types, utils
2423
from .inline_query_result import InlineQueryResult
2524

2625

@@ -60,6 +59,9 @@ class InlineQueryResultPhoto(InlineQueryResult):
6059
Pass "html" to enable HTML-style parsing only.
6160
Pass None to completely disable style parsing.
6261
62+
caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
63+
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.
64+
6365
reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup`, *optional*):
6466
An InlineKeyboardMarkup object.
6567
@@ -76,6 +78,7 @@ def __init__(
7678
description: str = None,
7779
caption: str = "",
7880
parse_mode: Optional[str] = object,
81+
caption_entities: List["types.MessageEntity"] = None,
7982
reply_markup: "types.InlineKeyboardMarkup" = None,
8083
input_message_content: "types.InputMessageContent" = None
8184
):
@@ -87,10 +90,11 @@ def __init__(
8790
self.description = description
8891
self.caption = caption
8992
self.parse_mode = parse_mode
93+
self.caption_entities = caption_entities
9094
self.reply_markup = reply_markup
9195
self.input_message_content = input_message_content
9296

93-
async def write(self):
97+
async def write(self, client: "pyrogram.Client"):
9498
photo = raw.types.InputWebDocument(
9599
url=self.photo_url,
96100
size=0,
@@ -108,6 +112,10 @@ async def write(self):
108112
attributes=[]
109113
)
110114

115+
message, entities = (await utils.parse_text_entities(
116+
client, self.caption, self.parse_mode, self.caption_entities
117+
)).values()
118+
111119
return raw.types.InputBotInlineResult(
112120
id=self.id,
113121
type=self.type,
@@ -116,11 +124,12 @@ async def write(self):
116124
thumb=thumb,
117125
content=photo,
118126
send_message=(
119-
await self.input_message_content.write(self.reply_markup)
127+
await self.input_message_content.write(client, self.reply_markup)
120128
if self.input_message_content
121129
else raw.types.InputBotInlineMessageMediaAuto(
122-
reply_markup=self.reply_markup.write() if self.reply_markup else None,
123-
**await(Parser(None)).parse(self.caption, self.parse_mode)
130+
reply_markup=await self.reply_markup.write(client) if self.reply_markup else None,
131+
message=message,
132+
entities=entities
124133
)
125134
)
126135
)

pyrogram/types/input_message_content/input_message_content.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import pyrogram
20+
1921
from ..object import Object
2022

2123
"""- :obj:`~pyrogram.types.InputLocationMessageContent`
@@ -34,5 +36,5 @@ class InputMessageContent(Object):
3436
def __init__(self):
3537
super().__init__()
3638

37-
def write(self, reply_markup):
39+
async def write(self, client: "pyrogram.Client", reply_markup):
3840
raise NotImplementedError

pyrogram/types/input_message_content/input_text_message_content.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import Optional
19+
from typing import Optional, List
2020

21-
from pyrogram import raw
22-
from pyrogram.parser import Parser
21+
import pyrogram
22+
from pyrogram import raw, types, utils
2323
from .input_message_content import InputMessageContent
2424

2525

@@ -37,20 +37,35 @@ class InputTextMessageContent(InputMessageContent):
3737
Pass "html" to enable HTML-style parsing only.
3838
Pass None to completely disable style parsing.
3939
40+
entities (List of :obj:`~pyrogram.types.MessageEntity`):
41+
List of special entities that appear in message text, which can be specified instead of *parse_mode*.
42+
4043
disable_web_page_preview (``bool``, *optional*):
4144
Disables link previews for links in this message.
4245
"""
4346

44-
def __init__(self, message_text: str, parse_mode: Optional[str] = object, disable_web_page_preview: bool = None):
47+
def __init__(
48+
self,
49+
message_text: str,
50+
parse_mode: Optional[str] = object,
51+
entities: List["types.MessageEntity"] = None,
52+
disable_web_page_preview: bool = None
53+
):
4554
super().__init__()
4655

4756
self.message_text = message_text
4857
self.parse_mode = parse_mode
58+
self.entities = entities
4959
self.disable_web_page_preview = disable_web_page_preview
5060

51-
async def write(self, reply_markup):
61+
async def write(self, client: "pyrogram.Client", reply_markup):
62+
message, entities = (await utils.parse_text_entities(
63+
client, self.message_text, self.parse_mode, self.entities
64+
)).values()
65+
5266
return raw.types.InputBotInlineMessageText(
5367
no_webpage=self.disable_web_page_preview or None,
54-
reply_markup=reply_markup.write() if reply_markup else None,
55-
**await(Parser(None)).parse(self.message_text, self.parse_mode)
68+
reply_markup=await reply_markup.write(client) if reply_markup else None,
69+
message=message,
70+
entities=entities
5671
)

0 commit comments

Comments
 (0)