Skip to content

Instantly share code, notes, and snippets.

@voider1
Last active June 27, 2018 15:04
Show Gist options
  • Save voider1/18a8e06c2b179bb78353fbacfeee82da to your computer and use it in GitHub Desktop.
Save voider1/18a8e06c2b179bb78353fbacfeee82da to your computer and use it in GitHub Desktop.
Use this Gist with the python-telegram-bot API, this is a decorator which executes a function before anything else is done.
from functools import wraps
from telegram import ChatAction
def before_response(function):
"""Do something before responding"""
def do_before(func):
@wraps(func)
def inner(*args, **kwargs):
try:
bot = args[0]
update = args[1]
function(bot, update)
except (IndexError, AttributeError):
msg = "Check if you spelled the attributes right and if this is \
applied to the right function."
raise Exception(msg)
return func(*args, **kwargs)
return inner
return do_before
def pre_response(bot, update):
bot.sendChatAction(chat_id=update.message.chat_id,
action=ChatAction.TYPING)
@before_response(pre_response)
def test(bot, update):
pass
# Add command with CommandHandler etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment