774056846 5 місяців тому
батько
коміт
7e15b0f7b4

+ 52 - 2
src/core/callcenter/__init__.py

@@ -1,10 +1,12 @@
 #!/usr/bin/env python3
 # encoding:utf-8
 
+import traceback
 from .config import BaseConfig
 from flask import Flask, request, render_template_string
 from flask_sqlalchemy import SQLAlchemy
 from flask_cors import CORS
+from .constant import error_response
 db = SQLAlchemy()
 
 
@@ -18,7 +20,55 @@ def create_app():
     # with app.app_context():
     #     from . import views  # Import routes
 
-    return _app
+    from src.core.voip.bot import BotAgent
+    from .call import CallService
+    from .agent import AgentService, AgentOperService
+    from .esl.client import InboundClient, OutboundClient
 
+    agent = BotAgent(_app.logger)
+    # agent, _outbound_client = None, None
+    _inbound_client = InboundClient(agent, _app.logger)
+    _outbound_client = OutboundClient(agent, _app.logger)
+    _call_service = CallService(_inbound_client, _app.logger)
+    _agent_service = AgentService(_inbound_client, _app.logger)
+    _agent_oper_service = AgentOperService(_inbound_client, _app.logger)
+    return _app, _inbound_client, _outbound_client, _call_service , _agent_service, _agent_oper_service
 
-app = create_app()
+
+app, inbound_client, outbound_client, call_service , agent_service, agent_oper_service = create_app()
+
+
+class BizException(Exception):
+    def __init__(self, message, status_code=400):
+        super().__init__(message)
+        self.message = message
+        self.status_code = status_code
+
+
+class SipUriSyntaxException(Exception):
+    def __init__(self, message, status_code=400):
+        super().__init__(message)
+        self.message = message
+        self.status_code = status_code
+
+
+@app.errorhandler(BizException)
+def handle_biz_exception(error):
+    traceback.print_exc()
+    return error_response(msg=str(error), http_code=200)
+
+# Generic error handler for uncaught exceptions
+@app.errorhandler(Exception)
+def handle_generic_exception(error):
+    traceback.print_exc()
+    return error_response(msg=str(error), http_code=500)
+
+# Specific error handler for 404 Not Found errors
+@app.errorhandler(404)
+def handle_404_error(error):
+    return error_response(msg="Resource not found", http_code=404)
+
+# Specific error handler for 400 Bad Request errors
+@app.errorhandler(400)
+def handle_400_error(error):
+    return error_response(msg="Bad Request", http_code=400)

+ 4 - 3
src/core/callcenter/api.py

@@ -5,7 +5,6 @@ import json
 from typing import Dict, Any, Optional, List
 
 from src.core.callcenter.constant import get_json_dict
-from src.core.callcenter.exception import SipUriSyntaxException
 from src.core.datasource import SIP_SERVER
 from src.core.callcenter.esl.constant.esl_constant import SPLIT, SIP_HEADER
 from src.core.callcenter.enumeration import CallType, DeviceType, Direction, CdrType, NextType
@@ -308,13 +307,15 @@ class SipURI:
 
         scheme = f"{self.SIP_SCHEME}{self.SCHEME_SEPARATOR}"
         if not sip_uri.startswith(scheme):
-            raise SipUriSyntaxException(f"SIP URI must start with {scheme}")
+            # raise SipUriSyntaxException(f"SIP URI must start with {scheme}")
+            return
 
         buf = sip_uri[len(scheme):]
         at_pos = buf.find("@")
 
         if at_pos == 0:
-            raise SipUriSyntaxException("userinfo cannot start with a '@'")
+            # raise SipUriSyntaxException("userinfo cannot start with a '@'")
+            return
         if at_pos > 0:
             self.userinfo = buf[:at_pos]
             buf = buf[at_pos + 1:]

+ 42 - 42
src/core/callcenter/exception.py

@@ -1,42 +1,42 @@
-#!/usr/bin/env python3
-# encoding:utf-8
-
-import traceback
-from . import app
-from .constant import error_response
-
-
-class BizException(Exception):
-    def __init__(self, message, status_code=400):
-        super().__init__(message)
-        self.message = message
-        self.status_code = status_code
-
-
-class SipUriSyntaxException(Exception):
-    def __init__(self, message, status_code=400):
-        super().__init__(message)
-        self.message = message
-        self.status_code = status_code
-
-
-@app.errorhandler(BizException)
-def handle_biz_exception(error):
-    traceback.print_exc()
-    return error_response(msg=str(error), http_code=200)
-
-# Generic error handler for uncaught exceptions
-@app.errorhandler(Exception)
-def handle_generic_exception(error):
-    traceback.print_exc()
-    return error_response(msg=str(error), http_code=500)
-
-# Specific error handler for 404 Not Found errors
-@app.errorhandler(404)
-def handle_404_error(error):
-    return error_response(msg="Resource not found", http_code=404)
-
-# Specific error handler for 400 Bad Request errors
-@app.errorhandler(400)
-def handle_400_error(error):
-    return error_response(msg="Bad Request", http_code=400)
+# #!/usr/bin/env python3
+# # encoding:utf-8
+#
+# import traceback
+# from . import app
+# from .constant import error_response
+#
+#
+# class BizException(Exception):
+#     def __init__(self, message, status_code=400):
+#         super().__init__(message)
+#         self.message = message
+#         self.status_code = status_code
+#
+#
+# class SipUriSyntaxException(Exception):
+#     def __init__(self, message, status_code=400):
+#         super().__init__(message)
+#         self.message = message
+#         self.status_code = status_code
+#
+#
+# @app.errorhandler(BizException)
+# def handle_biz_exception(error):
+#     traceback.print_exc()
+#     return error_response(msg=str(error), http_code=200)
+#
+# # Generic error handler for uncaught exceptions
+# @app.errorhandler(Exception)
+# def handle_generic_exception(error):
+#     traceback.print_exc()
+#     return error_response(msg=str(error), http_code=500)
+#
+# # Specific error handler for 404 Not Found errors
+# @app.errorhandler(404)
+# def handle_404_error(error):
+#     return error_response(msg="Resource not found", http_code=404)
+#
+# # Specific error handler for 400 Bad Request errors
+# @app.errorhandler(400)
+# def handle_400_error(error):
+#     return error_response(msg="Bad Request", http_code=400)

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

@@ -1,32 +1,16 @@
 #!/usr/bin/env python3
 # encoding:utf-8
 
-import os
-import json
-import threading
-from . import app
+from . import app, call_service , agent_service, agent_oper_service
 import src.core.callcenter.cache as Cache
-from src.core.callcenter import create_app
-from src.core.callcenter.agent import AgentService, AgentOperService
 from src.core.callcenter.constant import success_response, error_response
 from src.core.callcenter.enumeration import CallType
-from src.core.callcenter.esl.client import InboundClient, OutboundClient
 from flask import Flask, request, render_template_string
 
-from src.core.callcenter.call import CallService
 from src.core.callcenter.api import AgentCallRequest, AgentActionRequest, HangupCallRequest, \
     HumanServiceQueryRequest
-from src.core.voip.bot import BotAgent
 
 
-agent = BotAgent(app.logger)
-# agent = None
-inbound_client = InboundClient(agent, app.logger)
-outbound_client = OutboundClient(agent, app.logger)
-call_service = CallService(inbound_client, app.logger)
-agent_service = AgentService(inbound_client, app.logger)
-agent_oper_service = AgentOperService(inbound_client, app.logger)
-
 
 @app.route('/')
 def index():