俊峻娘 發表於 2019-5-12 12:47:00

微信公众号开发:消息处理

<p><span style="color: rgba(45, 148, 222, 1); font-size: 18pt">前言</span>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;回顾上一节服务器配置的内容,我们已经可以自己完成公众号服务器的配置。配置完成之后,我们就可以通过调用的方式,完成对消息管理的处理。当用户关注公众号或者发送消息的时候,我们应该启用默认回复,要不然用户得不到回应,</p>
<p>从而导致丢失体验。所以这一章节,我们将通过消息管理的方式,对用户的信息进行处理,完成公众号消息回复功能,实现公众号与用户之间的完整对话。</p>
<p><span style="font-size: 18pt; color: rgba(45, 148, 222, 1)">了解</span>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;微信公众平台对信息做了比较清晰的分类,最基本的包括请求(Request)和响应(Response)两大类信息,这两类信息有分为文字、语音、图片等格式。Senparc.Weixin.MP提供了MessageHandler消息处理类,这些类型在以枚举的方式区分,</p>
<p>同时根据严格命名规则命名了所有类型的RequestMessage和ResponseMessage。在Senparc里也详细说明了如何这个类的</p>
<p><img src="https://img2018.cnblogs.com/blog/1576550/201905/1576550-20190512145627171-881007020.png" alt="" width="859" height="373"></p>
<p><span style="font-size: 18pt; color: rgba(45, 148, 222, 1)">开始</span>:</p>
<p><span style="font-size: 14pt; font-family: 楷体">第一步</span>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;新建一个UserMessageHandler.cs,需要继承Senparc.Weixin.MP.MessageHandlers&lt;TC&gt;这个抽象类,并重写所有方法:</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> UserMessageHandler : MessageHandler&lt;UserMessageContext&gt;<span style="color: rgba(0, 0, 0, 1)">
    {
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 构造函数
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="inputStream"&gt;</span><span style="color: rgba(0, 128, 0, 1)">构造函数的inputStream用于接收来自微信服务器的请求流(如果需要在外部处理,这里也可以传入XDocument)</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="postModel"&gt;</span><span style="color: rgba(0, 128, 0, 1)">微信公众服务器Post过来的加密参数集合(不包括PostData)</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserMessageHandler(Stream inputStream, PostModel postModel)
            : </span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">(inputStream, postModel)
      {
         
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
      {
            </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 所有没有被处理的消息会默认返回这里的结果
            </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = <span style="color: rgba(0, 0, 255, 1)">this</span>.CreateResponseMessage&lt;ResponseMessageText&gt;();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ResponseMessageText也可以是News等其他类型</span>
            responseMessage.Content = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">这条消息来自DefaultResponseMessage。</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }
    }</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Senparc.Weixin.Context;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Senparc.Weixin.MP.Entities;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Collections.Generic;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Linq;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Web;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> WeiXinHandler
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> UserMessageContext: MessageContext&lt;IRequestMessageBase, IResponseMessageBase&gt;<span style="color: rgba(0, 0, 0, 1)">
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserMessageContext()
      {
            </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
            * 注意:即使使用其他类实现IMessageContext,
            * 也务必在这里进行下面的初始化,尤其是设置当前时间,
            * 这个时间关系到及时从缓存中移除过期的消息,节约内存使用
            </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
            <span style="color: rgba(0, 0, 255, 1)">base</span>.MessageContextRemoved +=<span style="color: rgba(0, 0, 0, 1)"> UserMessageContext_MessageContextRemoved;
      }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 当上下文过期,被移除时触发的时间
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="sender"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="e"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">void</span> UserMessageContext_MessageContextRemoved(<span style="color: rgba(0, 0, 255, 1)">object</span> sender, Senparc.Weixin.Context.WeixinContextRemovedEventArgs&lt;IRequestMessageBase, IResponseMessageBase&gt;<span style="color: rgba(0, 0, 0, 1)"> e)
      {
            </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 注意,这个事件不是实时触发的(当然你也可以专门写一个线程监控)
             * 为了提高效率,根据WeixinContext中的算法,这里的过期消息会在过期后下一条请求执行之前被清除
             </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> messageContext = e.MessageContext <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> CustomMessageContext;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (messageContext == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果是正常的调用,messageContext不会为null</span>
<span style="color: rgba(0, 0, 0, 1)">            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TODO:这里根据需要执行消息过期时候的逻辑,下面的代码仅供参考
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Log.InfoFormat("{0}的消息上下文已过期",e.OpenId);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">api.SendMessage(e.OpenId, "由于长时间未搭理客服,您的客服状态已退出!");</span>
<span style="color: rgba(0, 0, 0, 1)">      }
    }
}</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp;重写的方法对应了接收不同的Request类型,构造函数的inputStream用于接收来自微信服务器的请求流</p>
<p><span style="font-family: 楷体; font-size: 14pt">第二步</span>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;基本用户不同类型的请求,比如用户向我们发送一条信息,那么会最终会调用OnTextRequest这个方法,所以在不同的重写方法内,实现自己的方法。</p>
<p>比如:我们对于文字(Text)信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnTextRequest:</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = <span style="color: rgba(0, 0, 255, 1)">base</span>.CreateResponseMessage&lt;ResponseMessageText&gt;<span style="color: rgba(0, 0, 0, 1)">();
            responseMessage.Content </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">您刚刚发送了文字信息:</span><span style="color: rgba(128, 0, 0, 1)">"</span> + requestMessage.Content;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">requestMessage.Content即用户发过来的文字内容</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }</span></pre>
</div>
<p>&nbsp;对于图片信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnImageRequest</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 处理图片请求
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="requestMessage"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = CreateResponseMessage&lt;ResponseMessageNews&gt;<span style="color: rgba(0, 0, 0, 1)">();
            responseMessage.Articles.Add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Article()
            {
                Title </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">您刚才发送了图片信息</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                Description </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">您发送的图片将会显示在边上</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                PicUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> requestMessage.PicUrl,
                Url </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/i3yuan/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            });
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }</span></pre>
</div>
<p>&nbsp;对于语音信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnVoiceRequest</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 处理语音请求
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="requestMessage"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
      {

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取公众号</span>
            AccessTokenResult account =<span style="color: rgba(0, 0, 0, 1)"> Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(AppId, AppSecret);

            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = CreateResponseMessage&lt;ResponseMessageMusic&gt;<span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上传缩略图</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> uploadResult =<span style="color: rgba(0, 0, 0, 1)"> Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(account.access_token, UploadMediaFileType.image,
                                                         Server.GetMapPath(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">~/Images/Logo.jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置音乐信息</span>
            responseMessage.Music.Title = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">天籁之音</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            responseMessage.Music.Description </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">播放您上传的语音</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            responseMessage.Music.MusicUrl </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> requestMessage.MediaId;
            responseMessage.Music.HQMusicUrl </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://sdk.weixin.senparc.com/Media/GetVoice?mediaId=</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> requestMessage.MediaId;
            responseMessage.Music.ThumbMediaId </span>=<span style="color: rgba(0, 0, 0, 1)"> uploadResult.media_id;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }   </span></pre>
</div>
<p>对于视频信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnVideoRequest</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 处理视频请求
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="requestMessage"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = CreateResponseMessage&lt;ResponseMessageText&gt;<span style="color: rgba(0, 0, 0, 1)">();
            responseMessage.Content </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">您发送了一条视频信息,ID:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> requestMessage.MediaId;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }</span></pre>
</div>
<p>对于地理信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnLocationRequest</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 处理位置请求
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="requestMessage"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> locationService = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> LocationService();
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = locationService.GetResponseMessage(requestMessage <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> RequestMessageLocation);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }</span></pre>
</div>
<p>对于链接信息进行这样的处理,在UserMessageHandler中我们可以重写方法OnLinkRequest</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 处理链接消息请求
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="requestMessage"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = ResponseMessageBase.CreateFromRequestMessage&lt;ResponseMessageText&gt;<span style="color: rgba(0, 0, 0, 1)">(requestMessage);
            responseMessage.Content </span>= <span style="color: rgba(0, 0, 255, 1)">string</span>.Format(<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">您发送了一条连接信息:
            Title:{0}
            Description:{1}
            Url:{2}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, requestMessage.Title, requestMessage.Description, requestMessage.Url);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;
      }</span></pre>
</div>
<p><span style="font-family: 楷体; font-size: 14pt">第三步</span>:</p>
<p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;在Action中使用MessageHandler,返回对用户的处理,在上一节中我们已经新建了WXController.cs,在其中通过Post的方式处理用户的请求</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">      
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> Task&lt;ActionResult&gt;<span style="color: rgba(0, 0, 0, 1)"> Post(PostModel postModel)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> Task.Factory.StartNew&lt;ActionResult&gt;(() =&gt;<span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
                {
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> WeixinResult(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">参数错误!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
                }
                </span><span style="color: rgba(0, 0, 255, 1)">var</span> messageHandler = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UserMessageHandler(Request.InputStream);
                messageHandler.Execute(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行微信处理过程</span>
                <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FixWeixinBugWeixinResult(messageHandler);
            }).ContinueWith</span>&lt;ActionResult&gt;(task =&gt;<span style="color: rgba(0, 0, 0, 1)"> task.Result);
      }
    }</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp;messageHandler.Execute();用于执行整个信息处理过程,其中会调用重写的OnxxRequest方法</p>
<p><span style="font-size: 18pt; color: rgba(45, 148, 222, 1)">效果</span>:</p>
<p>&nbsp;测试发送文本</p>
<p><img src="https://img2018.cnblogs.com/blog/1576550/201905/1576550-20190512122546209-2046252474.jpg" alt="" width="247" height="314"></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;通过测试公众号,我们可以发现,当我们发送文本的时候,系统会对用户的信息进行处理,完成公众号消息回复功能,实现公众号与用户之间的完整对话。</p>
<p>测试发送图文消息</p>
<p><img src="https://img2018.cnblogs.com/blog/1576550/201905/1576550-20190512122529889-644024472.jpg" alt="" width="245" height="325"></p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)"> IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> responseMessage = CreateResponseMessage&lt;ResponseMessageNews&gt;<span style="color: rgba(0, 0, 0, 1)">();
            responseMessage.Articles.Add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Article()
            {
                Title </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">灌篮高手</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                Description </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">灌篮高手</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                PicUrl </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://images.cnblogs.com/cnblogs_com/i3yuan/1462639/o_timg%20(1).jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                Url </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/i3yuan/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            });
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> responseMessage;

      }</span></pre>
</div>
<p><span style="color: rgba(45, 148, 222, 1)"><span style="font-size: 24px">总结</span></span>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;1.通过MessageHandler的简单处理,我们就可以进行对用户文本消息的处理,完成公众号与用户的会话</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;2.发送不同的消息,处理不同的回复,实现更多类型的消息回复</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;3.参考了如何使用MessageHandler简化消息处理流程</p>

</div>
<div id="MySignature" role="contentinfo">
    <div>作者:艾三元</div>
<div>出处:https://www.cnblogs.com/i3yuan/</div>
<div>关注:推荐扫码关注公众号</div>
<img src="https://images.cnblogs.com/cnblogs_com/i3yuan/1381525/o_210326143458wxcode.jpg" style="width: 105px">
    <div>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 </div><br><br>
来源:https://www.cnblogs.com/i3yuan/p/10850169.html
頁: [1]
查看完整版本: 微信公众号开发:消息处理