We can continue with what we have not done before. now we can implement simple text interaction. This article will implement responses to various messages and events. We can continue with what we have not done before. now we can implement simple text interaction. This article will implement responses to various messages and events.
GitHub address: https://github.com/Andyahui/xgyxsh_WeiXin
SDK GitHub address: https://github.com/JeffreySu/WeiXinMPSDK/
SDK Official Blog: http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html
I. simple reply to various types of messages
Based on the current underlying framework, we can easily see that the rest of the response mechanisms are not very difficult, but we are not very familiar with this SDK. now we have implemented text reply, when we override the OnTextRequest method, we find that there are many remethods, as shown below.
This is how we need to implement various message and event mechanisms. You can implement corresponding methods to respond to various POST requests sent by the server. if the website has many requirements, the business logic to be processed is carried out in these methods.
public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage) { var responseVoice = base.CreateResponseMessage
(); responseVoice.Voice=new Voice() { MediaId = requestMessage.MediaId }; return responseVoice; }
The voice request sent by our client is first sent to the OnVoiceRequest method. if there is no reply, it is sent to the default reply method to reply to the user message. In the above method test, we created a voice response. by default, we reply to our own voice. MediaId is the ID of the multimedia file uploaded through the material management interface, the server actually saves the user's voice sending ID;
Public override IResponseMessageBase OnImageRequest (RequestMessageImage requestMessage) {var responseImage = base. CreateResponseMessage
(); ResponseImage. Content = "from image, ahui"; return responseImage ;}
The image request sent by the user, and then we reply to the text. If we need complicated logic, we can add it here. The base. CreateResponseMessage () Is to create a reply text type;
Public override IResponseMessageBase OnLocationRequest (RequestMessageLocation requestMessage) {// The Returned message is a text message about the address. Var responseLocation = base. CreateResponseMessage
(); Var markersList = new List
(); MarkersList. add (new BaiduMarkers () {Size = BaiduMarkerSize. m, Color = "red", Label = "A", Latitude = requestMessage. location_X, longpolling = requestMessage. location_Y,}); var mapUrl = BaiduMapHelper. getBaiduStaticMap (requestMessage. location_Y, requestMessage. location_X, 1,13, markersList); responseLocation. articles. add (new Article () {Description = string. format ("You just sent the location information. Location_X: {0}, Location_Y: {1}, Scale: {2}, tag: {3} ", requestMessage. location_X, requestMessage. location_Y, requestMessage. scale, requestMessage. label), PicUrl = "http://pic.cnblogs.com/avatar/679140/20141128195544.png", Title = "", Url = mapUrl}); return responseLocation ;}
When processing geographic location requests, users can send their own geographic locations. we can send users any information we want to send. the above is the interface that calls Baidu Map, send its own information again.
Public override IResponseMessageBase OnLinkRequest (RequestMessageLink requestMessage) {var responseLink = base. CreateResponseMessage
(); ResponseLink. Articles. Add (new Article () {Title = "process requests from links. ", Description = requestMessage. Description, PicUrl =" http://pic.cnblogs.com/avatar/679140/20141128195544.png ", Url = requestMessage. Url}); return responseLink ;}
The link also replies text messages. many of the previous messages reply text messages, but we need to pay attention to the parameters in them so that the reply content can be met.
II. POST event Reply
During the interaction between the user and the public account, some user operations will make the server send a request to the website server set by the developer in the form of events. some events will allow us to reply to user information; here is how to deal with these.
Event push for developer documents:
Http://mp.weixin.qq.com/wiki/7/9f89d962eba4c5924ed95b513ba69d9b.html
The Event _ identifier indicates the Event method. we need to override these methods to implement our response. There are many introductions on the official website.
////// Subscribe to events /////////
Public override IResponseMessageBase OnEvent_SubscribeRequest (RequestMessageEvent_Subscribe requestMessage) {var responseSubscribe = base. CreateResponseMessage
(); ResponseSubscribe. Content = "Welcome to subscribe, welcome to Zhang Hui. "; Return responseSubscribe ;}
Reply to the text content, which is conducive to testing. we can add any logic or reply to any text, voice, text, and other types. This depends on the needs of the project.
////// Click event /////////
Public override IResponseMessageBase OnEvent_ClickRequest (RequestMessageEvent_Click requestMessage) {var respondeEvert = base. CreateResponseMessage
(); RespondeEvert. Content = "click event,"; return respondeEvert ;}
You may not know what this click event is. it is set on the menu bar. then we will implement the menu bar function.
Public override IResponseMessageBase OnEvent_ViewRequest (RequestMessageEvent_View requestMessage) {var responseView = base. CreateResponseMessage
(); ResponseView. Content = "click the menu bar with view"; return responseView ;}
3. implement the custom menu of public accounts
Each page has a menu bar, which can be implemented through regular settings. now we only have the test number, which has not been found on the test number page for a long time, after query, you can set it in developer debugging. Another method is to use the page provided by the SDK.
In the first figure, we also need the body to call the json data packet of the interface. you can refer to the settings in the development documentation. Note that the two event types click and view are displayed.
{"Button": [{"name": "IT information", "sub_button": [{"type": "view", "name": "blog ", "url ":" http://m.cnblogs.com/ "," Sub_button ": [] },{" type ":" view "," name ":" IT news "," url ":" http://news.cnblogs.com/m "," Sub_button ": []}, {" type ":" click "," name ":" About developers "," key ":" http://m.cnblogs.com/ ? U = netxiaohui "," sub_button ": []}] },{" name ":" Campus Life "," sub_button ": [{" type ":" view ", "name": "Search", "url ":" http://www.baidu.com/ "," Sub_button ": [] },{" type ":" view "," name ":" video "," url ":" http://v.qq.com/ "," Sub_button ": [] },{" type ":" click "," name ":" Follow us "," key ":" V1001_GOOD "," sub_button ": []}] },{ "name": "Student", "sub_button": [{"type": "view", "name": "Search ", "url ":" http://www.soso.com/ "," Sub_button ": [] },{" type ":" view "," name ":" video "," url ":" http://v.qq.com/ "," Sub_button ": [] },{" type ":" click "," name ":" Follow us "," key ":" V1001_GOOD "," sub_button ": []}
We can set the event types at will to respond to the menu events we set earlier. we can process a series of logic code.
3.2: SDK custom menu
The following URL can be used to set the custom menu. the preceding two parameters AppId, secret, and Token are still required.
Set custom menu URL: http://sdk.weixin.senparc.com/Menu
The above is the detailed content for developing and implementing various message responses. For more information, see other related articles on php Chinese network!