call.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  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. class CallService:
  13. def __init__(self, client, logger):
  14. self.client = client
  15. self.logger = logger
  16. self.snowflake = Snowflake()
  17. def call(self, request: AgentCallRequest):
  18. call_id = 'C' + str(self.snowflake.next_id())
  19. device_id = 'D' + str(self.snowflake.next_id())
  20. # now = lambda: int(round(time.time() * 1000))
  21. now = datetime.utcnow().timestamp()
  22. agent = Cache.get_agent_info(request.saas_id, request.agent_id)
  23. route_gateway = Cache.get_route_gateway(request.saas_id)
  24. call_info = CallInfo(cti_flow_id=request.cti_flow_id, call_id=call_id, agent_key=agent.agent_number, sip_server=agent.sip_server,
  25. caller=agent.agent_number, called=request.called, direction=Direction.INBOUND.code,
  26. caller_display=request.caller_display, called_display=request.called_display,
  27. call_type=request.call_type.code, call_time=now, follow_data=request.follow_data,
  28. uuid1=request.uuid1, uuid2=request.uuid2, saas_id=saasId)
  29. 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,
  30. agent_key=agent.agent_number, caller_display=route_gateway.name, cdr_type=CdrType.INBOUND.code)
  31. call_info.device_list.append(device_id)
  32. call_info.next_commands.append(NextCommand(device_id, NextType.NEXT_CALL_OTHER.code))
  33. call_info.device_info_map = {device_id: device_info}
  34. Cache.add_call_info(call_info)
  35. context = MakeCallContext(display=request.called, caller=request.called, called=request.caller,
  36. call_id=call_id, device_id=device_id, device_type=device_info.device_type,
  37. call_type=call_info.call_type, sip_server=call_info.sip_server,
  38. sip_header_map={sipHeaderCtiFlowId: request.cti_flow_id})
  39. self.client.make_call_new(context)
  40. return call_id
  41. def hold(self, call_info: CallInfo, device_id):
  42. devices = call_info.device_list
  43. try:
  44. devices.remove(device_id)
  45. except:
  46. pass
  47. custom_device_id = devices[0]
  48. print('debugger::hold, custom_device_id=%s'%custom_device_id, flush=True)
  49. # self.client.sync_invoke_method("bridge_break", method_args=(custom_device_id,))
  50. # self.client.sync_invoke_method("hold_play", method_args=(custom_device_id,HOLD_MUSIC_PATH))
  51. self.client.bridge_break(call_info.call_id, custom_device_id)
  52. Cache.set_need_play_hold_music(call_info.call_id)
  53. print('debugger::hold success custom_device_id=%s'%custom_device_id, flush=True)
  54. def cancel_hold(self, call_info: CallInfo, device_id):
  55. self.client.bridge_call(call_info.call_id, call_info.device_list[0], call_info.device_list[1])
  56. def transfer(self, call_info: CallInfo, agent_number, service_id):
  57. caller = call_info.called
  58. call_id = call_info.call_id
  59. agent = Cache.get_agent_info(call_info.saas_id, call_info.agent_key)
  60. device_id = 'D' + str(self.snowflake.next_id())
  61. # now = lambda: int(round(time.time() * 1000))
  62. now = datetime.utcnow().timestamp()
  63. device_info = DeviceInfo(device_id=device_id, caller=caller, display=caller, called=agent_number, call_id=call_id,
  64. call_time=now, cdr_type=CdrType.TRANSFER.code, device_type=DeviceType.AGENT.code)
  65. call_info.device_list.append(device_id)
  66. call_info.caller = agent_number
  67. call_info.device_info_map[device_id] = device_info
  68. call_info.next_commands.append(NextCommand(device_info.device_id, NextType.NEXT_TRANSFER_CALL.code, call_info.device_list[0]))
  69. call_info.agent_key = agent_number
  70. # agent.sip_server
  71. Cache.add_call_info(call_info)
  72. Cache.add_agent_info(agent=agent, call_id=call_id, device_id=device_id)
  73. sip_header_map = {sipHeaderServiceId: service_id}
  74. context = MakeCallContext(display=call_info.called, caller=call_info.called, called=agent_number,
  75. call_id=call_id, device_id=device_id, device_type=device_info.device_type,
  76. call_type=call_info.call_type, service_id=service_id, sip_header_map=sip_header_map)
  77. self.client.make_call_new(context)
  78. def hangup(self, request: HangupCallRequest):
  79. call_info = Cache.get_call_info(request.call_id)
  80. if not call_info:
  81. self.logger.info('hangup call not exist callId: %s', request.call_id)
  82. return
  83. devices = call_info.device_list
  84. if not devices:
  85. self.logger.info('hangup deviceList is null callId: %s', request.call_id)
  86. return
  87. self.hangup_all(call_info, CallCause.AGENT_HANGUP_CALL)
  88. def hangup_all(self, call_info: CallInfo, case_enum=CallCause.DEFAULT):
  89. devices = call_info.device_list
  90. if not devices:
  91. self.logger.info('hangupCallAll skip 已全部挂断 callId: %s', call_info.call_id)
  92. return
  93. for device in devices:
  94. self.client.hangup_call(call_info.call_id, device, case_enum)
  95. def hangup_call(self, call_id):
  96. call_info = Cache.get_call_info(call_id)
  97. if not call_info:
  98. self.logger.info('hangup call not exist callId: %s', call_id)
  99. return
  100. devices = call_info.device_list
  101. if not devices:
  102. self.logger.info('hangup deviceList is null callId: %s', call_id)
  103. return
  104. for device in devices:
  105. self.client.hangup_call(call_info.call_id, device, CallCause.RESTART)
  106. def checkin_call(self, request: CheckInCallRequest):
  107. agent = Cache.get_agent_info(request.saas_id, request.agent_number)
  108. return self.client.show_channel(agent.device_id)