系列目录
前言
Senparc.Weixin.MP SDK提供了MessageHandler消息处理类
在作者的Wiki中也详细说明了如何定义这个类,下面我们来演示,消息的回复,及效果
了解MessageHandler
引用作者的话:CustomMessageHandle.cs需要继承Senparc.Weixin.MP.MessageHandlers<TC>这个抽象类,并实现部分方法。最初步的CustomMessageHandle.cs代码
public class CustomMessageHandler : MessageHandler<CustomMessageContext>
{
public CustomMessageHandler(Stream inputStream, PostModel postModel)
: base(inputStream, postModel)
{ } public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
responseMessage.Content = "这条消息来自DefaultResponseMessage。";
return responseMessage;
}
}
这个类继承接口IResponseMessageBase并可以选择重写下面事件
// 摘要:
// Event事件类型请求
public virtual IResponseMessageBase OnEventRequest(IRequestMessageEventBase requestMessage);
//
// 摘要:
// 卡券未通过审核
public virtual IResponseMessageBase OnEvent_Card_Not_Pass_CheckRequest(RequestMessageEvent_Card_Not_Pass_Check requestMessage);
//
// 摘要:
// 卡券通过审核
public virtual IResponseMessageBase OnEvent_Card_Pass_CheckRequest(RequestMessageEvent_Card_Pass_Check requestMessage);
//
// 摘要:
// Event事件类型请求之CLICK
public virtual IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage);
//
// 摘要:
// Event事件类型请求之ENTER
public virtual IResponseMessageBase OnEvent_EnterRequest(RequestMessageEvent_Enter requestMessage);
//
// 摘要:
// 多客服关闭会话
public virtual IResponseMessageBase OnEvent_Kf_Close_SessionRequest(RequestMessageEvent_Kf_Close_Session requestMessage);
//
// 摘要:
// 多客服接入会话
public virtual IResponseMessageBase OnEvent_Kf_Create_SessionRequest(RequestMessageEvent_Kf_Create_Session requestMessage);
//
// 摘要:
// 多客服转接会话
public virtual IResponseMessageBase OnEvent_Kf_Switch_SessionRequest(RequestMessageEvent_Kf_Switch_Session requestMessage);
//
// 摘要:
// Event事件类型请求之LOCATION
public virtual IResponseMessageBase OnEvent_LocationRequest(RequestMessageEvent_Location requestMessage);
//
// 摘要:
// 弹出地理位置选择器
public virtual IResponseMessageBase OnEvent_LocationSelectRequest(RequestMessageEvent_Location_Select requestMessage);
//
// 摘要:
// 事件推送群发结果
public virtual IResponseMessageBase OnEvent_MassSendJobFinishRequest(RequestMessageEvent_MassSendJobFinish requestMessage);
//
// 摘要:
// Event事件类型请求之微小店订单付款通知
public virtual IResponseMessageBase OnEvent_Merchant_Order(RequestMessageEvent_Merchant_Order requestMessage);
//
// 摘要:
// 弹出拍照或者相册发图
public virtual IResponseMessageBase OnEvent_PicPhotoOrAlbumRequest(RequestMessageEvent_Pic_Photo_Or_Album requestMessage);
//
// 摘要:
// 弹出系统拍照发图
public virtual IResponseMessageBase OnEvent_PicSysphotoRequest(RequestMessageEvent_Pic_Sysphoto requestMessage);
//
// 摘要:
// 弹出微信相册发图器
public virtual IResponseMessageBase OnEvent_PicWeixinRequest(RequestMessageEvent_Pic_Weixin requestMessage);
//
// 摘要:
// Event事件类型请求之审核结果事件推送
public virtual IResponseMessageBase OnEvent_Poi_Check_NotifyRequest(RequestMessageEvent_Poi_Check_Notify requestMessage);
//
// 摘要:
// 扫码推事件
public virtual IResponseMessageBase OnEvent_ScancodePushRequest(RequestMessageEvent_Scancode_Push requestMessage);
//
// 摘要:
// 扫码推事件且弹出“消息接收中”提示框
public virtual IResponseMessageBase OnEvent_ScancodeWaitmsgRequest(RequestMessageEvent_Scancode_Waitmsg requestMessage);
//
// 摘要:
// Event事件类型请求之scan
public virtual IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage);
//
// 摘要:
// Event事件类型请求之摇一摇事件通知
public virtual IResponseMessageBase OnEvent_ShakearoundUserShake(RequestMessageEvent_ShakearoundUserShake requestMessage);
//
// 摘要:
// Event事件类型请求之接收会员信息事件通知
public virtual IResponseMessageBase OnEvent_Submit_Membercard_User_Info(RequestMessageEvent_Submit_Membercard_User_Info requestMessage);
//
// 摘要:
// Event事件类型请求之subscribe
public virtual IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage);
//
// 摘要:
// 发送模板消息返回结果
public virtual IResponseMessageBase OnEvent_TemplateSendJobFinishRequest(RequestMessageEvent_TemplateSendJobFinish requestMessage);
//
// 摘要:
// Event事件类型请求之unsubscribe
public virtual IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage);
//
// 摘要:
// Event事件类型请求之卡券核销
public virtual IResponseMessageBase OnEvent_User_Consume_Card(RequestMessageEvent_User_Consume_Card requestMessage);
//
// 摘要:
// 删除卡券
public virtual IResponseMessageBase OnEvent_User_Del_CardRequest(RequestMessageEvent_User_Del_Card requestMessage);
//
// 摘要:
// Event事件类型请求之从卡券进入公众号会话
public virtual IResponseMessageBase OnEvent_User_Enter_Session_From_Card(RequestMessageEvent_User_Enter_Session_From_Card requestMessage);
//
// 摘要:
// 领取卡券
public virtual IResponseMessageBase OnEvent_User_Get_CardRequest(RequestMessageEvent_User_Get_Card requestMessage);
//
// 摘要:
// Event事件类型请求之进入会员卡
public virtual IResponseMessageBase OnEvent_User_View_Card(RequestMessageEvent_User_View_Card requestMessage);
//
// 摘要:
// 事件之URL跳转视图(View)
public virtual IResponseMessageBase OnEvent_ViewRequest(RequestMessageEvent_View requestMessage);
//
// 摘要:
// Event事件类型请求之Wi-Fi连网成功
public virtual IResponseMessageBase OnEvent_WifiConnected(RequestMessageEvent_WifiConnected requestMessage);
public virtual void OnExecuted();
public virtual void OnExecuting();
//
// 摘要:
// 图片类型请求
public virtual IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage);
//
// 摘要:
// 链接消息类型请求
public virtual IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage);
//
// 摘要:
// 位置类型请求
public virtual IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage);
//
// 摘要:
// 小视频类型请求
public virtual IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage);
//
// 摘要:
// 预处理文字或事件类型请求。 这个请求是一个比较特殊的请求,通常用于统一处理来自文字或菜单按钮的同一个执行逻辑, 会在执行OnTextRequest或OnEventRequest之前触发,具有以下一些特征:
// 1、如果返回null,则继续执行OnTextRequest或OnEventRequest 2、如果返回不为null,则终止执行OnTextRequest或OnEventRequest,返回最终ResponseMessage
// 3、如果是事件,则会将RequestMessageEvent自动转为RequestMessageText类型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey
public virtual IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage);
//
// 摘要:
// 文字类型请求
public virtual IResponseMessageBase OnTextRequest(RequestMessageText requestMessage);
//
// 摘要:
// 视频类型请求
public virtual IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage);
//
// 摘要:
// 语音类型请求
public virtual IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage);
基本列出了用户不同类型的请求,比如用户向我们发送一条信息,那么会最终会调用OnTextRequest这个方法。
所以我们在CustomMessageHandler中可以重写方法OnTextRequest
public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
{
var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
responseMessage.Content = "您的OpenID是:" + requestMessage.FromUserName //这里的requestMessage.FromUserName也可以直接写成base.WeixinOpenId
+ "。\r\n您发送了文字信息:" + requestMessage.Content; //\r\n用于换行,requestMessage.Content即用户发过来的文字内容
return responseMessage;
}
上面代码给用户回复了一条文本消息(下面有其他类型可供选择,但是我们一般使用的是文本和图文形式,其他情景基本不用)
- ResponseMessageText - 对应文本消息
- ResponseMessageNews - 对应图文消息
- ResponseMessageMusic - 对应音乐消息
- ResponseMessageXXX - 其他类型以此类推
同时用户不仅可以给公众号发送文本,也是可以发送语音,位置,视频的(但是实际应用中我们一般不处理这些场景),需要重写
- OnImageRequest -对应图文的请求
- OnLinkRequest - 对应超链接的请求
- OnTextRequest - 对应文本的请求
- OnLocationRequest - 对应位置的请求
- ...... - 其他请求类型或者时间
最后返回最用户的处理
[HttpPost]
[ActionName("Post")]
public ActionResult Post(string signature, string timestamp, string nonce, string echostr)
{
if (!CheckSignature.Check(signature, timestamp, nonce, Token))
{
return Content("参数错误!");
} var messageHandler = new CustomerMessageHandler(Request.InputStream);
messageHandler.Execute();//执行微信处理过程
return Content(messageHandler.ResponseDocument.ToString());
}
返回图文的类型
上面代码提供了文本的,我们这次再来提供一个图文本的吧,这个比较陌生:
当只是一条组合的时候是这样的
var responseMessage = CreateResponseMessage<ResponseMessageNews>();
responseMessage.Articles.Add(new Article()
{
Title = "十三妹",
Description = "十三妹",
PicUrl = "http://ymnets.imwork.net/uplouad/pic/20160903/1.png",
Url = "http://ymnets.cnblogs.com"
});
return responseMessage;
多条对应:只需要在responseMessage.Articles.Add添加Article就可以了
var responseMessage = CreateResponseMessage<ResponseMessageNews>();
responseMessage.Articles.Add(new Article()
{
Title = "陈浩南01",
Description = "陈浩南01",
PicUrl = "http://ymnets.imwork.net/uplouad/pic/20160903/2.png",
Url = "http://ymnets.cnblogs.com"
});
responseMessage.Articles.Add(new Article()
{
Title = "陈浩南02",
Description = "陈浩南02",
PicUrl = "http://ymnets.imwork.net/uplouad/pic/20160903/4.png",
Url = "http://ymnets.cnblogs.com"
});
responseMessage.Articles.Add(new Article()
{
Title = "陈浩南03",
Description = "陈浩南03",
PicUrl = "http://ymnets.imwork.net/uplouad/pic/20160903/5.png",
Url = "http://ymnets.cnblogs.com"
});
return responseMessage;
这一节,主要为我们后面创建自动回复模块进行铺垫,知道要返回的格式,才能对应的设计好数据库的表