BaseHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.CallCenter.Calls;
  3. using Hotline.CallCenter.Devices;
  4. using Hotline.Realtimes;
  5. using Hotline.Share.Dtos.CallCenter;
  6. using Hotline.Share.Enums.CallCenter;
  7. using Microsoft.Extensions.Options;
  8. using NewRock.Sdk;
  9. using NewRock.Sdk.Control.Request;
  10. using NewRock.Sdk.Transfer.Connect.Request;
  11. using NewRock.Sdk.Transfer.Queue.Request;
  12. namespace Hotline.Application.Handlers.CallCenter
  13. {
  14. public class BaseHandler
  15. {
  16. private readonly INewRockClient _newRockClient;
  17. private readonly IOptionsSnapshot<CallCenterConfiguration> _options;
  18. private readonly ICallRepository _callRepository;
  19. private readonly ICallCacheManager _callCacheManager;
  20. private readonly IRealtimeService _realtimeService;
  21. public BaseHandler(INewRockClient newRockClient, IOptionsSnapshot<CallCenterConfiguration> options,ICallRepository callRepository,ICallCacheManager callCacheManager,IRealtimeService realtimeService)
  22. {
  23. _newRockClient = newRockClient;
  24. _options = options;
  25. _callRepository = callRepository;
  26. _callCacheManager = callCacheManager;
  27. _realtimeService = realtimeService;
  28. }
  29. public async Task HandlerIvr(IvrAnswer? runResult, Call model, CancellationToken cancellationToken)
  30. {
  31. if (runResult != null)
  32. {
  33. var ivrAnswer = runResult;
  34. switch (ivrAnswer.IvrAnswerType)
  35. {
  36. case EIvrAnswerType.Voice:
  37. var tomenuId = ivrAnswer.Content;
  38. switch (model?.CallType)
  39. {
  40. case ECallType.ExtToOuter:
  41. await _newRockClient.OuterToMenu(new OuterToMenuRequest()
  42. {
  43. Attribute = "Connect",
  44. Menu = new OuterToMenuMenu() { Id = tomenuId },
  45. Outer = new OuterToMenuOuter() { Id = model.ConversationId },
  46. //VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  47. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  48. break;
  49. case ECallType.VisitorCallIn:
  50. await _newRockClient.VisitorToMenu(new VisitorToMenuRequest()
  51. {
  52. Attribute = "Connect",
  53. Menu = new VisitorToMenuMenu() { Id = tomenuId },
  54. Visitor = new VisitorToMenuVisitor() { Id = model.ConversationId },
  55. //VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  56. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  57. break;
  58. default:
  59. throw new ArgumentOutOfRangeException();
  60. }
  61. break;
  62. case EIvrAnswerType.Tel:
  63. var telNo = ivrAnswer.Content;
  64. switch (model.CallType)
  65. {
  66. case ECallType.VisitorCallIn:
  67. //await _newRockClient.VisitorToExt(new VisitorToExtRequest()
  68. //{
  69. // Attribute = "Connect",
  70. // Ext = new VisitorToExtExt() { Id = telNo },
  71. // VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  72. // AutoAnswer = "",
  73. //}, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  74. //来电去队列
  75. await _newRockClient.VisitorToExtQueue(new VisitorToExtQueueRequest()
  76. {
  77. Attribute = "Queue",
  78. Ext = new VisitorToExtQueueExt() { Id = telNo },
  79. Visitor = new VisitorToExtQueueVisitor() { Id = model.ConversationId }
  80. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  81. break;
  82. case ECallType.ExtToOuter:
  83. await _newRockClient.OuterToExt(new OuterToExtRequest()
  84. {
  85. Attribute = "Connect",
  86. Ext = new OuterToExtExt() { Id = telNo },
  87. Outer = new OuterToExtOuter() { Id = model.ConversationId }
  88. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  89. break;
  90. default:
  91. throw new ArgumentOutOfRangeException();
  92. }
  93. break;
  94. case EIvrAnswerType.TelGroup:
  95. var groupId = ivrAnswer.Content;
  96. model.InQueueTime= DateTime.Now;
  97. await _callRepository.UpdateAsync(model);
  98. await _newRockClient.VisitorToGroupQueue(new VisitorToGroupQueueRequest()
  99. {
  100. Attribute = "Queue",
  101. Group = new VisitorToGroupQueueGroup() { Id = groupId },
  102. Visitor = new VisitorToGroupQueueVisitor() { Id = model.ConversationId }
  103. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  104. //处理队列记录
  105. _callCacheManager.AddCallCache(model);
  106. await _realtimeService.CallQueueAsync(_callCacheManager.GetCallQueueList(), cancellationToken);
  107. break;
  108. case EIvrAnswerType.Out:
  109. var phoneNo = ivrAnswer.Content;
  110. switch (model.CallType)
  111. {
  112. case ECallType.VisitorCallIn:
  113. await _newRockClient.VisitorToOuter(new VisitorToOuterRequest()
  114. {
  115. Attribute = "Connect",
  116. Visitor = new VisitorToOuterVisitor() { Id = model.ConversationId },
  117. Outer = new VisitorToOuterOuter()
  118. {
  119. To = phoneNo,
  120. //TODO DISPLAY属性待定
  121. }
  122. },
  123. _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  124. break;
  125. case ECallType.ExtToOuter:
  126. await _newRockClient.OuterToOuter(new OuterToOuterRequest()
  127. {
  128. Attribute = "Connect",
  129. Outer = new List<OuterToOuterOuterModel>(){
  130. new(){Id = model.ConversationId},
  131. new(){To = phoneNo},
  132. }
  133. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  134. break;
  135. default:
  136. throw new ArgumentOutOfRangeException();
  137. }
  138. break;
  139. case EIvrAnswerType.HangUp:
  140. var id = ivrAnswer.Content;
  141. switch (model?.CallType)
  142. {
  143. case ECallType.VisitorCallIn:
  144. await _newRockClient.ClearCall(new ClearCallRequest()
  145. {
  146. Attribute = "Clear",
  147. Visitor = new ClearCallVisitor() { Id = model.ConversationId },
  148. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  149. break;
  150. case ECallType.ExtToOuter:
  151. await _newRockClient.ClearCall(new ClearCallRequest()
  152. {
  153. Attribute = "Clear",
  154. Outer = new ClearCallOuter() { Id = model.ConversationId },
  155. }, _options.Value.DeviceConfigs.ReceiveKey, _options.Value.DeviceConfigs.Expired, cancellationToken);
  156. break;
  157. }
  158. break;
  159. default:
  160. throw new ArgumentOutOfRangeException();
  161. }
  162. }
  163. }
  164. }
  165. }