ソースを参照

Merge pull request #569 from a5225662/master

时间管理模块加入到common目录,并增加了3条关于时间管理的config配置
zhayujie 3 年 前
コミット
f3daa8e3bf
4 ファイル変更47 行追加3 行削除
  1. 4 1
      channel/wechat/wechat_channel.py
  2. 39 0
      common/time_check.py
  3. 4 1
      config-template.json
  4. 0 1
      config.py

+ 4 - 1
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)

+ 39 - 0
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
+

+ 4 - 1
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"   
  }
 

+ 0 - 1
config.py

@@ -6,7 +6,6 @@ from common.log import logger
 
 config = {}
 
-
 def load_config():
     global config
     config_path = "./config.json"