Quellcode durchsuchen

新增ai白名单

Davidliu vor 2 Monaten
Ursprung
Commit
b59a40e1f2
3 geänderte Dateien mit 54 neuen und 25 gelöschten Zeilen
  1. 2 0
      src/core/callcenter/dao.py
  2. 12 0
      src/core/callcenter/enumeration.py
  3. 40 25
      src/core/callcenter/esl/client.py

+ 2 - 0
src/core/callcenter/dao.py

@@ -412,6 +412,7 @@ class Whitelist(db.Model):
     id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键')
     phone = db.Column(db.String(20), nullable=False, comment='电话号码')
     description = db.Column(db.String(255), nullable=True, comment='描述说明(备注)')
+    type = db.Column(db.SmallInteger, nullable=False, default='0', comment='类型(0人工白名单  1机器人白名单)')
     del_flag = db.Column(db.SmallInteger, nullable=False, default=False, comment='删除标志(0代表存在 2代表删除)')
     revision = db.Column(db.Integer, nullable=True, comment='乐观锁')
     create_by = db.Column(db.String(32), nullable=True, comment='创建人')
@@ -425,6 +426,7 @@ class Whitelist(db.Model):
             'id': self.id,
             'phone': self.phone,
             'description': self.description,
+            'type': self.type,
             'del_flag': self.del_flag,
             'revision': self.revision,
             'create_by': self.create_by,

+ 12 - 0
src/core/callcenter/enumeration.py

@@ -3,6 +3,18 @@
 
 from enum import Enum
 
+class WhiteTypeEnum(Enum):
+    DEFAULT = (0, "传统")
+    AI = (1, "AI")
+
+    def __init__(self, code=None, description=None):
+        self.code = code
+        self.description = description
+
+    @classmethod
+    def get_by_code(cls, code):
+        return next((member for member in cls if member.code == code), None)
+
 
 class DelayActionEnum(Enum):
     CALL_TIMEOUT_HANGUP = ('CALL_TIMEOUT_HANGUP', "超时挂机")

+ 40 - 25
src/core/callcenter/esl/client.py

@@ -27,7 +27,7 @@ import src.core.callcenter.esl.utils.esl_event_util as EslEventUtil
 import src.core.callcenter.esl.handler as event_handler
 from src.core.callcenter.esl.constant.sip_header_constant import sipHeaderHoldMusic, profile1, profile2, sipHeaderCallId
 from src.core.callcenter.enumeration import CallCause, DeviceType, DelayActionEnum, HangupDir, CallType, NextType, \
-    Direction, CdrType, BizErrorCode
+    Direction, CdrType, BizErrorCode, WhiteTypeEnum
 from src.core.callcenter.esl.handler.default_esl_event_handler import DefaultEslEventHandler
 from src.core.callcenter.snowflake import Snowflake
 from src.core.datasource import SERVE_HOST
@@ -537,28 +537,28 @@ class OutboundClient:
                     service_category = 0
                     bucket = self.server.get_bucket(call_id)
                     whitelist = self.server.get_whitelist()
-                    self.server.logger.info('call incoming call_id=%s, caller_number=%s, device_id=%s, new_device_id=%s, bucket=%s', call_id, caller_number, device_id, new_device_id, bucket.name)
-
-                    if self.in_whitelist(caller_number, whitelist):
+                    _in_whitelist = self.in_whitelist(caller_number, whitelist)
+                    self.server.logger.info('call incoming call_id=%s, caller_number=%s, device_id=%s, new_device_id=%s, in_whitelist=%s, bucket=%s', call_id, caller_number, device_id, new_device_id, _in_whitelist, bucket.name)
+                    if _in_whitelist:
                         # 检查白名单
-                        _bucket_call_type = 0
-                        self.answer(con, call_id, device_id)
-                        self.build_call_info(CallType.INCOMING_AGENT_CALL.code, call_id, device_id, new_device_id, destination=None, bucket_type=_bucket_call_type, **kwargs)
-                        self.server.agent.acd_service.transfer_to_agent(call_id, device_id)
-                    elif bucket and bucket.name == 'AI':
-                        #转到ai机器人
-                        _bucket_call_type = 1
-                        service_category = 1
-                        destination = self.server.agent.register(**kwargs)
-                        self.server.logger.info("device_id=%s, destination=%s, new_device_id=%s" % (device_id, destination, new_device_id))
-                        self.build_call_info(CallType.INCOMING_BOT_CALL.code, call_id, device_id, new_device_id, str(destination), bucket_type=_bucket_call_type, **kwargs)
-                        self.server.cache.add_device_user_part(device_id, destination)
-                        con.execute("bridge", "{sip_h_P-LIBRA-CallId=%s,sip_h_P-LIBRA-DeviceId=%s,origination_uuid=%s}user/%s" % (call_id, new_device_id, new_device_id, destination), device_id)
+                        if WhiteTypeEnum.AI == _in_whitelist:
+                            # 转到ai机器人
+                            _bucket_call_type = 0
+                            service_category = 1
+                            destination = self.bridge_ai(con, _bucket_call_type, call_id, device_id, new_device_id, **kwargs)
+                        else:
+                            _bucket_call_type = 0
+                            self.transfer_custom(con, _bucket_call_type, call_id, device_id, new_device_id, **kwargs)
                     else:
-                        # 传统服务
-                        self.answer(con, call_id, device_id)
-                        self.build_call_info(CallType.INCOMING_AGENT_CALL.code, call_id, device_id, new_device_id, destination=None, bucket_type=_bucket_call_type,  **kwargs)
-                        self.server.agent.acd_service.transfer_to_agent(call_id, device_id)
+                        # 自然分流
+                        if bucket and bucket.name == 'AI':
+                            #转到ai机器人
+                            _bucket_call_type = 1
+                            service_category = 1
+                            destination = self.bridge_ai(con, _bucket_call_type, call_id,  device_id, new_device_id, **kwargs)
+                        else:
+                            # 传统服务
+                            self.transfer_custom(con, _bucket_call_type, call_id, device_id, new_device_id, **kwargs)
 
                     registry.CALL_INCOMING_REQUESTS.labels(f"{_bucket_call_type}").inc()
                     self.server.dataHandleServer.create_record(call_id, caller_number, _bucket_call_type, service_category=service_category, user_id=destination if _bucket_call_type == 1 else None , user_name=  f"机器人{destination}" if _bucket_call_type ==1 else None)
@@ -580,11 +580,26 @@ class OutboundClient:
                     # Ignore the error if socket is already closed
                     pass
 
+        def transfer_custom(self, con, _bucket_call_type, call_id, device_id, new_device_id, kwargs):
+            self.answer(con, call_id, device_id)
+            self.build_call_info(CallType.INCOMING_AGENT_CALL.code, call_id, device_id, new_device_id, destination=None, bucket_type=_bucket_call_type, **kwargs)
+            self.server.agent.acd_service.transfer_to_agent(call_id, device_id)
+
+        def bridge_ai(self, con, _bucket_call_type, call_id, device_id, new_device_id, kwargs):
+            destination = self.server.agent.register(**kwargs)
+            self.server.logger.info("call_id=%s, device_id=%s, destination=%s, new_device_id=%s" % (call_id, device_id, destination, new_device_id))
+            self.build_call_info(CallType.INCOMING_BOT_CALL.code, call_id, device_id, new_device_id, str(destination), bucket_type=_bucket_call_type, **kwargs)
+            self.server.cache.add_device_user_part(device_id, destination)
+            con.execute("bridge", "{sip_h_P-LIBRA-CallId=%s,sip_h_P-LIBRA-DeviceId=%s,origination_uuid=%s}user/%s" % (call_id, new_device_id, new_device_id, destination), device_id)
+            return destination
+
         def in_whitelist(self, caller_number, whitelist):
             for x in whitelist:
-                if caller_number in x or caller_number in x:
-                    return True
-            return False
+                phone, _type = x
+                if caller_number in phone or phone in caller_number:
+                    return WhiteTypeEnum.get_by_code(_type)
+
+            return None
 
         def answer(self, con, call_id, device_id, timeouts=30):
             con.execute("answer", "", device_id)
@@ -652,7 +667,7 @@ class OutboundClient:
         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.whitelist = [(phone.phone, phone.type) for phone in phones]
                 self.logger.info("Whitelist updated: %s", self.whitelist)
 
         def get_whitelist(self):