call.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. import time
  4. from datetime import datetime
  5. import src.core.callcenter.cache as Cache
  6. from src.core.callcenter.constant import saasId, HOLD_MUSIC_PATH
  7. from src.core.callcenter.enumeration import CallCause, Direction, NextType, DeviceType, CdrType, AgentServiceState
  8. from src.core.callcenter.api import AgentCallRequest, CallInfo, HangupCallRequest, CheckInCallRequest, \
  9. DeviceInfo, NextCommand, MakeCallContext
  10. from src.core.callcenter.esl.constant.sip_header_constant import sipHeaderServiceId, sipHeaderCtiFlowId
  11. from src.core.callcenter.snowflake import Snowflake
  12. from src.core.callcenter.data_handler import *
  13. class CallService:
  14. def __init__(self, client, logger):
  15. self.client = client
  16. self.logger = logger
  17. self.snowflake = Snowflake()
  18. self.dataHandleServer=dataHandleServer(client.app)
  19. def call(self, request: AgentCallRequest):
  20. call_id = 'C' + str(self.snowflake.next_id())
  21. device_id = 'D' + str(self.snowflake.next_id())
  22. # now = lambda: int(round(time.time() * 1000))
  23. now = datetime.utcnow().timestamp()
  24. agent = Cache.get_agent_info(request.saas_id, request.agent_id)
  25. route_gateway = Cache.get_route_gateway(request.saas_id)
  26. call_info = CallInfo(cti_flow_id=request.cti_flow_id, call_id=call_id, agent_key=agent.agent_number, sip_server=agent.sip_server,
  27. caller=agent.agent_number, called=request.called, direction=Direction.INBOUND.code,
  28. caller_display=request.caller_display, called_display=request.called_display,
  29. call_type=request.call_type.code, call_time=now, follow_data=request.follow_data,
  30. uuid1=request.uuid1, uuid2=request.uuid2, saas_id=saasId)
  31. device_info = DeviceInfo(cti_flow_id=request.cti_flow_id, device_id=device_id, call_time=now, call_id=call_id, device_type=DeviceType.AGENT.code,
  32. agent_key=agent.agent_number, caller_display=route_gateway.name, cdr_type=CdrType.INBOUND.code)
  33. call_info.device_list.append(device_id)
  34. call_info.next_commands.append(NextCommand(device_id, NextType.NEXT_CALL_OTHER.code))
  35. call_info.device_info_map = {device_id: device_info}
  36. Cache.add_call_info(call_info)
  37. context = MakeCallContext(display=request.called, caller=request.called, called=request.caller,
  38. call_id=call_id, device_id=device_id, device_type=device_info.device_type,
  39. call_type=call_info.call_type, sip_server=call_info.sip_server,
  40. sip_header_map={sipHeaderCtiFlowId: request.cti_flow_id})
  41. self.client.make_call_new(context)
  42. # 创建一条通话记录
  43. self.dataHandleServer.create_record({
  44. "session_id": call_id,
  45. "time_begin": datetime.utcnow(),
  46. "category": 1,
  47. "agent_num":request.agent_id,
  48. "phone": request.called
  49. })
  50. # 变更坐席状态为拨号中
  51. self.dataHandleServer.update_agent_monitor_service_state(request.agent_id, AgentServiceState.DIALING.code)
  52. return call_id
  53. def hold(self, call_info: CallInfo, device_id):
  54. devices = call_info.device_list
  55. try:
  56. devices.remove(device_id)
  57. except:
  58. pass
  59. custom_device_id = devices[0]
  60. print('debugger::hold, custom_device_id=%s'%custom_device_id, flush=True)
  61. # self.client.sync_invoke_method("bridge_break", method_args=(custom_device_id,))
  62. # self.client.sync_invoke_method("hold_play", method_args=(custom_device_id,HOLD_MUSIC_PATH))
  63. self.client.bridge_break(call_info.call_id, custom_device_id)
  64. Cache.set_need_play_hold_music(call_info.call_id)
  65. print('debugger::hold success custom_device_id=%s'%custom_device_id, flush=True)
  66. def cancel_hold(self, call_info: CallInfo, device_id):
  67. self.client.bridge_call(call_info.call_id, call_info.device_list[0], call_info.device_list[1])
  68. def transfer(self, call_info: CallInfo, agent_number, service_id):
  69. caller = call_info.called
  70. call_id = call_info.call_id
  71. agent = Cache.get_agent_info(call_info.saas_id, call_info.agent_key)
  72. device_id = 'D' + str(self.snowflake.next_id())
  73. # now = lambda: int(round(time.time() * 1000))
  74. now = datetime.utcnow().timestamp()
  75. device_info = DeviceInfo(device_id=device_id, caller=caller, display=caller, called=agent_number, call_id=call_id,
  76. call_time=now, cdr_type=CdrType.TRANSFER.code, device_type=DeviceType.AGENT.code)
  77. call_info.device_list.append(device_id)
  78. call_info.caller = agent_number
  79. call_info.device_info_map[device_id] = device_info
  80. call_info.next_commands.append(NextCommand(device_info.device_id, NextType.NEXT_TRANSFER_CALL.code, call_info.device_list[0]))
  81. call_info.agent_key = agent_number
  82. # agent.sip_server
  83. Cache.add_call_info(call_info)
  84. Cache.add_agent_info(agent=agent, call_id=call_id, device_id=device_id)
  85. sip_header_map = {sipHeaderServiceId: service_id}
  86. context = MakeCallContext(display=call_info.called, caller=call_info.called, called=agent_number,
  87. call_id=call_id, device_id=device_id, device_type=device_info.device_type,
  88. call_type=call_info.call_type, service_id=service_id, sip_header_map=sip_header_map)
  89. self.client.make_call_new(context)
  90. def hangup(self, request: HangupCallRequest):
  91. call_info = Cache.get_call_info(request.call_id)
  92. if not call_info:
  93. self.logger.info('hangup call not exist callId: %s', request.call_id)
  94. return
  95. devices = call_info.device_list
  96. if not devices:
  97. self.logger.info('hangup deviceList is null callId: %s', request.call_id)
  98. return
  99. self.hangup_all(call_info, CallCause.AGENT_HANGUP_CALL)
  100. def hangup_all(self, call_info: CallInfo, case_enum=CallCause.DEFAULT):
  101. devices = call_info.device_list
  102. if not devices:
  103. self.logger.info('hangupCallAll skip 已全部挂断 callId: %s', call_info.call_id)
  104. return
  105. for device in devices:
  106. self.client.hangup_call(call_info.call_id, device, case_enum)
  107. def hangup_call(self, call_id):
  108. call_info = Cache.get_call_info(call_id)
  109. if not call_info:
  110. self.logger.info('hangup call not exist callId: %s', call_id)
  111. return
  112. devices = call_info.device_list
  113. if not devices:
  114. self.logger.info('hangup deviceList is null callId: %s', call_id)
  115. return
  116. for device in devices:
  117. self.client.hangup_call(call_info.call_id, device, CallCause.RESTART)
  118. def checkin_call(self, request: CheckInCallRequest):
  119. agent = Cache.get_agent_info(request.saas_id, request.agent_number)
  120. return self.client.show_channel(agent.device_id)