Esempio n. 1
0
    def on_validation_error(self, update: Update, context: CallbackContext,
                            exception: Exception, help_message: str) -> bool:
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id

        denied_text = "\n".join(
            [":exclamation: `{}`".format(str(exception)), "", help_message])
        send_message(bot,
                     chat_id=chat_id,
                     message=denied_text,
                     parse_mode=ParseMode.MARKDOWN,
                     reply_to=message.message_id)
        return True
Esempio n. 2
0
    def on_permission_error(self, update: Update, context: CallbackContext,
                            permissions: Permission) -> bool:
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id

        text = "YOU SHALL NOT PASS! :hand::mage:"

        from telegram_click.util import send_message
        send_message(bot,
                     chat_id=chat_id,
                     message=text,
                     reply_to=message.message_id)

        return True
Esempio n. 3
0
    def on_permission_error(self, update: Update, context: CallbackContext,
                            permissions: Permission) -> bool:
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id

        if not self.silent_denial:
            # send 'permission denied' message
            text = self.DEFAULT_PERMISSION_DENIED_MESSAGE
            send_message(bot,
                         chat_id=chat_id,
                         message=text,
                         parse_mode=ParseMode.MARKDOWN,
                         reply_to=message.message_id)

        return True
Esempio n. 4
0
    def on_execution_error(self, update: Update, context: CallbackContext,
                           exception: Exception) -> bool:
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id

        if self.print_error:
            import traceback
            exception_text = "\n".join(
                list(
                    map(
                        lambda x: "{}:{}\n\t{}".format(x.filename, x.lineno, x.
                                                       line),
                        traceback.extract_tb(exception.__traceback__))))
            denied_text = ":boom: `{}`".format(exception_text)
        else:
            denied_text = ":boom: There was an error executing your command :worried:"
        send_message(bot,
                     chat_id=chat_id,
                     message=denied_text,
                     parse_mode=ParseMode.MARKDOWN,
                     reply_to=message.message_id)
        return True
Esempio n. 5
0
    def wrapper(*args, **kwargs):
        # find function arguments
        update = find_first(args, Update)
        context = find_first(args, CallbackContext)

        # get bot, chat and message info
        bot = context.bot
        message = update.effective_message
        chat_id = message.chat_id

        try:
            if not _check_permissions(update, context, permissions):
                LOGGER.debug("Permission denied in chat {} for user {} for message: {}".format(
                    chat_id,
                    update.effective_message.from_user.id,
                    message))

                # send 'permission denied' message, if configured
                if permission_denied_message is not None:
                    send_message(bot, chat_id=chat_id, message=permission_denied_message,
                                 parse_mode=ParseMode.MARKDOWN,
                                 reply_to=message.message_id)
                # don't process command
                return

            # parse and check command target
            cmd, _ = split_command_from_args(message.text)
            _, target = split_command_from_target(bot.username, cmd)
            # check if we are allowed to process the given command target
            if not filter_command_target(target, bot.username, command_target):
                LOGGER.debug("Ignoring command for unspecified target {} in chat {} for user {}: {}".format(
                    target,
                    chat_id,
                    update.effective_message.from_user.id,
                    message))

                # don't process command
                return

            try:
                # parse command and arguments
                cmd, parsed_args = parse_telegram_command(bot.username, message.text, arguments)
            except ValueError as ex:
                # handle exceptions that occur during permission and argument parsing
                logging.exception("Error parsing command arguments")

                denied_text = "\n".join([
                    ":exclamation: `{}`".format(str(ex)),
                    "",
                    help_message
                ])
                send_message(bot, chat_id=chat_id, message=denied_text, parse_mode=ParseMode.MARKDOWN,
                             reply_to=message.message_id)
                return

            # convert argument names to python param naming convention (snake-case)
            kw_function_args = dict(map(lambda x: (x[0].lower().replace("-", "_"), x[1]), list(parsed_args.items())))
            # execute wrapped function
            return func(*args, **{**kw_function_args, **kwargs})
        except Exception as ex:
            # execute wrapped function
            logging.exception("Error in callback")

            import traceback
            exception_text = "\n".join(list(map(lambda x: "{}:{}\n\t{}".format(x.filename, x.lineno, x.line),
                                                traceback.extract_tb(ex.__traceback__))))
            if print_error:
                denied_text = ":boom: `{}`".format(exception_text)
            else:
                denied_text = ":boom: There was an error executing your command :worried:"
            send_message(bot, chat_id=chat_id, message=denied_text, parse_mode=ParseMode.MARKDOWN,
                         reply_to=message.message_id)