Last active
October 30, 2016 18:33
-
-
Save voider1/46f3d8ab50381d93f8682df3ce318bff to your computer and use it in GitHub Desktop.
Use this the python-telegram-bot API, this ensures there's a timeout for certain functions, just add @timeout(duration) above your function definition.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from functools import wraps | |
timeout_funcs = {} | |
def get_seconds(): | |
return int(round(time.time())) | |
def timeout(duration): | |
def check(func): | |
@wraps(func) | |
def inner(*args, **kwargs): | |
if func not in timeout_funcs or timeout_funcs[func] < get_seconds(): | |
timeout_funcs[func] = get_seconds() + duration | |
return func(*args, **kwargs) | |
func_name = func.__name__ | |
try: | |
update = args[1] | |
msg = "/{} is timed out for {} seconds after you called it.". \ | |
format(func_name, duration) | |
update.message.reply_text(msg) | |
except (IndexError, AttributeError): | |
msg = "Check if you spelled the attributes right and if this is \ | |
applied on the right function." | |
raise Exception(msg) | |
return inner | |
return check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment