Explorar el Código

fix: 修改bug添加白名单查询

余尚辉 hace 2 semanas
padre
commit
72ce039d3e
Se han modificado 2 ficheros con 31 adiciones y 15 borrados
  1. 30 14
      src/core/callcenter/esl/client.py
  2. 1 1
      src/core/callcenter/views.py

+ 30 - 14
src/core/callcenter/esl/client.py

@@ -477,17 +477,33 @@ class InboundClient:
 
 class OutboundClient:
 
-    def __init__(self, agent, logger):
+    def __init__(self, agent, logger,app):
         self.logger = logger
-        # self.whitelist = self.load_whitelist()
+        self.app = app
+        self.whitelist = []
+        self.update_whitelist()  # 初始化加载白名单
+
+        # 定时更新白名单
+        threading.Thread(target=self.refresh_whitelist, daemon=True).start()
+
         #threading.Thread(target=self.start, args=('0.0.0.0', 8084, agent, logger)).start()
         server_thread = threading.Thread(target=self.start, args=('0.0.0.0', 8084, agent, logger))
         server_thread.daemon = True  # 设置守护线程
         server_thread.start()
+
+    def update_whitelist(self):
+        with self.app.app_context():
+            phones = Whitelist.query.filter_by(del_flag=0).all()
+            self.whitelist = [phone.phone for phone in phones]
+            self.logger.info("Whitelist updated: %s", self.whitelist)
+
+    def refresh_whitelist(self):
+        while True:
+            time.sleep(3600) # 每 1小时 更新一次
+            self.update_whitelist()
+
     def load_whitelist(self):
-        phones = Whitelist.query.filter_by(del_flag=0).all()
-        phone_list = [phone.phone for phone in phones]
-        return phone_list
+        return self.whitelist
 
     class ESLRequestHandler(socketserver.BaseRequestHandler):
         def setup(self):
@@ -502,14 +518,13 @@ class OutboundClient:
                     # print(json.loads(info.serialize('json')))
                     event_name = info.getHeader("Event-Name")
                     device_id = info.getHeader("unique-id")
-                    # caller_number = info.getHeader("Caller-Caller-ID-Number")  # 获取来电号码
-
+                    caller_number = info.getHeader("Caller-Caller-ID-Number")  # 获取来电号码
+                    whitelist = self.server.load_whitelist()
                      # 检查白名单
-                    # if caller_number in self.server.whitelist:
-                    #     self.server.logger.info("Caller %s is in whitelist, directly transferring call.", caller_number)
-                    #     # 直接转接到指定用户
-                    #     print('come in whitelist')
-                    #     return
+                    if caller_number in whitelist:
+                        # 直接转接到人工坐席
+                        self.server.logger.info("Caller %s is in whitelist, directly transferring call.", caller_number)
+                        return
                     
                     kwargs = json.loads(info.serialize('json'))
                     destination = self.server.agent.register(**kwargs)
@@ -541,14 +556,15 @@ class OutboundClient:
                 traceback.print_exc()
 
     class CustomTCPServer(socketserver.TCPServer):
-        def __init__(self, server_address, RequestHandlerClass, agent, logger):
+        def __init__(self, server_address, RequestHandlerClass, agent, logger,whitelist_loader):
             self.agent = agent
             self.logger = logger
+            self.load_whitelist = whitelist_loader
             super().__init__(server_address, RequestHandlerClass)
 
     def start(self, HOST='0.0.0.0', PORT=8084, agent=None, logger=None):
         # HOST, PORT = "0.0.0.0", 8084
         # 创建一个 TCP 服务器
-        with self.CustomTCPServer((HOST, PORT), self.ESLRequestHandler, agent, logger) as server:
+        with self.CustomTCPServer((HOST, PORT), self.ESLRequestHandler, agent, logger, self.load_whitelist) as server:
             self.logger.info(f"ESL server listening on {HOST}:{PORT}")
             server.serve_forever()

+ 1 - 1
src/core/callcenter/views.py

@@ -16,7 +16,7 @@ from src.core.voip.bot import BotAgent
 
 agent = BotAgent(app.logger)
 inbound_client = InboundClient(agent, app.logger)
-outbound_client = OutboundClient(agent, app.logger)
+outbound_client = OutboundClient(agent, app.logger,app)
 call_service = CallService(inbound_client, app.logger)
 agent_service = AgentService(inbound_client, app.logger)
 agent_oper_service = AgentOperService(inbound_client, app.logger)