Placeholder system

Send a placeholder message or chat_action if the request takes some 
time.
This commit is contained in:
Davte 2019-11-28 18:08:58 +01:00
parent 74079fb481
commit 85341d7064
2 changed files with 55 additions and 1 deletions

View File

@ -14,7 +14,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.3.4" __version__ = "2.3.5"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -214,6 +214,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
self.recent_users = OrderedDict() self.recent_users = OrderedDict()
self._log_file_name = None self._log_file_name = None
self._errors_file_name = None self._errors_file_name = None
self.placeholder_requests = dict()
return return
@property @property
@ -2026,6 +2027,59 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
del self.individual_location_handlers[identifier] del self.individual_location_handlers[identifier]
return return
def set_placeholder(self, chat_id,
text=None, sent_message=None, timeout=1):
"""Set a placeholder chat action or text message.
If it takes the bot more than `timeout` to answer, send a placeholder
message or a `is typing` chat action.
`timeout` may be expressed in seconds (int) or datetime.timedelta
This method returns a `request_id`. When the calling function has
performed its task, it must set to 1 the value of
`self.placeholder_requests[request_id]`.
If this value is still 0 at `timeout`, the placeholder is sent.
Otherwise, no action is performed.
"""
request_id = len(self.placeholder_requests)
self.placeholder_requests[request_id] = 0
asyncio.ensure_future(
self.placeholder_effector(
request_id=request_id,
timeout=timeout,
chat_id=chat_id,
sent_message=sent_message,
text=text
)
)
return request_id
async def placeholder_effector(self, request_id, timeout, chat_id,
sent_message=None, text=None):
"""Send a placeholder chat action or text message if needed.
If it takes the bot more than `timeout` to answer, send a placeholder
message or a `is typing` chat action.
`timeout` may be expressed in seconds (int) or datetime.timedelta
"""
if type(timeout) is datetime.timedelta:
timeout = timeout.total_seconds()
print(timeout)
await asyncio.sleep(timeout)
print("sleep ends")
if not self.placeholder_requests[request_id]:
if sent_message and text:
await self.edit_message_text(
update=sent_message,
text=text,
)
else:
await self.sendChatAction(
chat_id=chat_id,
action='typing'
)
return
async def webhook_feeder(self, request): async def webhook_feeder(self, request):
"""Handle incoming HTTP `request`s. """Handle incoming HTTP `request`s.