BaseHandler.cs 9.4 KB

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