"Article Summary"
Although the Senparc.Weixin.MP is the public number of the SDK, but because the easy-faith public and Sina Weibo fan service platform also provides a compatible interface, so you can also use it to quickly implement the corresponding services, of course, Weibo due to the differences, if not change the original SDK, you need to adjust some places. This article uses Senparc.Weixin.MP to quickly realize the interface of the Sina Weibo fan service platform.
"Article index"
- Sina Weibo fan Service Platform Interface description
- Senparc.Weixin.MP Usage Introduction
- Handling of events for micro-bot
"One, Sina Weibo fan service Platform Interface description"
about how to apply for Sina Weibo fan service platform is not detailed introduction, Blue V certification can use the Fan service platform, if you need to use the development mode call interface, but also need to create an application in the microblogging open platform, type select "Micro Service Application" and "fan service platform", to apply the registration is good, You can get the Appkey and Appsecret for your application. Then in the Weibo "Management Center", "fan service" and "advanced features" can be set to the development mode, unlike the public number, you need to customize the token when using the development mode and when validating the message with other parameters placed together with the creation of signature signature, When using development mode, Weibo needs to fill in the Appkey of the appyou just created, and then create signature with the corresponding Appsecret. When you set up the Weibo fan service platform for the first time, Weibo validates the server address that you set up, that is, passing in the ECHOSTR parameter via get, then you need to return the parameters, and you can also verify the authenticity of the source.
The management interface of the Weibo fan service platform is almost identical to the public number, even with icons. Fan service platform and the public number is very similar, the bottom menu, message event push and so on are the same, and in the push of the message is more directly back to the user's UID, rather than just return to the current app only OpenID.
For a fan service platform that has just been configured, the default message push interface is JSON type, but it's nice to have a compatible XML interface, and if you've developed a public number, you can reuse the code a lot. If you need to switch between different formats, refer to Http://open.weibo.com/wiki/Eps/push/set_format This document, you need to post the Access_token and format=xml two parameters to the interface, Seems very complex, in fact, it is very simple to write one or to find a post-parameter program can be, and access_token is not the same as the need to get through the OAuth2.0, but in the microblog's Fan service Platform Management page directly display (valid for one year), such as.
In addition to this, the custom menu interface is similar to that of passing in the above Access_token, as well as passing the JSON code of the menu into the menus parameter to create the menu. The following is a main introduction to messages and events to push this comparison core content.
"Ii. Introduction to the use of Senparc.Weixin.MP"
Senparc.Weixin.MP is a FreeBSD-based open source public Platform SDK, the official website is: Http://weixin.senparc.com,GitHub is: Http://github.com/JeffreySu/WeiXinMPSDK. At the same time, the release version is available on NuGet, which makes it easy to manage with NuGet. In addition, the author also has an official documentation, http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html.
Weibo's fan service platform, like the public number, will be pushed to the address set in the fan service Platform developer mode for events sent by users, as well as clicks on menus. As with, the first authentication address uses the GET request method, which is used for post mode for future push.
For each request, you first need to verify the authenticity of the source, Senparc.Weixin.MP has encapsulated the feature in the Checksignature class, such as using HttpHandler, you can directly use the following code to check whether the source is valid:
String signature = request.querystring["signature"= request.querystring[" timestamp" = request.querystring[" nonce " " App_secret ");
When the validation source is valid, you can use Senparc.WeiXin.MP's messagehandler to implement the processing of the message, usually we need to inherit the class to implement a custom reply, such as the following code can send all users to reply to the message "Default reply message":
1 Public classWeibomessagehandler:messagehandler<messagecontext>2 {3 PrivateResponsemessagetext responsemessage;4 5 PublicWeibomessagehandler (Stream inputstream)6:Base(InputStream)7 {8 This. Responsemessage = This. Createresponsemessage<responsemessagetext>();9 }Ten One Public Overrideiresponsemessagebase defaultresponsemessage (irequestmessagebase requestmessage) A { - This. responsemessage.content ="Default reply Message"; - returnResponsemessage; the } -}
We can then call MessageHandler in Httphanlder with the following code to get the processed result and output:
1 if (string.equals ( " post " , Request.HttpMethod.ToUpperInvariant ())) // Post method reply message 2 { 3 Weibomessagehandler messagehandler = new Weibomessagehandler (request.inputstream); 4 Messagehandler.execute (); 5 Response.Output.Write (MessageHandler.ResponseDocument.ToString ()); 7 }
In addition, in inherited MessageHandler, you can also overload the following methods to implement different functions:
1, Iresponsemessagebase ontextrequest (Requestmessagetext requestmessage): Text message
2, Iresponsemessagebase oneventrequest (irequestmessageeventbase requestmessage): Event message
3, Iresponsemessagebase onevent_clickrequest (Requestmessageevent_click requestmessage): Menu click event Message
4, Iresponsemessagebase onevent_viewrequest (Requestmessageevent_view requestmessage): Menu click Link Message
5, Iresponsemessagebase onevent_subscriberequest (requestmessageevent_subscribe requestmessage): User Subscription event message
6, Iresponsemessagebase onevent_unsubscriberequest (requestmessageevent_unsubscribe requestMessage): User unsubscribe from event messages
For example, Fromusername can be obtained in each requestmessage, for which only OpenID is guaranteed within each application, and the user's UID is in the microblog. For text messages, there is a content property in Requestmessage to get the text content sent by the user, and to return different results based on the different content sent by the user; For event messages There is an event property to get the events type; The Eventkey attribute can be used to represent the menu event name and the URL to jump after clicking the menu, especially for click events, which can be used to return different content depending on the eventkey, whereas in the method of user subscription and unsubscribe, the relevant tags can be written to their own database. In addition to overloading the methods described above, there are a lot of ways that can be overloaded, which is not described here, for messages that are not handled individually (such as without overloading the appropriate method or elsewhere), the message in Defaultresponsemessage is returned by default.
It should be explained that in the microblogging fan service platform, for users to send messages, only text, Image, voice and position (location) four types, and no video and link two types, and for event messages, With the same attention (subscribe), cancel attention (unsubscribe), scan QR code (subscribe or scan) and menu click events (click) and menu click Links (VIEW) several, There is no event (location) that opens the Chat window to automatically escalate geolocation information, but there are more user concerns (follow) and a cancellation of attention (Unfollow) two events.
"Third, for the micro-bot to deal with the incident"
The Senparc.WeiXin.MP uses an overloaded approach to make the public number very simple, but there is a discrepancy between Weibo and the information being pushed. For Weibo, it doesn't matter, but for more than two incidents of Weibo (attention and cancellation of attention), additional processing is needed. Of course, modifying the source code of Senparc.WeiXin.MP is a scenario, but there are a few ways to use nuget to manage or not to modify the original code.
Because the Senparc.WeiXin.MP itself does not support follow and unfollow types, requestmessage in Oneventrequest or Requestmessage methods in MessageHandler do not get the correct eve NT attribute, whose event property is always enter, and if we do not handle it, Senparc.WeiXin.MP will also be processed by default by pressing the Enter event. So we need to read the information from the raw data, such as the following code:
1 Public Overrideiresponsemessagebase oneventrequest (irequestmessageeventbase requestmessage)2 {3String key = This. RequestDocument.Root.Element ("Event"). Value.tolowerinvariant ();4 5 Switch(Key)6 {7 Case "Follow"://concern8 return This. Onevent_followrequest (requestmessage);9 Case "unfollow"://Cancel your attentionTen return This. Onevent_unfollowrequest (requestmessage); One } A - //Other event handling code - the return Base. Oneventrequest (requestmessage); - } - - Publiciresponsemessagebase onevent_followrequest (irequestmessageeventbase requestmessage) + { - //Handling user Concern events + A return This. Responsemessage; at } - Publiciresponsemessagebase onevent_unfollowrequest (irequestmessageeventbase requestmessage) - { - //handling User canceled concern events - - return This. Responsemessage; in}
Of course For the old version of Senparc.WeiXin.MP, there was an error in Requestmessageeventbase's inheritance (Requestmessageeventbase should have implemented irequestmessageeventbase but not implemented). When the event type system is not recognized, execute Messagehanlder execute () will have a null pointer error, update to the latest 7.0.18 and later versions to resolve the problem.
"Reference link"
- Fan Service platform-Weibo api:http://open.weibo.com/wiki/%e7%b2%89%e4%b8%9d%e6%9c%8d%e5%8a%a1%e5%b9%b3%e5%8f%b0
. NET implementation of micro-blog fan Service Platform interface