This article mainly introduces public platform development: Learn about MessageHandler and continue the previous code. we will continue to add a CustomMessageHandle. cs class for the project:
It may be as follows:
Using 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
{Public mmmmessagehandler (Stream inputStream, PostModel postModel): base (inputStream, postModel) {} public override IResponseMessageBase DefaultResponseMessage (IRequestMessageBase requestMessage) {var responseMessage = base. CreateResponseMessage
(); // ResponseMessageText can also be News or other types of responseMessage. Content = "this message comes from DefaultResponseMessage. "; Return responseMessage ;}}}
We can see that the abstract method that must be rewritten is named DefaultResponseMessage (). This information is used to return a message. if the message of the corresponding type (such as voice) is not processed by the code, the result is returned by default.
In the DefaultResponseMessage () method, we can see the following sentence:
Var responseMessage = base. CreateResponseMessage
(); // ResponseMessageText can also be News or other types
CreateResponseMessage The method creates a return object. T can be any of the following types, corresponding to different return types:
ResponseMessageText-text message
ResponseMessageNews-corresponding text message
ResponseMessageMusic-corresponding music message
ResponseMessageXXX-other types, and so on
For how to set all the preceding parameters, see the Demo of the open-source project. we will not repeat it here: https://github.com/jeffreysu/weixinmpsdk.
So how can we handle text messages sent from users?
Very simple-simply rewrite an OnTextRequest method:
Public override IResponseMessageBase OnTextRequest (RequestMessageText requestMessage) {var responseMessage = base. CreateResponseMessage
(); ResponseMessage. Content = "your OpenID is:" + requestMessage. FromUserName // here requestMessage. FromUserName can also be directly written as base. WeixinOpenId + ". \ R \ n you have sent the text message: "+ requestMessage. Content; // \ r \ n for line feed. requestMessage. Content is the user's text Content return responseMessage ;}
This method can be used freely, such as reading databases, determining keywords, and even returning different ResponseMessageXX types (as long as the final type is under the ireeclipsemessagebase interface ).
It corresponds to OnTextRequest. to process voice, geographic location, menu, and other types of messages, you only need to override the corresponding method. you can override the method as follows:
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)
OnEvent_XX corresponds to the subtype of the Event request.
When setting the basic class of CustomMessageHandler, we can see that a wildcard called MessageContext (MessageHandler ). MessageContext is a default message context processing class provided by the SDK. this class can handle the most basic situations. if your application is not very complex, you can simply use this class. If the project is complex, you can also write your own class (inheriting the IMessageContext interface) as needed ), or inherit from this class to expand some more attributes (such as workflow and distributed cache ).
So far, we have used MassageHandler to process requests sent by all users.
The following describes some "secret weapons" of MassageHandler ".
OnExecuting () and OnExecuted ()
We can directly rewrite these two methods. OnExecuting is executed before all message processing methods (such as OnTextRequest and OnVoiceRequest). in this process, we can set CancelExecute to true, to interrupt the execution of all subsequent methods (including OnExecuted), for example:
Public override void OnExecuting () {if (RequestMessage. fromUserName = "olPjZjsXuQPJoV0HlruZkNzKc91E") {CancelExcute = true; // terminate the dialog of this user. // if the following code is not found, the user will not receive any reply, at this time, ResponseMessage is null // add a fixed response var responseMessage = CreateResponseMessage
(); ResponseMessage. Content = "Hey! You have been hacked! "; ResponseMessage = responseMessage; // Set the returned object }}
If there is no interruption in OnExecuting, the OnExecuted () method will be triggered after the OnTextRequest method is executed (or the default method is executed), and we can rewrite it accordingly. Note that within the OnExecuted () method, the ResponseMessage has been assigned a return value.
More public platform development: For more information about MessageHandler, see the Chinese PHP website!