前の記事のコードを続けて、プロジェクトに CustomMessageHandle.cs クラスを追加します。
CustomMessageHandle.cs は、抽象クラス Senparc.Weixin.MP.MessageHandlersusing System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.MessageHandlers;
namespace Senparc.Weixin.MP.Sample.Weixin
{
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;
}
}
}
var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
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; }
public virtual IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage); public virtual IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage); public virtual IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage); public virtual IResponseMessageBase OnTextRequest(RequestMessageText requestMessage); public virtual IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage); public virtual IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage); public virtual IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage); public virtual IResponseMessageBase OnEvent_ViewRequest(RequestMessageEvent_View requestMessage); public virtual IResponseMessageBase OnEvent_EnterRequest(RequestMessageEvent_Enter requestMessage); public virtual IResponseMessageBase OnEvent_LocationRequest(RequestMessageEvent_Location requestMessage); public virtual IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage); public virtual IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage); public virtual IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage) public virtual IResponseMessageBase OneEvent_MassSendJobFinisRequest(RequestMessageEvent_MassSendJobFinish requestMessage)
public override void OnExecuting() { if (RequestMessage.FromUserName == "olPjZjsXuQPJoV0HlruZkNzKc91E") { CancelExcute = true; //终止此用户的对话 //如果没有下面的代码,用户不会收到任何回复,因为此时ResponseMessage为null //添加一条固定回复 var responseMessage = CreateResponseMessage<ResponseMessageText>(); responseMessage.Content = "Hey!你已经被拉黑啦!"; ResponseMessage = responseMessage;//设置返回对象 } }