BaseHandler.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. namespace CallCenter.Application.Handlers
  13. {
  14. public class BaseHandler
  15. {
  16. private readonly INewRockClient _newRockClient;
  17. private readonly IOptionsSnapshot<DeviceConfigs> _options;
  18. private readonly ICallCacheManager _callCacheManager;
  19. private readonly ICallRepository _callRepository;
  20. public BaseHandler(INewRockClient newRockClient, IOptionsSnapshot<DeviceConfigs> options,ICallCacheManager callCacheManager, ICallRepository callRepository)
  21. {
  22. _newRockClient = newRockClient;
  23. _options = options;
  24. _callCacheManager = callCacheManager;
  25. _callRepository = callRepository;
  26. }
  27. public async Task HandlerIvr(IvrAnswer? runResult,Call model,CancellationToken cancellationToken)
  28. {
  29. if (runResult != null)
  30. {
  31. var ivrAnswer = runResult;
  32. switch (ivrAnswer.IvrAnswerType)
  33. {
  34. case EIvrAnswerType.Voice:
  35. var tomenuId = ivrAnswer.Content;
  36. switch (model?.CallType)
  37. {
  38. case ECallType.ExtToOuter:
  39. await _newRockClient.OuterToMenu(new OuterToMenuRequest()
  40. {
  41. Attribute = "Connect",
  42. Menu = new OuterToMenuMenu() { Id = tomenuId },
  43. Outer = new OuterToMenuOuter() { Id = model.ConversationId },
  44. //VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  45. }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  46. break;
  47. case ECallType.VisitorCallIn:
  48. await _newRockClient.VisitorToMenu(new VisitorToMenuRequest()
  49. {
  50. Attribute = "Connect",
  51. Menu = new VisitorToMenuMenu() { Id = tomenuId },
  52. Visitor = new VisitorToMenuVisitor() { Id = model.ConversationId },
  53. //VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  54. }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  55. break;
  56. default:
  57. throw new ArgumentOutOfRangeException();
  58. }
  59. break;
  60. case EIvrAnswerType.Tel:
  61. var telNo = ivrAnswer.Content;
  62. switch (model.CallType)
  63. {
  64. case ECallType.VisitorCallIn:
  65. //await _newRockClient.VisitorToExt(new VisitorToExtRequest()
  66. //{
  67. // Attribute = "Connect",
  68. // Ext = new VisitorToExtExt() { Id = telNo },
  69. // VoiceFile = string.IsNullOrEmpty(ivrAnswer.PreVoice) ? "" : ivrAnswer.PreVoice,
  70. // AutoAnswer = "",
  71. //}, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  72. //来电去队列
  73. await _newRockClient.VisitorToExtQueue(new VisitorToExtQueueRequest()
  74. {
  75. Attribute = "Queue",
  76. Ext = new VisitorToExtQueueExt() { Id = telNo },
  77. Visitor = new VisitorToExtQueueVisitor() { Id = model.ConversationId }
  78. }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  79. break;
  80. case ECallType.ExtToOuter:
  81. await _newRockClient.OuterToExt(new OuterToExtRequest()
  82. {
  83. Attribute = "Connect",
  84. Ext = new OuterToExtExt() { Id = telNo },
  85. Outer = new OuterToExtOuter() { Id = model.ConversationId }
  86. }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  87. break;
  88. default:
  89. throw new ArgumentOutOfRangeException();
  90. }
  91. break;
  92. case EIvrAnswerType.TelGroup:
  93. var groupId = ivrAnswer.Content;
  94. if (model.InIvrTime!=null)
  95. {
  96. model.OutIvrTime = DateTime.Now;
  97. }
  98. model.InGroupTime = DateTime.Now;
  99. await _callRepository.UpdateAsync(model,cancellationToken);
  100. await _newRockClient.VisitorToGroupQueue(new VisitorToGroupQueueRequest()
  101. {
  102. Attribute = "Queue",
  103. Group = new VisitorToGroupQueueGroup() { Id = groupId },
  104. Visitor = new VisitorToGroupQueueVisitor() { Id = model.ConversationId }
  105. }, _options.Value.ReceiveKey, _options.Value.Expired, cancellationToken);
  106. //处理队列记录 TODO
  107. //_callCacheManager.AddCallCache(model);
  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.ReceiveKey, _options.Value.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.ReceiveKey, _options.Value.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.ReceiveKey, _options.Value.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.ReceiveKey, _options.Value.Expired, cancellationToken);
  157. break;
  158. }
  159. break;
  160. default:
  161. throw new ArgumentOutOfRangeException();
  162. }
  163. }
  164. }
  165. }
  166. }