From fde101c435f67fdd4abe76cbf83bd975b2df3b06 Mon Sep 17 00:00:00 2001 From: onewhitethreee <38567612+onewhitethreee@users.noreply.github.com> Date: Sun, 5 Mar 2023 00:19:59 +0100 Subject: [PATCH 1/5] Update wechat_mp_channel.py --- channel/wechat/wechat_mp_channel.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index d4fd9bc..142b443 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -5,6 +5,7 @@ from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor +sensitive_word = [] robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) @@ -12,12 +13,16 @@ cache = {} @robot.text def hello_world(msg): - logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) - key = msg.content + '|' + msg.source - if cache.get(key): - # request time - cache.get(key)['req_times'] += 1 - return WechatSubsribeAccount().handle(msg) + with open('sensitive_words.txt', 'r', encoding='utf-8') as f: #加入检测违规词 + if msg.content in f.read(): + return '你输入的内容包含敏感词汇' + else: + logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) + key = msg.content + '|' + msg.source + if cache.get(key): + # request time + cache.get(key)['req_times'] += 1 + return WechatSubsribeAccount().handle(msg) class WechatSubsribeAccount(Channel): @@ -59,4 +64,4 @@ class WechatSubsribeAccount(Channel): reply_text = super().build_reply_content(query, context) logger.info('[WX_Public] reply content: {}'.format(reply_text)) - cache[key] = {"status": "success", "data": reply_text} \ No newline at end of file + cache[key] = {"status": "success", "data": reply_text} From c78dd25729ab2e1e857e92e6ed66416f5e530aba Mon Sep 17 00:00:00 2001 From: onewhitethreee <38567612+onewhitethreee@users.noreply.github.com> Date: Sun, 5 Mar 2023 00:23:27 +0100 Subject: [PATCH 2/5] Update wechat_mp_channel.py --- channel/wechat/wechat_mp_channel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index 142b443..ca14eeb 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -5,7 +5,6 @@ from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor -sensitive_word = [] robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) From bf3b0975b9afd9f9cefb4b5348875f4df4da8e67 Mon Sep 17 00:00:00 2001 From: onewhitethreee <38567612+onewhitethreee@users.noreply.github.com> Date: Sun, 5 Mar 2023 01:21:49 +0100 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_mp_channel.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index ca14eeb..e7f8dd8 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -13,7 +13,15 @@ cache = {} @robot.text def hello_world(msg): with open('sensitive_words.txt', 'r', encoding='utf-8') as f: #加入检测违规词 - if msg.content in f.read(): + sensitive_wordss = [msg.content[i:i+2] for i in range(0, len(msg.content), 2)] + found = False + for i in sensitive_wordss: + if i in f.read(): + found = True + break + else: + found = False + if found: return '你输入的内容包含敏感词汇' else: logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) @@ -24,6 +32,7 @@ def hello_world(msg): return WechatSubsribeAccount().handle(msg) + class WechatSubsribeAccount(Channel): def startup(self): logger.info('[WX_Public] Wechat Public account service start!') From f7c55920dfed200bba78eecc075fbfc22a07e033 Mon Sep 17 00:00:00 2001 From: onewhitethreee <38567612+onewhitethreee@users.noreply.github.com> Date: Sun, 5 Mar 2023 11:32:29 +0100 Subject: [PATCH 4/5] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E4=BA=86=E6=95=8F?= =?UTF-8?q?=E6=84=9F=E8=AF=8D=E4=B8=BA=E7=A9=BA=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_mp_channel.py | 35 ++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index e7f8dd8..42e67aa 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -5,6 +5,7 @@ from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor +sensitive_word = [] robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) @@ -15,22 +16,26 @@ def hello_world(msg): with open('sensitive_words.txt', 'r', encoding='utf-8') as f: #加入检测违规词 sensitive_wordss = [msg.content[i:i+2] for i in range(0, len(msg.content), 2)] found = False - for i in sensitive_wordss: - if i in f.read(): - found = True - break - else: - found = False - if found: - return '你输入的内容包含敏感词汇' - else: - logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) - key = msg.content + '|' + msg.source - if cache.get(key): - # request time - cache.get(key)['req_times'] += 1 + #如果文件为空 + if not f.read(): + print('empty file in sensitive_words.txt') #如为空则不限制 return WechatSubsribeAccount().handle(msg) - + else: + for i in sensitive_wordss: + if i in f.read(): + found = True + break + else: + found = False + if found: + return '你输入的内容包含敏感词汇' + else: + logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) + key = msg.content + '|' + msg.source + if cache.get(key): + # request time + cache.get(key)['req_times'] += 1 + return WechatSubsribeAccount().handle(msg) class WechatSubsribeAccount(Channel): From 750f95bfa463ef92d48791b3613e845b86c917ec Mon Sep 17 00:00:00 2001 From: onewhitethreee <38567612+onewhitethreee@users.noreply.github.com> Date: Sun, 5 Mar 2023 11:47:47 +0100 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=BA=E7=A9=BA=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_mp_channel.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index 42e67aa..7279311 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -5,7 +5,9 @@ from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor -sensitive_word = [] +import os + + robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) @@ -16,10 +18,9 @@ def hello_world(msg): with open('sensitive_words.txt', 'r', encoding='utf-8') as f: #加入检测违规词 sensitive_wordss = [msg.content[i:i+2] for i in range(0, len(msg.content), 2)] found = False - #如果文件为空 - if not f.read(): - print('empty file in sensitive_words.txt') #如为空则不限制 - return WechatSubsribeAccount().handle(msg) + #判断文件是否为空 + if not os.path.getsize('sensitive_words.txt'): + found = False else: for i in sensitive_wordss: if i in f.read(): @@ -27,15 +28,15 @@ def hello_world(msg): break else: found = False - if found: - return '你输入的内容包含敏感词汇' - else: - logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) - key = msg.content + '|' + msg.source - if cache.get(key): - # request time - cache.get(key)['req_times'] += 1 - return WechatSubsribeAccount().handle(msg) + if found: + return '你输入的内容包含敏感词汇' + else: + logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) + key = msg.content + '|' + msg.source + if cache.get(key): + # request time + cache.get(key)['req_times'] += 1 + return WechatSubsribeAccount().handle(msg) class WechatSubsribeAccount(Channel):