Expose entity parameter in client.inline_query

Some bots, such as @gamee, use this to determine the type of results
to return (and "disable" themselves in channels).
This commit is contained in:
Lonami Exo
2020-10-11 16:57:38 +02:00
parent d0faaa2ead
commit adf52a1b74
4 changed files with 32 additions and 8 deletions

View File

@@ -13,6 +13,7 @@ class BotMethods:
bot: 'hints.EntityLike',
query: str,
*,
entity: 'hints.EntityLike' = None,
offset: str = None,
geo_point: 'types.GeoPoint' = None) -> custom.InlineResults:
"""
@@ -25,6 +26,15 @@ class BotMethods:
query (`str`):
The query that should be made to the bot.
entity (`entity`, optional):
The entity where the inline query is being made from. Certain
bots use this to display different results depending on where
it's used, such as private chats, groups or channels.
If specified, it will also be the default entity where the
message will be sent after clicked. Otherwise, the "empty
peer" will be used, which some bots may not handle correctly.
offset (`str`, optional):
The string offset to use for the bot.
@@ -46,12 +56,17 @@ class BotMethods:
message = await results[0].click('TelethonOffTopic')
"""
bot = await self.get_input_entity(bot)
if entity:
peer = await self.get_input_entity(entity)
else:
peer = types.InputPeerEmpty()
result = await self(functions.messages.GetInlineBotResultsRequest(
bot=bot,
peer=types.InputPeerEmpty(),
peer=peer,
query=query,
offset=offset or '',
geo_point=geo_point
))
return custom.InlineResults(self, result)
return custom.InlineResults(self, result, entity=peer if entity else None)

View File

@@ -25,10 +25,11 @@ class InlineResult:
CONTACT = 'contact'
GAME = 'game'
def __init__(self, client, original, query_id=None):
def __init__(self, client, original, query_id=None, *, entity=None):
self._client = client
self.result = original
self._query_id = query_id
self._entity = entity
@property
def type(self):
@@ -97,7 +98,7 @@ class InlineResult:
elif isinstance(self.result, types.BotInlineMediaResult):
return self.result.document
async def click(self, entity, reply_to=None,
async def click(self, entity=None, reply_to=None,
silent=False, clear_draft=False, hide_via=False):
"""
Clicks this result and sends the associated `message`.
@@ -123,7 +124,13 @@ class InlineResult:
Whether the "via @bot" should be hidden or not.
Only works with certain bots (like @bing or @gif).
"""
entity = await self._client.get_input_entity(entity)
if entity:
entity = await self._client.get_input_entity(entity)
elif self._entity:
entity = self._entity
else:
raise ValueError('You must provide the entity where the result should be sent to')
reply_id = None if reply_to is None else utils.get_message_id(reply_to)
req = functions.messages.SendInlineBotResultRequest(
peer=entity,

View File

@@ -44,8 +44,8 @@ class InlineResults(list):
switch to a private conversation with the bot using
the text in this object.
"""
def __init__(self, client, original):
super().__init__(InlineResult(client, x, original.query_id)
def __init__(self, client, original, *, entity=None):
super().__init__(InlineResult(client, x, original.query_id, entity=entity)
for x in original.results)
self.result = original