MessageHandlerAsync.Message.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:MessageHandlerAsync.Message.cs
  17. 文件功能描述:微信请求【异步方法】的集中处理方法:Message相关
  18. 创建标识:Senparc - 20180122
  19. 修改标识:Senparc - 20180829
  20. 修改描述:v15.4.0 支持NeuChar,添加 OnNeuCharRequestAsync() 方法
  21. ----------------------------------------------------------------*/
  22. #if !NET35 && !NET40
  23. using System;
  24. using Senparc.Weixin.Exceptions;
  25. using Senparc.CO2NET.Extensions;
  26. using Senparc.Weixin.MP.Entities;
  27. using Senparc.Weixin.MP.Helpers;
  28. using System.Threading.Tasks;
  29. using System.Linq;
  30. using Senparc.NeuChar.Entities;
  31. using Senparc.NeuChar.Helpers;
  32. namespace Senparc.Weixin.MP.MessageHandlers
  33. {
  34. public abstract partial class MessageHandler<TC>
  35. {
  36. #region 默认方法及未知类型方法
  37. /// <summary>
  38. /// 【异步方法】认返回消息(当任何OnXX消息没有被重写,都将自动返回此默认消息)
  39. /// </summary>
  40. public virtual async Task<IResponseMessageBase> DefaultResponseMessageAsync(IRequestMessageBase requestMessage)
  41. {
  42. return await Task.Run(() => DefaultResponseMessage(requestMessage));
  43. }
  44. //{
  45. // 例如可以这样实现:
  46. // var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
  47. // responseMessage.Content = "您发送的消息类型暂未被识别。";
  48. // return responseMessage;
  49. //}
  50. /// <summary>
  51. /// 【异步方法】未知类型消息触发的事件,默认将抛出异常,建议进行重写
  52. /// </summary>
  53. /// <param name="requestMessage"></param>
  54. /// <returns></returns>
  55. public virtual async Task<IResponseMessageBase> OnUnknownTypeRequestAsync(RequestMessageUnknownType requestMessage)
  56. {
  57. var msgType = MsgTypeHelper.GetRequestMsgTypeString(requestMessage.RequestDocument);
  58. throw new UnknownRequestMsgTypeException("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!".FormatWith(msgType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
  59. }
  60. #endregion
  61. #region 接收消息方法
  62. /// <summary>
  63. /// 【异步方法】预处理文字或事件类型请求。
  64. /// 这个请求是一个比较特殊的请求,通常用于统一处理来自文字或菜单按钮的同一个执行逻辑,
  65. /// 会在执行OnTextRequest或OnEventRequest之前触发,具有以下一些特征:
  66. /// 1、如果返回null,则继续执行OnTextRequest或OnEventRequest
  67. /// 2、如果返回不为null,则终止执行OnTextRequest或OnEventRequest,返回最终ResponseMessage
  68. /// 3、如果是事件,则会将RequestMessageEvent自动转为RequestMessageText类型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey
  69. /// </summary>
  70. public virtual async Task<IResponseMessageBase> OnTextOrEventRequestAsync(RequestMessageText requestMessage)
  71. {
  72. var result = base.DefaultMessageHandlerAsyncEvent == Senparc.NeuChar.MessageHandlers.DefaultMessageHandlerAsyncEvent.DefaultResponseMessageAsync
  73. ? null
  74. : await Task.Run(() => OnTextOrEventRequest(requestMessage));
  75. return result;
  76. }
  77. /// <summary>
  78. /// 【异步方法】文字类型请求
  79. /// </summary>
  80. public virtual async Task<IResponseMessageBase> OnTextRequestAsync(RequestMessageText requestMessage)
  81. {
  82. return await DefaultAsyncMethod(requestMessage, () => OnTextRequest(requestMessage));
  83. }
  84. /// <summary>
  85. /// 【异步方法】位置类型请求
  86. /// </summary>
  87. public virtual async Task<IResponseMessageBase> OnLocationRequestAsync(RequestMessageLocation requestMessage)
  88. {
  89. return await DefaultAsyncMethod(requestMessage, () => OnLocationRequest(requestMessage));
  90. }
  91. /// <summary>
  92. /// 【异步方法】图片类型请求
  93. /// </summary>
  94. public virtual async Task<IResponseMessageBase> OnImageRequestAsync(RequestMessageImage requestMessage)
  95. {
  96. return await DefaultAsyncMethod(requestMessage, () => OnImageRequest(requestMessage));
  97. }
  98. /// <summary>
  99. /// 【异步方法】语音类型请求
  100. /// </summary>
  101. public virtual async Task<IResponseMessageBase> OnVoiceRequestAsync(RequestMessageVoice requestMessage)
  102. {
  103. return await DefaultAsyncMethod(requestMessage, () => OnVoiceRequest(requestMessage));
  104. }
  105. /// <summary>
  106. /// 【异步方法】视频类型请求
  107. /// </summary>
  108. public virtual async Task<IResponseMessageBase> OnVideoRequestAsync(RequestMessageVideo requestMessage)
  109. {
  110. return await DefaultAsyncMethod(requestMessage, () => OnVideoRequest(requestMessage));
  111. }
  112. /// <summary>
  113. /// 【异步方法】链接消息类型请求
  114. /// </summary>
  115. public virtual async Task<IResponseMessageBase> OnLinkRequestAsync(RequestMessageLink requestMessage)
  116. {
  117. return await DefaultAsyncMethod(requestMessage, () => OnLinkRequest(requestMessage));
  118. }
  119. /// <summary>
  120. /// 【异步方法】小视频类型请求
  121. /// </summary>
  122. public virtual async Task<IResponseMessageBase> OnShortVideoRequestAsync(RequestMessageShortVideo requestMessage)
  123. {
  124. return await DefaultAsyncMethod(requestMessage, () => OnShortVideoRequest(requestMessage));
  125. }
  126. /// <summary>
  127. /// 【异步方法】文件类型请求
  128. /// </summary>
  129. public virtual async Task<IResponseMessageBase> OnFileRequestAsync(RequestMessageFile requestMessage)
  130. {
  131. return await DefaultAsyncMethod(requestMessage, () => OnFileRequest(requestMessage));
  132. }
  133. #endregion
  134. #region NeuChar 方法
  135. /// <summary>
  136. /// NeuChar 请求
  137. /// </summary>
  138. public virtual async Task<IResponseMessageBase> OnNeuCharRequestAsync(RequestMessageNeuChar requestMessage)
  139. {
  140. return await DefaultAsyncMethod(requestMessage, () => OnNeuCharRequest(requestMessage));
  141. }
  142. #endregion
  143. }
  144. }
  145. #endif