diff --git a/README.md b/README.md index 938f1599..ae6800b8 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ ✅支持敏感词过滤,避免账号风险 - 难以监测机器人与用户对话时的内容,故引入此功能以减少机器人风险 + - 加入了百度云内容审核,在`config.py`中修改`baidu_check`的值,并填写`baidu_api_key`和`baidu_secret_key`以开启此功能 - 编辑`sensitive.json`,并在`config.py`中修改`sensitive_word_filter`的值以开启此功能 diff --git a/config-template.py b/config-template.py index 5e6996ed..347f7d52 100644 --- a/config-template.py +++ b/config-template.py @@ -106,6 +106,19 @@ ignore_rules = { # 请在sensitive.json中添加敏感词 sensitive_word_filter = True +# 是否启用百度云内容安全审核 +# 注册方式查看 https://cloud.baidu.com/doc/ANTIPORN/s/Wkhu9d5iy +baidu_check = False + +# 百度云API_KEY 24位英文数字字符串 +baidu_api_key = "" + +# 百度云SECRET_KEY 32位的英文数字字符串 +baidu_secret_key = "" + +# 不合规消息自定义返回 +inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" + # 启动时是否发送赞赏码 # 仅当使用量已经超过2048字时发送 encourage_sponsor_at_start = True diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 7cbca39b..e5ee8794 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -1,19 +1,71 @@ # 敏感词过滤模块 import re +import requests +import json +import logging class ReplyFilter: - sensitive_words = [] + # 默认值( 兼容性考虑 ) + baidu_check = False + baidu_api_key = "" + baidu_secret_key = "" + inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" + def __init__(self, sensitive_words: list): self.sensitive_words = sensitive_words + import config + if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'): + self.baidu_check = config.baidu_check + self.baidu_api_key = config.baidu_api_key + self.baidu_secret_key = config.baidu_secret_key + self.inappropriate_message_tips = config.inappropriate_message_tips def process(self, message: str) -> str: + + # 本地关键词屏蔽 for word in self.sensitive_words: match = re.findall(word, message) if len(match) > 0: for i in range(len(match)): message = message.replace(match[i], "*" * len(match[i])) + # 百度云审核 + if self.baidu_check: + + # 百度云审核URL + baidu_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + \ + str(requests.post("https://aip.baidubce.com/oauth/2.0/token", + params={"grant_type": "client_credentials", + "client_id": self.baidu_api_key, + "client_secret": self.baidu_secret_key}).json().get("access_token")) + + # 百度云审核 + payload = "text=" + message + logging.info("向百度云发送:" + payload) + headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'} + + if isinstance(payload, str): + payload = payload.encode('utf-8') + + response = requests.request("POST", baidu_url, headers=headers, data=payload) + response_dict = json.loads(response.text) + + if "error_code" in response_dict: + error_msg = response_dict.get("error_msg") + logging.warning(f"百度云判定出错,错误信息:{error_msg}") + conclusion = f"百度云判定出错,错误信息:{error_msg}\n以下是原消息:{message}" + else: + conclusion = response_dict["conclusion"] + if conclusion in ("合规"): + logging.info(f"百度云判定结果:{conclusion}") + return message + else: + logging.warning(f"百度云判定结果:{conclusion}") + conclusion = self.inappropriate_message_tips + # 返回百度云审核结果 + return conclusion + return message