From 480d201c55ecc610604e43818eeb34edb4f70ef0 Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sat, 4 Mar 2023 21:02:10 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BE=E5=BA=A6?= =?UTF-8?q?=E4=BA=91=E5=86=85=E5=AE=B9=E5=AE=A1=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 12 +++++++++++ pkg/qqbot/filter.py | 49 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/config-template.py b/config-template.py index fec0a7ef..b73bc871 100644 --- a/config-template.py +++ b/config-template.py @@ -106,6 +106,18 @@ ignore_rules = { # 请在sensitive.json中添加敏感词 sensitive_word_filter = True +#是否启动百度云内容安全审核 +check = False + +#百度云API_KEY 24位英文数字字符串 +baidu_api_key = "" + +#百度云SECRET_KEY 32位的英文数字字符串 +baidu_secret_key ="" + +#不合规消息自定义返回 +illgalmessage = "[百度云]请珍惜机器人,当前返回内容不合规" + # 启动时是否发送赞赏码 # 仅当使用量已经超过2048字时发送 encourage_sponsor_at_start = True diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 7cbca39b..e670aa22 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -1,19 +1,64 @@ # 敏感词过滤模块 import re +import requests +import json +from config import check, baidu_api_key, baidu_secret_key, illgalmessage +import logging + + +# 然后可以通过config.check, config.baidu_api_key等方式来使用这些变量。 + +def get_access_token(): + """ + 使用 AK,SK 生成鉴权签名(Access Token) + :return: access_token,或是None(如果错误) + """ + url = "https://aip.baidubce.com/oauth/2.0/token" + params = {"grant_type": "client_credentials", "client_id": baidu_api_key, + "client_secret": baidu_secret_key} + return str(requests.post(url, params=params).json().get("access_token")) + + +# 百度云审核URL +baidu_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" \ + + get_access_token() class ReplyFilter: - sensitive_words = [] def __init__(self, sensitive_words: list): self.sensitive_words = sensitive_words def process(self, message: str) -> str: + # 百度云审核 + if check: + # 百度云审核 + payload = "text=" + message + logging.info("向百度云发送:" + payload) + headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'} + response = requests.request("POST", baidu_url, headers=headers, data=payload.encode('utf-8')) + response_dict = json.loads(response.text) + # 处理百度云审核结果 + if "error_code" in response_dict: + error_msg = response_dict.get("error_msg") + logging.info(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.info(f"百度云判定结果:{conclusion}") + conclusion = illgalmessage + # 返回百度云审核结果 + return conclusion + + # 本地关键词屏蔽 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])) - return message From c89a8e1cd1e979b8a2d2bd95792a3e238f21cc3e Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sat, 4 Mar 2023 21:06:58 +0800 Subject: [PATCH 2/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 938f1599..8271a1a9 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ ✅支持敏感词过滤,避免账号风险 - 难以监测机器人与用户对话时的内容,故引入此功能以减少机器人风险 + - 加入了百度云内容审核 - 编辑`sensitive.json`,并在`config.py`中修改`sensitive_word_filter`的值以开启此功能 From b8ed9ba321b97e45a9db1bbb31b6f89b90375f57 Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sat, 4 Mar 2023 21:08:48 +0800 Subject: [PATCH 3/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8271a1a9..25d9fdc3 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ ✅支持敏感词过滤,避免账号风险 - 难以监测机器人与用户对话时的内容,故引入此功能以减少机器人风险 - - 加入了百度云内容审核 + - 加入了百度云内容审核,在`config.py`中修改`check`的值,并填写`baidu_api_key`和`baidu_secret_key`以开启此功能 - 编辑`sensitive.json`,并在`config.py`中修改`sensitive_word_filter`的值以开启此功能 From 2e1ebff985c1786eabf42f4332a2cbc62bba2f1f Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sat, 4 Mar 2023 21:12:50 +0800 Subject: [PATCH 4/8] change value name --- README.md | 2 +- config-template.py | 2 +- pkg/qqbot/filter.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 25d9fdc3..ae6800b8 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ ✅支持敏感词过滤,避免账号风险 - 难以监测机器人与用户对话时的内容,故引入此功能以减少机器人风险 - - 加入了百度云内容审核,在`config.py`中修改`check`的值,并填写`baidu_api_key`和`baidu_secret_key`以开启此功能 + - 加入了百度云内容审核,在`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 b73bc871..c3603b39 100644 --- a/config-template.py +++ b/config-template.py @@ -107,7 +107,7 @@ ignore_rules = { sensitive_word_filter = True #是否启动百度云内容安全审核 -check = False +baidu_check = False #百度云API_KEY 24位英文数字字符串 baidu_api_key = "" diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index e670aa22..1878c083 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -2,7 +2,7 @@ import re import requests import json -from config import check, baidu_api_key, baidu_secret_key, illgalmessage +from config import baidu_check, baidu_api_key, baidu_secret_key, illgalmessage import logging @@ -32,7 +32,7 @@ class ReplyFilter: def process(self, message: str) -> str: # 百度云审核 - if check: + if baidu_check: # 百度云审核 payload = "text=" + message logging.info("向百度云发送:" + payload) From bc6728d12357068d9caa96e3aba6847bf95b143b Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sun, 5 Mar 2023 01:17:23 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 2 +- pkg/qqbot/filter.py | 68 +++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/config-template.py b/config-template.py index c3603b39..3d7c5163 100644 --- a/config-template.py +++ b/config-template.py @@ -116,7 +116,7 @@ baidu_api_key = "" baidu_secret_key ="" #不合规消息自定义返回 -illgalmessage = "[百度云]请珍惜机器人,当前返回内容不合规" +inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" # 启动时是否发送赞赏码 # 仅当使用量已经超过2048字时发送 diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 1878c083..81f4abe6 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -2,44 +2,64 @@ import re import requests import json -from config import baidu_check, baidu_api_key, baidu_secret_key, illgalmessage +import importlib import logging +# 默认值( 兼容性考虑 ) -# 然后可以通过config.check, config.baidu_api_key等方式来使用这些变量。 +baidu_check = False +baidu_api_key = "" +baidu_secret_key = "" +inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" -def get_access_token(): - """ - 使用 AK,SK 生成鉴权签名(Access Token) - :return: access_token,或是None(如果错误) - """ - url = "https://aip.baidubce.com/oauth/2.0/token" - params = {"grant_type": "client_credentials", "client_id": baidu_api_key, - "client_secret": baidu_secret_key} - return str(requests.post(url, params=params).json().get("access_token")) - - -# 百度云审核URL -baidu_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" \ - + get_access_token() +# 初始化 +config = importlib.import_module('config') +baidu_check = config.baidu_check +baidu_api_key = config.baidu_api_key +baidu_secret_key = config.baidu_secret_key +inappropriate_message_tips = config.inappropriate_message_tips class ReplyFilter: sensitive_words = [] def __init__(self, sensitive_words: list): self.sensitive_words = sensitive_words + 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 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 baidu_check: + 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": baidu_api_key, + "client_secret": baidu_secret_key}).json().get("access_token")) + # 百度云审核 payload = "text=" + message logging.info("向百度云发送:" + payload) headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'} - response = requests.request("POST", baidu_url, headers=headers, data=payload.encode('utf-8')) + + 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.info(f"百度云判定出错,错误信息:{error_msg}") @@ -51,14 +71,8 @@ class ReplyFilter: return message else: logging.info(f"百度云判定结果:{conclusion}") - conclusion = illgalmessage + conclusion = inappropriate_message_tips # 返回百度云审核结果 return conclusion - - # 本地关键词屏蔽 - 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])) + return message From 42f5092bb9c1c19d62568920eb74596d0ebfd3c3 Mon Sep 17 00:00:00 2001 From: Haibersut Date: Sun, 5 Mar 2023 01:45:36 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E6=97=A5?= =?UTF-8?q?=E8=AE=B0=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将错误信息调整为warning --- pkg/qqbot/filter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 81f4abe6..31ebabd1 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -62,7 +62,7 @@ class ReplyFilter: if "error_code" in response_dict: error_msg = response_dict.get("error_msg") - logging.info(f"百度云判定出错,错误信息:{error_msg}") + logging.warning(f"百度云判定出错,错误信息:{error_msg}") conclusion = f"百度云判定出错,错误信息:{error_msg}\n以下是原消息:{message}" else: conclusion = response_dict["conclusion"] @@ -70,7 +70,7 @@ class ReplyFilter: logging.info(f"百度云判定结果:{conclusion}") return message else: - logging.info(f"百度云判定结果:{conclusion}") + logging.warning(f"百度云判定结果:{conclusion}") conclusion = inappropriate_message_tips # 返回百度云审核结果 return conclusion From cc3beb191fb90c83e6d1737ec2784279d386e204 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 09:54:44 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix:=20=E7=99=BE=E5=BA=A6=E4=BA=91=E5=AE=A1?= =?UTF-8?q?=E6=A0=B8=E7=9A=84=E9=85=8D=E7=BD=AE=E4=BD=8E=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/filter.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 31ebabd1..e5ee8794 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -2,33 +2,26 @@ import re import requests import json -import importlib import logging -# 默认值( 兼容性考虑 ) - -baidu_check = False -baidu_api_key = "" -baidu_secret_key = "" -inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" - -# 初始化 - -config = importlib.import_module('config') -baidu_check = config.baidu_check -baidu_api_key = config.baidu_api_key -baidu_secret_key = config.baidu_secret_key -inappropriate_message_tips = config.inappropriate_message_tips 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: @@ -46,8 +39,8 @@ class ReplyFilter: 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": baidu_api_key, - "client_secret": baidu_secret_key}).json().get("access_token")) + "client_id": self.baidu_api_key, + "client_secret": self.baidu_secret_key}).json().get("access_token")) # 百度云审核 payload = "text=" + message @@ -71,7 +64,7 @@ class ReplyFilter: return message else: logging.warning(f"百度云判定结果:{conclusion}") - conclusion = inappropriate_message_tips + conclusion = self.inappropriate_message_tips # 返回百度云审核结果 return conclusion From 0bba205cf2759578f4e0e5e199d5a300a4301fa5 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 5 Mar 2023 10:12:49 +0800 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/config-template.py b/config-template.py index 3d7c5163..c1afcdd1 100644 --- a/config-template.py +++ b/config-template.py @@ -106,16 +106,17 @@ ignore_rules = { # 请在sensitive.json中添加敏感词 sensitive_word_filter = True -#是否启动百度云内容安全审核 +# 是否启用百度云内容安全审核 +# 注册方式查看 https://cloud.baidu.com/doc/ANTIPORN/s/Wkhu9d5iy baidu_check = False -#百度云API_KEY 24位英文数字字符串 +# 百度云API_KEY 24位英文数字字符串 baidu_api_key = "" -#百度云SECRET_KEY 32位的英文数字字符串 -baidu_secret_key ="" +# 百度云SECRET_KEY 32位的英文数字字符串 +baidu_secret_key = "" -#不合规消息自定义返回 +# 不合规消息自定义返回 inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" # 启动时是否发送赞赏码