WeChat public platform development and learning series (2): WeChat public platform receives and sends messages, and the public sends messages

Source: Internet
Author: User

Public platform development and learning series (ii): The public platform receives and sends messages, and the public sends messages

This article describes how to use senparc to process messages received and sent by the public platform.

The first vertex will send the user's sent information to the url filled in with the post request. The server code is as follows:

1 [HttpPost] 2 public ActionResult Get (PostModel postModel) 3 {4 var messageHandler = new CustomMessageHandler (Request. inputStream, postModel); 5 6 messageHandler. execute (); // Execute the processing process 7 8 return Content (messageHandler. responseDocument. toString (); 9}

Create a CustomMessageHandler class and a CustomMessageContext class. The CustomMessageHandler class inherits the MessageHandler <CustomMessageContext> and the messmmessagecontext class inherits the MessageContext <strong, strong> and CustomMessageContext classes, the main method is to implement the CustomMessageHandler class. The Code is as follows:

1 public CustomMessageHandler (Stream inputStream, PostModel postModel) 2: base (inputStream, postModel) 3 {4 5} 6 7 public override IResponseMessageBase DefaultResponseMessage (IRequestMessageBase requestMessage) 8 {9 var responseMessage = base. createResponseMessage <ResponseMessageText> (); // ResponseMessageText indicates that the returned text is 10 responseMessage. content = "this message comes from DefaultResponseMessage. "+ RequestMessage. MsgId; 11 return responseMessage; 12}

The formats of returned information are diverse, such as ResponseMessageText, ResponseMessageNews, and ResponseMessageImage. There are also many formats for receiving information. Different formats have different processing methods: OnTextRequest, OnImageRequest, OnEvent_ViewRequest, etc. If the corresponding method is not overwritten, The DefaultResponseMessage method is executed by default. In this article, rewrite two methods as a simple example.

Processing of returned information when a user sends text information

1 public override IResponseMessageBase OnTextRequest (RequestMessageText requestMessage) 2 {3 var responseMessage = base. createResponseMessage <ResponseMessageText> (); 4 responseMessage. content = "Your OpenID is:" + requestMessage. fromUserName 5 + ". \ R \ n you have sent the text message: "+ requestMessage. Content; 6 return responseMessage; 7}

 

Processing of returned information when a menu click event occurs

1 public override IResponseMessageBase OnEvent_ClickRequest (RequestMessageEvent_Click requestMessage) 2 {3 IResponseMessageBase reponseMessage = null; 4 // click the menu, which must match the Key 5 switch (requestMessage. eventKey) 6 {7 case "OneClick": 8 {9 var strongResponseMessage = CreateResponseMessage <ResponseMessageNews> (); 10 reponseMessage = strongResponseMessage; 11 strongResponseMessage. articles. add (new Arti Cle () 12 {13 Title = "you clicked the menu text button", 14 Description = "you clicked the text link button, which is a text message. ", 15 PicUrl =" https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3233073314,694013259&fm=58&s=39C718720E8EBE011B398BAC0300F024 ", 16 Url =" http://www.baidu.com "17}); 18} 19 break; 20 case" success ": 21 {22 var strongResponseMessage = CreateResponseMessage <ResponseMessageText> (); 23 reponseMessage = strongResponseMessage; 24 strongResponseMessage. content = "you have clicked the sub-menu button. "; 25} 26 break; 27 default: 28 {29 var strongResponseMessage = CreateResponseMessage <ResponseMessageText> (); 30 strongResponseMessage. content = "you have clicked the button, EventKey:" + requestMessage. eventKey; 31 reponseMessage = strongResponseMessage; 32} 33 break; 34} 35 36 return reponseMessage; 37}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.