diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index c3c2ed14..bc5faf99 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -14,8 +14,8 @@ from concurrent.futures import ThreadPoolExecutor from common.log import logger from common.tmp_dir import TmpDir from config import conf +from common.time_check import time_checker from plugins import * - import requests import io import time @@ -77,6 +77,8 @@ class WechatChannel(Channel): context.kwargs = {'isgroup': False, 'msg': msg, 'receiver': other_user_id, 'session_id': other_user_id} thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) + + @time_checker def handle_text(self, msg): logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False)) content = msg['Text'] @@ -108,6 +110,7 @@ class WechatChannel(Channel): context.content = content thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) + @time_checker def handle_group(self, msg): logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) group_name = msg['User'].get('NickName', None) diff --git a/common/time_check.py b/common/time_check.py new file mode 100644 index 00000000..56ea3361 --- /dev/null +++ b/common/time_check.py @@ -0,0 +1,39 @@ +import time,re,hashlib +import config +from common.log import logger + +def time_checker(f): + def _time_checker(self, *args, **kwargs): + _config = config.conf() + chat_time_module = _config["chat_time_module"] + chat_start_time = _config["chat_start_time"] + chat_stopt_time = _config["chat_stop_time"] + + if chat_time_module: + time_regex = re.compile(r'^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$') #时间匹配,包含24:00 + + starttime_format_check = time_regex.match(chat_start_time) # 检查停止时间格式 + stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式 + chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间 + + # 时间格式检查 + if not (starttime_format_check and stoptime_format_check and chat_time_check): + logger.warn('时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,开始({})-结束({})'.format(starttime_format_check,stoptime_format_check)) + if chat_start_time>"23:59": + logger.error('启动时间可能存在问题,请修改!') + + # 服务时间检查 + now_time = time.strftime("%H:%M", time.localtime()) + if chat_start_time <= now_time <= chat_stopt_time: # 服务时间内,正常返回回答 + f(self, *args, **kwargs) + return None + else: + if args[0]['Content'] == "#更新配置": # 不在服务时间内也可以更新配置 + f(self, *args, **kwargs) + else: + logger.info('非服务时间内,不接受访问') + return None + else: + f(self, *args, **kwargs) # 未开启时间模块则直接回答 + return _time_checker + diff --git a/config-template.json b/config-template.json index 359de500..b6fdc978 100644 --- a/config-template.json +++ b/config-template.json @@ -11,6 +11,9 @@ "voice_reply_voice": false, "conversation_max_tokens": 1000, "expires_in_seconds": 3600, - "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", + "chat_time_module": false, + "chat_start_time": "00:00", + "chat_stop_time": "24:00" } diff --git a/config.py b/config.py index 9cce2f09..1a5a1f29 100644 --- a/config.py +++ b/config.py @@ -6,7 +6,6 @@ from common.log import logger config = {} - def load_config(): global config config_path = "./config.json"