Browse Source

banwords: support reply filter

master
lanvent 1 year ago
parent
commit
82ec440b45
3 changed files with 47 additions and 3 deletions
  1. +19
    -1
      plugins/banwords/README.md
  2. +25
    -1
      plugins/banwords/banwords.py
  3. +3
    -1
      plugins/banwords/config.json.template

+ 19
- 1
plugins/banwords/README.md View File

@@ -1,9 +1,27 @@

## 插件描述 ## 插件描述

简易的敏感词插件,暂不支持分词,请自行导入词库到插件文件夹中的`banwords.txt`,每行一个词,一个参考词库是[1](https://github.com/cjh0613/tencent-sensitive-words/blob/main/sensitive_words_lines.txt)。 简易的敏感词插件,暂不支持分词,请自行导入词库到插件文件夹中的`banwords.txt`,每行一个词,一个参考词库是[1](https://github.com/cjh0613/tencent-sensitive-words/blob/main/sensitive_words_lines.txt)。


`config.json`中能够填写默认的处理行为,目前行为有:
使用前将`config.json.template`复制为`config.json`,并自行配置。

目前插件对消息的默认处理行为有如下两种:

- `ignore` : 无视这条消息。 - `ignore` : 无视这条消息。
- `replace` : 将消息中的敏感词替换成"*",并回复违规。 - `replace` : 将消息中的敏感词替换成"*",并回复违规。


```json
"action": "replace",
"reply_filter": true,
"reply_action": "ignore"
```

在以上配置项中:

- `action`: 对用户消息的默认处理行为
- `reply_filter`: 是否对ChatGPT的回复也进行敏感词过滤
- `reply_action`: 如果开启了回复过滤,对回复的默认处理行为

## 致谢 ## 致谢

搜索功能实现来自https://github.com/toolgood/ToolGood.Words 搜索功能实现来自https://github.com/toolgood/ToolGood.Words

+ 25
- 1
plugins/banwords/banwords.py View File

@@ -36,6 +36,9 @@ class Banwords(Plugin):
words.append(word) words.append(word)
self.searchr.SetKeywords(words) self.searchr.SetKeywords(words)
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context 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") logger.info("[Banwords] inited")
except Exception as e: except Exception as e:
logger.warn("[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords .") 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": if self.action == "ignore":
f = self.searchr.FindFirst(content) f = self.searchr.FindFirst(content)
if f: if f:
logger.info("Banwords: %s" % f["Keyword"])
logger.info("[Banwords] %s in message" % f["Keyword"])
e_context.action = EventAction.BREAK_PASS e_context.action = EventAction.BREAK_PASS
return return
elif self.action == "replace": elif self.action == "replace":
@@ -63,5 +66,26 @@ class Banwords(Plugin):
e_context.action = EventAction.BREAK_PASS e_context.action = EventAction.BREAK_PASS
return 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): def get_help_text(self, **kwargs):
return Banwords.desc return Banwords.desc

+ 3
- 1
plugins/banwords/config.json.template View File

@@ -1,3 +1,5 @@
{ {
"action": "ignore"
"action": "replace",
"reply_filter": true,
"reply_action": "ignore"
} }

Loading…
Cancel
Save