From 82ec440b459b65a8a2cd017d68bf25f7ff32e2e9 Mon Sep 17 00:00:00 2001 From: lanvent Date: Wed, 12 Apr 2023 20:16:21 +0800 Subject: [PATCH] banwords: support reply filter --- plugins/banwords/README.md | 20 +++++++++++++++++++- plugins/banwords/banwords.py | 26 +++++++++++++++++++++++++- plugins/banwords/config.json.template | 4 +++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/plugins/banwords/README.md b/plugins/banwords/README.md index c423a36..39517f6 100644 --- a/plugins/banwords/README.md +++ b/plugins/banwords/README.md @@ -1,9 +1,27 @@ + ## 插件描述 + 简易的敏感词插件,暂不支持分词,请自行导入词库到插件文件夹中的`banwords.txt`,每行一个词,一个参考词库是[1](https://github.com/cjh0613/tencent-sensitive-words/blob/main/sensitive_words_lines.txt)。 -`config.json`中能够填写默认的处理行为,目前行为有: +使用前将`config.json.template`复制为`config.json`,并自行配置。 + +目前插件对消息的默认处理行为有如下两种: + - `ignore` : 无视这条消息。 - `replace` : 将消息中的敏感词替换成"*",并回复违规。 +```json + "action": "replace", + "reply_filter": true, + "reply_action": "ignore" +``` + +在以上配置项中: + +- `action`: 对用户消息的默认处理行为 +- `reply_filter`: 是否对ChatGPT的回复也进行敏感词过滤 +- `reply_action`: 如果开启了回复过滤,对回复的默认处理行为 + ## 致谢 + 搜索功能实现来自https://github.com/toolgood/ToolGood.Words \ No newline at end of file diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index e2719d6..2d94af4 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -36,6 +36,9 @@ class Banwords(Plugin): words.append(word) self.searchr.SetKeywords(words) self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + if conf.get("reply_filter",True): + self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply + self.reply_action = conf.get("reply_action","ignore") logger.info("[Banwords] inited") except Exception as e: logger.warn("[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords .") @@ -53,7 +56,7 @@ class Banwords(Plugin): if self.action == "ignore": f = self.searchr.FindFirst(content) if f: - logger.info("Banwords: %s" % f["Keyword"]) + logger.info("[Banwords] %s in message" % f["Keyword"]) e_context.action = EventAction.BREAK_PASS return elif self.action == "replace": @@ -63,5 +66,26 @@ class Banwords(Plugin): e_context.action = EventAction.BREAK_PASS return + def on_decorate_reply(self, e_context: EventContext): + + if e_context['reply'].type not in [ReplyType.TEXT]: + return + + reply = e_context['reply'] + content = reply.content + if self.reply_action == "ignore": + f = self.searchr.FindFirst(content) + if f: + logger.info("[Banwords] %s in reply" % f["Keyword"]) + e_context['reply'] = None + e_context.action = EventAction.BREAK_PASS + return + elif self.reply_action == "replace": + if self.searchr.ContainsAny(content): + reply = Reply(ReplyType.INFO, "已替换回复中的敏感词: \n"+self.searchr.Replace(content)) + e_context['reply'] = reply + e_context.action = EventAction.CONTINUE + return + def get_help_text(self, **kwargs): return Banwords.desc \ No newline at end of file diff --git a/plugins/banwords/config.json.template b/plugins/banwords/config.json.template index 000fdda..8b4d3d1 100644 --- a/plugins/banwords/config.json.template +++ b/plugins/banwords/config.json.template @@ -1,3 +1,5 @@ { - "action": "ignore" + "action": "replace", + "reply_filter": true, + "reply_action": "ignore" } \ No newline at end of file