diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index d4fd9bc..7279311 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -5,6 +5,9 @@ from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor +import os + + robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) @@ -12,12 +15,28 @@ 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: #加入检测违规词 + sensitive_wordss = [msg.content[i:i+2] for i in range(0, len(msg.content), 2)] + found = False + #判断文件是否为空 + if not os.path.getsize('sensitive_words.txt'): + found = False + 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): @@ -59,4 +78,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}