From 69945b730fd3f6b6138ce50e49fc3392fcd74d71 Mon Sep 17 00:00:00 2001
From: yj <2077506045@qq.com>
Date: 星期一, 28 七月 2025 18:16:52 +0800
Subject: [PATCH] 新增关键词过滤;新增活跃客服统计;新增掉线通知

---
 app/services/ecloud_client.py |   82 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/app/services/ecloud_client.py b/app/services/ecloud_client.py
index 42495c3..b8a64ef 100644
--- a/app/services/ecloud_client.py
+++ b/app/services/ecloud_client.py
@@ -20,6 +20,27 @@
         self.session = requests.Session()
         self.session.headers.update(self.headers)
 
+    def _filter_keywords(self, content: str) -> str:
+        """
+        杩囨护娑堟伅鍐呭涓殑鍏抽敭璇�
+
+        Args:
+            content: 鍘熷娑堟伅鍐呭
+
+        Returns:
+            杩囨护鍚庣殑娑堟伅鍐呭
+        """
+        if not settings.keyword_filter_enabled or not settings.keyword_filter_keywords:
+            return content
+
+        filtered_content = content
+        for keyword in settings.keyword_filter_keywords:
+            if keyword in filtered_content:
+                filtered_content = filtered_content.replace(keyword, "")
+                logger.info(f"杩囨护鍏抽敭璇�: {keyword}")
+
+        return filtered_content
+
     def get_contact_info(self, w_id: str, wc_id: str) -> Optional[Dict[str, Any]]:
         """
         鑾峰彇鑱旂郴浜轰俊鎭�
@@ -84,18 +105,21 @@
         Returns:
             鍙戦�佹垚鍔熻繑鍥濼rue锛屽け璐ヨ繑鍥濬alse
         """
+        # 杩囨护鍏抽敭璇�
+        filtered_content = self._filter_keywords(content)
+
         if max_retries is None:
             from config import settings
             max_retries = settings.max_retry_count
-            
+
         retry_count = 0
         while retry_count <= max_retries:
             try:
                 url = f"{self.base_url}/sendText"
-                payload = {"wId": w_id, "wcId": wc_id, "content": content}
+                payload = {"wId": w_id, "wcId": wc_id, "content": filtered_content}
 
                 logger.info(
-                    f"鍙戦�佹枃鏈秷鎭�: wId={w_id}, wcId={wc_id}, content_length={len(content)}, retry={retry_count}"
+                    f"鍙戦�佹枃鏈秷鎭�: wId={w_id}, wcId={wc_id}, content_length={len(filtered_content)}, retry={retry_count}"
                 )
 
                 response = self.session.post(url, json=payload, timeout=30)
@@ -141,7 +165,9 @@
         Returns:
             鍙戦�佹垚鍔熻繑鍥濼rue锛屽け璐ヨ繑鍥濬alse
         """
-        return self.send_text_message(w_id, group_id, content)
+        # 杩囨护鍏抽敭璇�
+        filtered_content = self._filter_keywords(content)
+        return self.send_text_message(w_id, group_id, filtered_content)
 
     def init_address_list(self, w_id: str) -> bool:
         """
@@ -238,6 +264,9 @@
         Returns:
             鍙戦�佹垚鍔熻繑鍥濼rue锛屽け璐ヨ繑鍥濬alse
         """
+        # 杩囨护鍏抽敭璇�
+        filtered_content = self._filter_keywords(content)
+
         if max_retries is None:
             from config import settings
             max_retries = settings.max_retry_count
@@ -251,12 +280,12 @@
                 payload = {
                     "wId": w_id,
                     "wcId": wc_id,
-                    "content": content,
+                    "content": filtered_content,
                     "at": at_str
                 }
 
                 logger.info(
-                    f"鍙戦�佺兢鑱夽娑堟伅: wId={w_id}, wcId={wc_id}, at={at_str}, content_length={len(content)}, retry={retry_count}"
+                    f"鍙戦�佺兢鑱夽娑堟伅: wId={w_id}, wcId={wc_id}, at={at_str}, content_length={len(filtered_content)}, retry={retry_count}"
                 )
 
                 response = self.session.post(url, json=payload, timeout=30)
@@ -290,6 +319,47 @@
         )
         return False
 
+    def query_online_wechat_list(self) -> Optional[List[Dict[str, str]]]:
+        """
+        鏌ヨ璐﹀彿涓湪绾跨殑寰俊鍒楄〃
+
+        Returns:
+            鍦ㄧ嚎寰俊鍒楄〃锛屾瘡涓厓绱犲寘鍚玾cId鍜寃Id锛屽け璐ヨ繑鍥濶one
+            杩斿洖鏍煎紡: [
+                {
+                    "wcId": "wxid_i6qsbbjenju2",
+                    "wId": "72223018-7f2a-4f4f-bfa3-26e47dbd61"
+                }
+            ]
+        """
+        try:
+            url = f"{self.base_url}/queryLoginWx"
+            payload = {}
+
+            logger.info("鏌ヨ鍦ㄧ嚎寰俊鍒楄〃")
+
+            response = self.session.post(url, json=payload, timeout=30)
+            response.raise_for_status()
+
+            result = response.json()
+
+            if result.get("code") == "1000":
+                online_list = result.get("data", [])
+                logger.info(f"鎴愬姛鏌ヨ鍦ㄧ嚎寰俊鍒楄〃: count={len(online_list)}")
+                return online_list
+            else:
+                logger.warning(
+                    f"鏌ヨ鍦ㄧ嚎寰俊鍒楄〃澶辫触: code={result.get('code')}, message={result.get('message')}"
+                )
+                return []
+
+        except requests.exceptions.RequestException as e:
+            logger.error(f"鏌ヨ鍦ㄧ嚎寰俊鍒楄〃缃戠粶閿欒: error={str(e)}")
+            return None
+        except Exception as e:
+            logger.error(f"鏌ヨ鍦ㄧ嚎寰俊鍒楄〃寮傚父: error={str(e)}")
+            return None
+
 
 # 鍏ㄥ眬E浜戠瀹跺鎴风瀹炰緥
 ecloud_client = ECloudClient()

--
Gitblit v1.9.1