MessageHandler.Message.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 文件名:MessageHandler.Message.cs
  17. 文件功能描述:微信请求的集中处理方法:Message相关
  18. 创建标识:Senparc - 20150924
  19. 修改标识:Senparc - 20180829
  20. 修改描述:v15.4.0 支持NeuChar,添加 OnNeuCharRequest() 方法
  21. ----------------------------------------------------------------*/
  22. using System;
  23. using Senparc.Weixin.Exceptions;
  24. using Senparc.CO2NET.Extensions;
  25. using Senparc.Weixin.MP.Entities;
  26. using Senparc.Weixin.MP.Helpers;
  27. using Senparc.CO2NET.Helpers;
  28. using Senparc.NeuChar;
  29. using Senparc.CO2NET.Trace;
  30. using System.IO;
  31. using Senparc.CO2NET.Utilities;
  32. using Senparc.NeuChar.Entities;
  33. using Senparc.NeuChar.Helpers;
  34. namespace Senparc.Weixin.MP.MessageHandlers
  35. {
  36. public abstract partial class MessageHandler<TC>
  37. {
  38. #region 默认方法及未知类型方法
  39. /// <summary>
  40. /// 默认返回消息(当任何OnXX消息没有被重写,都将自动返回此默认消息)
  41. /// </summary>
  42. public abstract IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage);
  43. //{
  44. // 例如可以这样实现:
  45. // var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
  46. // responseMessage.Content = "您发送的消息类型暂未被识别。";
  47. // return responseMessage;
  48. //}
  49. /// <summary>
  50. /// 未知类型消息触发的事件,默认将抛出异常,建议进行重写
  51. /// </summary>
  52. /// <param name="requestMessage"></param>
  53. /// <returns></returns>
  54. public virtual IResponseMessageBase OnUnknownTypeRequest(RequestMessageUnknownType requestMessage)
  55. {
  56. var msgType = MsgTypeHelper.GetRequestMsgTypeString(requestMessage.RequestDocument);
  57. throw new UnknownRequestMsgTypeException("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!".FormatWith(msgType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
  58. }
  59. #endregion
  60. #region 接收消息方法
  61. /// <summary>
  62. /// 预处理文字或事件类型请求。
  63. /// 这个请求是一个比较特殊的请求,通常用于统一处理来自文字或菜单按钮的同一个执行逻辑,
  64. /// 会在执行OnTextRequest或OnEventRequest之前触发,具有以下一些特征:
  65. /// 1、如果返回null,则继续执行OnTextRequest或OnEventRequest
  66. /// 2、如果返回不为null,则终止执行OnTextRequest或OnEventRequest,返回最终ResponseMessage
  67. /// 3、如果是事件,则会将RequestMessageEvent自动转为RequestMessageText类型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey
  68. /// </summary>
  69. public virtual IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage)
  70. {
  71. return null;
  72. }
  73. /// <summary>
  74. /// 文字类型请求
  75. /// </summary>
  76. public virtual IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
  77. {
  78. return DefaultResponseMessage(requestMessage);
  79. }
  80. /// <summary>
  81. /// 位置类型请求
  82. /// </summary>
  83. public virtual IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
  84. {
  85. return DefaultResponseMessage(requestMessage);
  86. }
  87. /// <summary>
  88. /// 图片类型请求
  89. /// </summary>
  90. public virtual IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
  91. {
  92. return DefaultResponseMessage(requestMessage);
  93. }
  94. /// <summary>
  95. /// 语音类型请求
  96. /// </summary>
  97. public virtual IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
  98. {
  99. return DefaultResponseMessage(requestMessage);
  100. }
  101. /// <summary>
  102. /// 视频类型请求
  103. /// </summary>
  104. public virtual IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
  105. {
  106. return DefaultResponseMessage(requestMessage);
  107. }
  108. /// <summary>
  109. /// 链接消息类型请求
  110. /// </summary>
  111. public virtual IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
  112. {
  113. return DefaultResponseMessage(requestMessage);
  114. }
  115. /// <summary>
  116. /// 小视频类型请求
  117. /// </summary>
  118. public virtual IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
  119. {
  120. return DefaultResponseMessage(requestMessage);
  121. }
  122. /// <summary>
  123. /// 文件请求
  124. /// </summary>
  125. public virtual IResponseMessageBase OnFileRequest(RequestMessageFile requestMessage)
  126. {
  127. return DefaultResponseMessage(requestMessage);
  128. }
  129. #endregion
  130. }
  131. }