From f28ac0166536a2a4b68cac685a41ea667f60f7e9 Mon Sep 17 00:00:00 2001 From: yj <2077506045@qq.com> Date: 星期三, 03 九月 2025 14:43:03 +0800 Subject: [PATCH] 兼容企业微信 --- config.py | 205 ++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 178 insertions(+), 27 deletions(-) diff --git a/config.py b/config.py index ae16800..79c1456 100644 --- a/config.py +++ b/config.py @@ -3,44 +3,195 @@ """ import os -from typing import Optional -from pydantic_settings import BaseSettings +import json +import threading +from loguru import logger -class Settings(BaseSettings): +class Settings: """搴旂敤閰嶇疆""" - # 鏁版嵁搴撻厤缃� - database_url: str = "mysql+pymysql://root:TAI%402019%23Zjun@120.24.39.179:3306/ecloud_dify" + def __init__(self, config_file: str = "config.json"): + """鍒濆鍖栭厤缃紝浠嶫SON鏂囦欢鍔犺浇""" + self.config_file = config_file + self._lock = threading.Lock() # 鐢ㄤ簬绾跨▼瀹夊叏鐨勯厤缃洿鏂� + self._load_config() - # Redis閰嶇疆 - redis_url: str = "redis://localhost:6379/0" + def _load_config(self): + """浠嶫SON鏂囦欢鍔犺浇閰嶇疆""" + if not os.path.exists(self.config_file): + raise FileNotFoundError(f"閰嶇疆鏂囦欢 {self.config_file} 涓嶅瓨鍦�") - # E浜戠瀹堕厤缃� - ecloud_base_url: str = "http://your-ecloud-domain.com" - ecloud_authorization: str = "your-authorization-token" + try: + with open(self.config_file, 'r', encoding='utf-8') as f: + config_data = json.load(f) + self._set_config_from_dict(config_data) + except Exception as e: + raise Exception(f"鍔犺浇閰嶇疆鏂囦欢澶辫触: {e}") - # DifyAI閰嶇疆 - dify_base_url: str = "https://api.dify.ai/v1" - dify_api_key: str = "your-dify-api-key" + def _set_config_from_dict(self, config_data: dict): + """浠庡瓧鍏歌缃厤缃�""" + # 鏁版嵁搴撻厤缃� + self.database_url = config_data["database"]["url"] - # 鏈嶅姟閰嶇疆 - server_host: str = "0.0.0.0" - server_port: int = 8000 - debug: bool = True + # Redis閰嶇疆 + self.redis_url = config_data["redis"]["url"] - # 鏃ュ織閰嶇疆 - log_level: str = "INFO" - log_file: str = "logs/app.log" + # E浜戠瀹堕厤缃� + ecloud_config = config_data["ecloud"] + self.ecloud_base_url = ecloud_config["base_url"] + self.ecloud_authorization = ecloud_config["authorization"] + self.ecloud_w_id = ecloud_config["w_id"] - # 娑堟伅澶勭悊閰嶇疆 - max_retry_count: int = 3 - retry_delay: int = 5 # 绉� - queue_timeout: int = 300 # 绉� + # DifyAI閰嶇疆 + dify_config = config_data["dify"] + self.dify_base_url = dify_config["base_url"] + self.dify_api_key = dify_config["api_key"] + self.dify_streaming_enabled = dify_config["streaming_enabled"] + self.dify_streaming_timeout = dify_config["streaming_timeout"] - class Config: - env_file = ".env" - env_file_encoding = "utf-8" + # 鏈嶅姟閰嶇疆 + server_config = config_data["server"] + self.server_host = server_config["host"] + self.server_port = server_config["port"] + self.debug = server_config["debug"] + + # 鏃ュ織閰嶇疆 + logging_config = config_data["logging"] + self.log_level = logging_config["level"] + self.log_file = logging_config["file"] + + # 娑堟伅澶勭悊閰嶇疆 + msg_config = config_data["message_processing"] + self.max_retry_count = msg_config["max_retry_count"] + self.retry_delay = msg_config["retry_delay"] + self.queue_timeout = msg_config["queue_timeout"] + + # 瀹㈡湇閰嶇疆 + customer_service_config = config_data["customer_service"] + self.customer_service_names = customer_service_config["names"] + self.customer_service_default_name = customer_service_config.get("default_name", "鏅鸿兘瀹㈡湇") + + # 濂藉弸蹇界暐閰嶇疆 + friend_ignore_config = config_data["friend_ignore"] + self.friend_ignore_enabled = friend_ignore_config["enabled"] + self.friend_ignore_whitelist = friend_ignore_config["whitelist"] + + # 闈欓粯妯″紡閰嶇疆 + silence_mode_config = config_data.get("silence_mode", {}) + self.silence_mode_enabled = silence_mode_config.get("enabled", True) + self.silence_duration_minutes = silence_mode_config.get("duration_minutes", 10) + + # 鍦ㄧ嚎鐘舵�佺洃鎺ч厤缃� + online_status_config = config_data["online_status_monitor"] + self.online_status_enabled = online_status_config["enabled"] + self.online_status_check_interval = online_status_config["check_interval_minutes"] + self.online_status_notification_message = online_status_config["notification_message"] + + # 閭欢閫氱煡閰嶇疆 + email_config = config_data["email_notification"] + self.email_enabled = email_config["enabled"] + self.email_smtp_server = email_config["smtp_server"] + self.email_smtp_port = email_config["smtp_port"] + self.email_smtp_username = email_config["smtp_username"] + self.email_smtp_password = email_config["smtp_password"] + self.email_from_email = email_config["from_email"] + self.email_to_emails = email_config["to_emails"] + + # 鐭俊閫氱煡閰嶇疆 + sms_config = config_data["sms_notification"] + self.sms_enabled = sms_config["enabled"] + self.sms_api_url = sms_config["api_url"] + self.sms_username = sms_config["username"] + self.sms_password = sms_config["password"] + self.sms_phone_numbers = sms_config["phone_numbers"] + + # 鍏抽敭璇嶈繃婊ら厤缃� + keyword_filter_config = config_data.get("keyword_filter", {}) + self.keyword_filter_enabled = keyword_filter_config.get("enabled", False) + self.keyword_filter_keywords = keyword_filter_config.get("keywords", []) + + # 娑堟伅鑱氬悎閰嶇疆 + message_aggregation_config = config_data.get("message_aggregation", {}) + self.message_aggregation_enabled = message_aggregation_config.get("enabled", True) + self.message_aggregation_timeout = message_aggregation_config.get("timeout_seconds", 15) + + # 缁撴潫瀛楃涓查厤缃� + self.end_str_list = config_data.get("end_str_list", []) + + # 娴嬭瘯缇ょ粍鍏抽敭瀛楅厤缃� + self.test_group_keywords = config_data.get("test_group_keywords", []) + + def update_ecloud_w_id(self, new_w_id: str) -> bool: + """ + 鍔ㄦ�佹洿鏂癊浜戠瀹剁殑w_id閰嶇疆 + + Args: + new_w_id: 鏂扮殑w_id鍊� + + Returns: + 鏇存柊鎴愬姛杩斿洖True锛屽け璐ヨ繑鍥濬alse + """ + if not new_w_id or new_w_id == self.ecloud_w_id: + return True + + with self._lock: + try: + # 鏇存柊鍐呭瓨涓殑閰嶇疆 + old_w_id = self.ecloud_w_id + self.ecloud_w_id = new_w_id + + # 鏇存柊閰嶇疆鏂囦欢 + if self._update_config_file_w_id(new_w_id): + logger.info(f"鎴愬姛鏇存柊w_id: {old_w_id} -> {new_w_id}") + return True + else: + # 濡傛灉鏂囦欢鏇存柊澶辫触锛屽洖婊氬唴瀛橀厤缃� + self.ecloud_w_id = old_w_id + logger.error(f"鏇存柊閰嶇疆鏂囦欢澶辫触锛屽洖婊歸_id: {new_w_id} -> {old_w_id}") + return False + + except Exception as e: + logger.error(f"鏇存柊w_id寮傚父: {str(e)}") + return False + + def _update_config_file_w_id(self, new_w_id: str) -> bool: + """ + 鏇存柊閰嶇疆鏂囦欢涓殑w_id + + Args: + new_w_id: 鏂扮殑w_id鍊� + + Returns: + 鏇存柊鎴愬姛杩斿洖True锛屽け璐ヨ繑鍥濬alse + """ + try: + # 璇诲彇褰撳墠閰嶇疆鏂囦欢 + with open(self.config_file, 'r', encoding='utf-8') as f: + config_data = json.load(f) + + # 鏇存柊w_id + config_data["ecloud"]["w_id"] = new_w_id + + # 鍐欏洖閰嶇疆鏂囦欢 + with open(self.config_file, 'w', encoding='utf-8') as f: + json.dump(config_data, f, indent=2, ensure_ascii=False) + + return True + + except Exception as e: + logger.error(f"鏇存柊閰嶇疆鏂囦欢w_id澶辫触: {str(e)}") + return False + + def get_current_w_id(self) -> str: + """ + 鑾峰彇褰撳墠鐨剋_id + + Returns: + 褰撳墠鐨剋_id鍊� + """ + with self._lock: + return self.ecloud_w_id # 鍏ㄥ眬閰嶇疆瀹炰緥 -- Gitblit v1.9.1