Exemplo n.º 1
0
    def __new__(cls, name, bases, dct):
        x = super().__new__(cls, name, bases, dct)
        for name in list(x.__dict__):
            method = getattr(x, name)
            if hasattr(method, "is_command"):

                def get_wrapper(func):
                    argspec = inspect.getfullargspec(method).args

                    if (argspec[0] != 'self'):
                        raise ValueError(
                            'First argument of TelegramCommand should be '
                            'self')
                    argspec = argspec[1:]

                    def wrapper(bot, update, user_data=None, args=[]):
                        ins = x(context=user_data, update=update, bot=bot)
                        if len(argspec) != len(args):
                            logging.debug(
                                'Command called with invalid argument count')
                            bot.send_message(
                                chat_id=update.message.chat_id,
                                text='Ongeldig aantal argumenten.')
                            bot.send_message(chat_id=update.message.chat_id,
                                             text=cls.get_help_text(func))
                        else:
                            func(ins, *args)

                    wrapper.original = func
                    return wrapper

                setattr(x, name, get_wrapper(method))

                handler = CommandHandler(name,
                                         getattr(x, name),
                                         pass_user_data=True,
                                         pass_args=True)
                dispatcher.add_handler(handler)
            elif hasattr(method, 'filter'):

                def get_wrapper(func):
                    def wrapper(bot, update, user_data=None):
                        ins = x(context=user_data, update=update, bot=bot)
                        func(ins)

                    wrapper.original = func
                    return wrapper

                setattr(x, name, get_wrapper(method))
                handler = MessageHandler(method.filter,
                                         getattr(x, name),
                                         pass_user_data=True,
                                         edited_updates=True)
                dispatcher.add_handler(handler)

        return x
Exemplo n.º 2
0
def init():
    global GLOBAL_KEYBOARD_COUNTER
    global GLOBAL_KEYBOARD_MAP
    GLOBAL_KEYBOARD_COUNTER = 0
    GLOBAL_KEYBOARD_MAP = dict()
    dispatcher.add_handler(CallbackQueryHandler(button_handler))