示例#1
0
def apply_substitutions(text, substitutions: Dict[Any, Substitution], bot: Bot,
                        extra):
    for needle, sub in substitutions.items():
        if sub.key and sub.argument:
            param = sub.key
            extra["argument"] = MessageAction.get_argument_value(
                extra["message"], sub.argument - 1)
        elif sub.key:
            param = sub.key
        elif sub.argument:
            param = MessageAction.get_argument_value(extra["message"],
                                                     sub.argument - 1)
        else:
            log.error("Unknown param for response.")
            continue
        value: Any = sub.cb(param, extra)
        if value is None:
            return None
        try:
            for f in sub.filters:
                value = bot.apply_filter(value, f)
        except:
            log.exception("Exception caught in filter application")
        if value is None:
            return None
        text = text.replace(needle, str(value))

    return text
示例#2
0
def apply_substitutions(text, substitutions: Dict[Any, Substitution], bot: Bot,
                        extra):
    for needle, sub in substitutions.items():
        if sub.key and sub.argument:
            param = sub.key
            extra["argument"] = get_argument_value(extra["message"],
                                                   sub.argument)
        elif sub.key:
            param = sub.key
        elif sub.argument:
            param = get_argument_value(extra["message"], sub.argument)
        else:
            log.error("Unknown param for response.")
            continue
        # The dictionary of substitutions here will always come from get_substitutions, which means it will always have a callback to call
        assert sub.cb is not None
        value: Any = sub.cb(param, extra)
        try:
            for f in sub.filters:
                value = bot.apply_filter(value, f)
        except:
            log.exception("Exception caught in filter application")
        if value is None:
            return None
        text = text.replace(needle, str(value))

    return text