diff --git a/telethon/client/bots.py b/telethon/client/bots.py index e8ac1a36..95d918d9 100644 --- a/telethon/client/bots.py +++ b/telethon/client/bots.py @@ -38,8 +38,4 @@ class BotMethods(UserMethods): geo_point=geo_point )) - # TODO Custom InlineResults(list) class with more information - return [ - custom.InlineResult(self, x, query_id=result.query_id) - for x in result.results - ] + return custom.InlineResults(self, result) diff --git a/telethon/tl/custom/__init__.py b/telethon/tl/custom/__init__.py index e3b6f39b..7511c176 100644 --- a/telethon/tl/custom/__init__.py +++ b/telethon/tl/custom/__init__.py @@ -7,3 +7,4 @@ from .message import Message from .button import Button from .inline import InlineBuilder from .inlineresult import InlineResult +from .inlineresults import InlineResults diff --git a/telethon/tl/custom/inlineresults.py b/telethon/tl/custom/inlineresults.py new file mode 100644 index 00000000..153e3bed --- /dev/null +++ b/telethon/tl/custom/inlineresults.py @@ -0,0 +1,82 @@ +import time + +from .inlineresult import InlineResult + +class InlineResults(list): + """ + Custom class that encapsulates :tl:`BotResults` providing + an abstraction to easily access some commonly needed features + (such as clicking one of the results to select it) + + Note that this is a list of `InlineResult + ` + so you can iterate over it or use indices to + access its elements. In addition, it has some + attributes. + + Attributes: + result (:tl:`BotResults`): + The original :tl:`BotResults` object. + + query_id (`int`): + The random ID that identifies this query. + + cache_time (`int`): + For how long the results should be considered + valid. You can call `results_valid` at any + moment to determine if the results are still + valid or not. + + users (:tl:`User`): + The users present in this inline query. + + gallery (`bool`): + Whether these results should be presented + in a grid (as a gallery of images) or not. + + next_offset (`str`, optional): + The string to be used as an offset to get + the next chunk of results, if any. + + switch_pm (:tl:`InlineBotSwitchPM`, optional): + If presents, the results should show a button to + 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) + for x in original.results) + + self.result = original + self.query_id = original.query_id + self.cache_time = original.cache_time + self._valid_until = time.time() + self.cache_time + self.users = original.users + self.gallery = bool(original.gallery) + self.next_offset = original.next_offset + self.switch_pm = original.switch_pm + + def results_valid(self): + """ + Returns ``True`` if the cache time has not expired + yet and the results can still be considered valid. + """ + return time.time() < self._valid_until + + def _to_str(self, item_function): + return ('[{}, query_id={}, cache_time={}, users={}, gallery={}, ' + 'next_offset={}, switch_pm={}]'.format( + ', '.join(item_function(x) for x in self), + self.query_id, + self.cache_time, + self.users, + self.gallery, + self.next_offset, + self.switch_pm + )) + + def __str__(self): + return self._to_str(str) + + def __repr__(self): + return self._to_str(repr)