BaseHandler.cs 10 KB

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