call.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  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(worker_id=1, data_center_id=1)
  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(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(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)
  31. call_info.device_list.append(device_info)
  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. self.client.make_call_new(context)
  39. return call_id
  40. def hold(self, call_info: CallInfo, device_id):
  41. devices = call_info.device_list
  42. devices.remove(device_id)
  43. self.client.bridge_break(devices.get(0))
  44. self.client.hold_play(devices.get(0), HOLD_MUSIC_PATH)
  45. def cancel_hold(self, call_info: CallInfo, device_id):
  46. self.client.bridge_call(call_info.call_id, call_info.device_list[0], call_info.device_list[1])
  47. def transfer(self, call_info: CallInfo, agent_number, service_id):
  48. caller = call_info.called
  49. call_id = call_info.call_id
  50. agent = Cache.get_agent_info(call_info.saas_id, call_info.agent_key)
  51. device_id = 'D' + str(self.snowflake.next_id())
  52. # now = lambda: int(round(time.time() * 1000))
  53. now = datetime.utcnow().timestamp()
  54. device_info = DeviceInfo(device_id=device_id, caller=caller, display=caller, called=agent_number, call_id=call_id,
  55. call_time=now, cdr_type=CdrType.TRANSFER.code, device_type=DeviceType.AGENT.code)
  56. call_info.device_list.append(device_id)
  57. call_info.caller = agent_number
  58. call_info.device_info_map[device_id] = device_info
  59. call_info.next_commands.append(NextCommand(device_info.device_id, NextType.NEXT_TRANSFER_CALL.code, call_info.device_list[0]))
  60. call_info.agent_key = agent_number
  61. # agent.sip_server
  62. Cache.add_call_info(call_info)
  63. Cache.add_agent_info(agent=agent, call_id=call_id, device_id=device_id)
  64. sip_header_map = {sipHeaderServiceId: service_id}
  65. context = MakeCallContext(display=call_info.called, caller=call_info.called, called=agent_number,
  66. call_id=call_id, device_id=device_id, device_type=device_info.device_type,
  67. call_type=call_info.call_type, service_id=service_id, sip_header_map=sip_header_map)
  68. self.inbound_client.make_call_new(context)
  69. def hangup(self, request: HangupCallRequest):
  70. call_info = Cache.get_call_info(request.call_id)
  71. if not call_info:
  72. self.logger.info('hangup call not exist callId: %s', request.call_id)
  73. return
  74. devices = call_info.device_list
  75. if not devices:
  76. self.logger.info('hangup deviceList is null callId: %s', request.call_id)
  77. return
  78. self.hangup_all(call_info, CallCause.AGENT_HANGUP_CALL)
  79. def hangup_all(self, call_info: CallInfo, case_enum=CallCause.DEFAULT):
  80. devices = call_info.device_list
  81. if not devices:
  82. self.logger.info('hangupCallAll skip 已全部挂断 callId: %s', call_info.call_id)
  83. return
  84. for device in devices:
  85. self.client.hangup_call(call_info.call_id, device, case_enum)
  86. def hangup_call(self, call_id):
  87. call_info = Cache.get_call_info(call_id)
  88. if not call_info:
  89. self.logger.info('hangup call not exist callId: %s', call_id)
  90. return
  91. devices = call_info.device_list
  92. if not devices:
  93. self.logger.info('hangup deviceList is null callId: %s', call_id)
  94. return
  95. for device in devices:
  96. self.client.hangup_call(call_info.call_id, device, CallCause.RESTART)
  97. def checkin_call(self, request: CheckInCallRequest):
  98. agent = Cache.get_agent_info(request.saas_id, request.agent_number)
  99. return self.client.show_channel(agent.device_id)