mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
Merge Pull Request #686 into master
This commit is contained in:
36
config.py
36
config.py
@@ -4,6 +4,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
from common.log import logger
|
||||
import pickle
|
||||
|
||||
# 将所有可用的配置项写在字典里, 请使用小写字母
|
||||
available_setting = {
|
||||
@@ -76,11 +77,14 @@ available_setting = {
|
||||
# wechaty的配置
|
||||
"wechaty_puppet_service_token": "", # wechaty的token
|
||||
|
||||
# wechatmp的配置
|
||||
"wechatmp_token": "", # 微信公众平台的Token
|
||||
|
||||
# chatgpt指令自定义触发词
|
||||
"clear_memory_commands": ['#清除记忆'], # 重置会话指令,必须以#开头
|
||||
|
||||
# channel配置
|
||||
"channel_type": "wx", # 通道类型,支持wx,wxy和terminal
|
||||
"channel_type": "wx", # 通道类型,支持:{wx,wxy,terminal,wechatmp}
|
||||
|
||||
"debug": False, # 是否开启debug模式,开启后会打印更多日志
|
||||
|
||||
@@ -88,6 +92,11 @@ available_setting = {
|
||||
|
||||
|
||||
class Config(dict):
|
||||
def __init__(self, d:dict={}):
|
||||
super().__init__(d)
|
||||
# user_datas: 用户数据,key为用户名,value为用户数据,也是dict
|
||||
self.user_datas = {}
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key not in available_setting:
|
||||
raise Exception("key {} not in available_setting".format(key))
|
||||
@@ -106,6 +115,30 @@ class Config(dict):
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
# Make sure to return a dictionary to ensure atomic
|
||||
def get_user_data(self, user) -> dict:
|
||||
if self.user_datas.get(user) is None:
|
||||
self.user_datas[user] = {}
|
||||
return self.user_datas[user]
|
||||
|
||||
def load_user_datas(self):
|
||||
try:
|
||||
with open('user_datas.pkl', 'rb') as f:
|
||||
self.user_datas = pickle.load(f)
|
||||
logger.info("[Config] User datas loaded.")
|
||||
except FileNotFoundError as e:
|
||||
logger.info("[Config] User datas file not found, ignore.")
|
||||
except Exception as e:
|
||||
logger.info("[Config] User datas error: {}".format(e))
|
||||
self.user_datas = {}
|
||||
|
||||
def save_user_datas(self):
|
||||
try:
|
||||
with open('user_datas.pkl', 'wb') as f:
|
||||
pickle.dump(self.user_datas, f)
|
||||
logger.info("[Config] User datas saved.")
|
||||
except Exception as e:
|
||||
logger.info("[Config] User datas error: {}".format(e))
|
||||
|
||||
config = Config()
|
||||
|
||||
@@ -146,6 +179,7 @@ def load_config():
|
||||
|
||||
logger.info("[INIT] load config: {}".format(config))
|
||||
|
||||
config.load_user_datas()
|
||||
|
||||
def get_root():
|
||||
return os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
Reference in New Issue
Block a user