ホームページ >バックエンド開発 >C#.Net チュートリアル >WeChatパブリックプラットフォームSDKコアライブラリの詳細な紹介

WeChatパブリックプラットフォームSDKコアライブラリの詳細な紹介

零下一度
零下一度オリジナル
2017-06-24 10:41:131988ブラウズ

NuGet パッケージ アドレス:

受信 OYMLCN.WeChat は深くカプセル化されており、完全にテストされていないため、メッセージを受信するための SDK の受動的応答モジュールが再構築されています。

最大限に簡素化およびモジュール化されており、各モジュールは単体テストを完了しています (WeChat ドキュメントのサンプルコンテンツによる)。

次に、WeChatインターフェースの呼び出しモジュールが再構築され、基本的に完成した後にリリースされます。

JQuery に似た操作メソッドは、すべての機能が完成しテストされた後、OYMLCN.WeChat に統合されます。

主な使用方法:

var postModel = this.Request.GetQuery().IsValidRequest(Config);if (postModel == null)return Content("");return Content(new DemoHandler(WeChatRequest.Build(Config, postModel, this.Request.GetBody().ReadToEnd())).Result);
Config是基础接口配置OYMLCN.WeChat.Config的实例,参数依次为【公众平台微信号】、【AppId】、【AppSecret】、【Token】以及可选的【AESKey】。
    配置的参数顺序相对于以往版本有变更,若报错无法验证签名有效性则需要检查参数的赋值顺序。
Request.GetQuery()是对Request的扩展方法,封装在依赖包OYMLCN.Web当中,主要获取请求附带的参数。若在ASP.Net Web Api当中使用,需要安装兼容扩展包OYMLCN.Web.Api。
Request.GetBody()主要是获取请求的正文,相当于Request.Body,为了使代码兼容WebApi而设置的一个方法。
ReadToEnd()是Stream的扩展方法,主要是读取Stream中的文本内容。
IsValidRequest(Config)是Dictionary<string, string>的扩展方法,用于验证请求的有效性并获取PostModel参数。验证失败则返回null。
当前示例是使用Handler处理的。
public class DemoHandler : OYMLCN.WeChat.MessageHandler
{public DemoHandler(OYMLCN.WeChat.WeChatRequest request) : base(request) { }public override OYMLCN.WeChat.WeChatResponse DefaultResponseMessage(OYMLCN.WeChat.WeChatRequest request)
    {return OYMLCN.WeChat.WeChatResponse.ResponseText(request, "Success");
    }
}

要件が簡単であればHandlerを使わずに対応可能です。 使用方法は以下の通りです:

OYMLCN.WeChat.Config config = new OYMLCN.WeChat.Config("微信账号名", "AppId", "AppSecret", "Token", "AESKey");
OYMLCN.WeChat.PostModel postModel = this.Request.GetQuery().IsValidRequest(config);string body = this.Request.GetBody().ReadToEnd();
OYMLCN.WeChat.WeChatRequest request = OYMLCN.WeChat.WeChatRequest.Build(config, postModel, body);
OYMLCN.WeChat.WeChatResponse response = null;if (request.MessageType == OYMLCN.WeChat.WeChatRequestMessageType.Text)
    response = OYMLCN.WeChat.WeChatResponse.ResponseText(request, "我是测试");else if (request.MessageType == OYMLCN.WeChat.WeChatRequestMessageType.Event)if (request.EventType == OYMLCN.WeChat.WeChatRequestEventType.Event关注)
        response = OYMLCN.WeChat.WeChatResponse.ResponseText(request, "你来晚了");if (response != null)return Content(response.Result);return Content("");

単体テストコードが添付されています。そして基本的な使い方が示されています。

   1 using System;   2 using System.Collections.Generic;   3 using Microsoft.VisualStudio.TestTools.UnitTesting;   4 using System.Linq;   5    6 namespace OYMLCN.WeChat.Core.Test   7 {   8     [TestClass]   9     public class UnitTest  10     {  11         Config Config = new Config("wxName", "appId", "appSecret", "token", "aes");  12         PostModel PostModel = PostModel.Build(new Dictionary<string, string>()  13         {  14             {"nonce","1362870167" },  15             {"openid","oOk2XjhrbcHP3tGgzDGAVHppo3Bs" },  16             {"signature","7940891098b505c22f99b0e3708627ec715aa832" },  17             {"timestamp","1496218735" }  18         });  19   20         [TestMethod]  21         public void WeChatRequestTest()  22         {  23             string textMsg = @"<xml>  24  <ToUserName><![CDATA[toUser]]></ToUserName>  25  <FromUserName><![CDATA[fromUser]]></FromUserName>  26  <CreateTime>1348831860</CreateTime>  27  <MsgType><![CDATA[text]]></MsgType>  28  <Content><![CDATA[this is a test]]></Content>  29  <MsgId>1234567890123456</MsgId>  30  </xml>";  31   32             var request = WeChatRequest.Build(Config, PostModel, textMsg);  33             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Text);  34             Assert.AreEqual(request.ToUserName, "toUser");  35             Assert.AreEqual(request.FromUserName, "fromUser");  36             Assert.AreEqual(request.CreateTime, 1348831860);  37             Assert.AreEqual(request.MsgId, 1234567890123456);  38             Assert.AreEqual(request.MessageText.Content, "this is a test");  39   40             string imgMsg = @"<xml>  41 <ToUserName><![CDATA[toUser]]></ToUserName>  42 <FromUserName><![CDATA[fromUser]]></FromUserName>  43 <CreateTime>1348831860</CreateTime>  44 <MsgType><![CDATA[image]]></MsgType>  45 <PicUrl><![CDATA[this is a url]]></PicUrl>  46 <MediaId><![CDATA[media_id]]></MediaId>  47 <MsgId>1234567890123456</MsgId>  48 </xml>";  49             request = WeChatRequest.Build(Config, PostModel, imgMsg);  50             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Image);  51             Assert.AreEqual(request.ToUserName, "toUser");  52             Assert.AreEqual(request.FromUserName, "fromUser");  53             Assert.AreEqual(request.CreateTime, 1348831860);  54             Assert.AreEqual(request.MessageImage.PicUrl, "this is a url");  55             Assert.AreEqual(request.MessageImage.MediaId, "media_id");  56             Assert.AreEqual(request.MsgId, 1234567890123456);  57   58             string voiceMsg = @"<xml>  59 <ToUserName><![CDATA[toUser]]></ToUserName>  60 <FromUserName><![CDATA[fromUser]]></FromUserName>  61 <CreateTime>1357290913</CreateTime>  62 <MsgType><![CDATA[voice]]></MsgType>  63 <MediaId><![CDATA[media_id]]></MediaId>  64 <Format><![CDATA[Format]]></Format>  65 <MsgId>1234567890123456</MsgId>  66 </xml>";  67             request = WeChatRequest.Build(Config, PostModel, voiceMsg);  68             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Voice);  69             Assert.AreEqual(request.ToUserName, "toUser");  70             Assert.AreEqual(request.FromUserName, "fromUser");  71             Assert.AreEqual(request.CreateTime, 1357290913);  72             Assert.AreEqual(request.MessageVoice.MediaId, "media_id");  73             Assert.AreEqual(request.MessageVoice.Format, "Format");  74             Assert.AreEqual(request.MsgId, 1234567890123456);  75   76             voiceMsg = @"<xml>  77 <ToUserName><![CDATA[toUser]]></ToUserName>  78 <FromUserName><![CDATA[fromUser]]></FromUserName>  79 <CreateTime>1357290913</CreateTime>  80 <MsgType><![CDATA[voice]]></MsgType>  81 <MediaId><![CDATA[media_id]]></MediaId>  82 <Format><![CDATA[Format]]></Format>  83 <Recognition><![CDATA[腾讯微信团队]]></Recognition>  84 <MsgId>1234567890123456</MsgId>  85 </xml>";  86             request = WeChatRequest.Build(Config, PostModel, voiceMsg);  87             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Voice);  88             Assert.AreEqual(request.ToUserName, "toUser");  89             Assert.AreEqual(request.FromUserName, "fromUser");  90             Assert.AreEqual(request.CreateTime, 1357290913);  91             Assert.AreEqual(request.MessageVoice.MediaId, "media_id");  92             Assert.AreEqual(request.MessageVoice.Format, "Format");  93             Assert.AreEqual(request.MessageVoice.Recognition, "腾讯微信团队");  94             Assert.AreEqual(request.MsgId, 1234567890123456);  95   96             var videoMsg = @"<xml>  97 <ToUserName><![CDATA[toUser]]></ToUserName>  98 <FromUserName><![CDATA[fromUser]]></FromUserName>  99 <CreateTime>1357290913</CreateTime> 100 <MsgType><![CDATA[video]]></MsgType> 101 <MediaId><![CDATA[media_id]]></MediaId> 102 <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> 103 <MsgId>1234567890123456</MsgId> 104 </xml>"; 105             request = WeChatRequest.Build(Config, PostModel, videoMsg); 106             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Video); 107             Assert.AreEqual(request.ToUserName, "toUser"); 108             Assert.AreEqual(request.FromUserName, "fromUser"); 109             Assert.AreEqual(request.CreateTime, 1357290913); 110             Assert.AreEqual(request.MessageVideo.MediaId, "media_id"); 111             Assert.AreEqual(request.MessageVideo.ThumbMediaId, "thumb_media_id"); 112             Assert.AreEqual(request.MsgId, 1234567890123456); 113  114             videoMsg = @"<xml> 115 <ToUserName><![CDATA[toUser]]></ToUserName> 116 <FromUserName><![CDATA[fromUser]]></FromUserName> 117 <CreateTime>1357290913</CreateTime> 118 <MsgType><![CDATA[shortvideo]]></MsgType> 119 <MediaId><![CDATA[media_id]]></MediaId> 120 <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> 121 <MsgId>1234567890123456</MsgId> 122 </xml>"; 123             request = WeChatRequest.Build(Config, PostModel, videoMsg); 124             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.ShortVideo); 125             Assert.AreEqual(request.ToUserName, "toUser"); 126             Assert.AreEqual(request.FromUserName, "fromUser"); 127             Assert.AreEqual(request.CreateTime, 1357290913); 128             Assert.AreEqual(request.MessageVideo.MediaId, "media_id"); 129             Assert.AreEqual(request.MessageVideo.ThumbMediaId, "thumb_media_id"); 130             Assert.AreEqual(request.MsgId, 1234567890123456); 131  132             var locationMsg = @"<xml> 133 <ToUserName><![CDATA[toUser]]></ToUserName> 134 <FromUserName><![CDATA[fromUser]]></FromUserName> 135 <CreateTime>1351776360</CreateTime> 136 <MsgType><![CDATA[location]]></MsgType> 137 <Location_X>23.134521</Location_X> 138 <Location_Y>113.358803</Location_Y> 139 <Scale>20</Scale> 140 <Label><![CDATA[位置信息]]></Label> 141 <MsgId>1234567890123456</MsgId> 142 </xml>"; 143             request = WeChatRequest.Build(Config, PostModel, locationMsg); 144             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Location); 145             Assert.AreEqual(request.ToUserName, "toUser"); 146             Assert.AreEqual(request.FromUserName, "fromUser"); 147             Assert.AreEqual(request.CreateTime, 1351776360); 148             Assert.AreEqual(request.MessageLocation.Location_X, 23.134521); 149             Assert.AreEqual(request.MessageLocation.Location_Y, 113.358803); 150             Assert.AreEqual(request.MessageLocation.Scale, 20); 151             Assert.AreEqual(request.MessageLocation.Label, "位置信息"); 152             Assert.AreEqual(request.MsgId, 1234567890123456); 153  154             var linkMsg = @"<xml> 155 <ToUserName><![CDATA[toUser]]></ToUserName> 156 <FromUserName><![CDATA[fromUser]]></FromUserName> 157 <CreateTime>1351776360</CreateTime> 158 <MsgType><![CDATA[link]]></MsgType> 159 <Title><![CDATA[公众平台官网链接]]></Title> 160 <Description><![CDATA[公众平台官网链接]]></Description> 161 <Url><![CDATA[url]]></Url> 162 <MsgId>1234567890123456</MsgId> 163 </xml>"; 164             request = WeChatRequest.Build(Config, PostModel, linkMsg); 165             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Link); 166             Assert.AreEqual(request.ToUserName, "toUser"); 167             Assert.AreEqual(request.FromUserName, "fromUser"); 168             Assert.AreEqual(request.CreateTime, 1351776360); 169             Assert.AreEqual(request.MessageLink.Title, "公众平台官网链接"); 170             Assert.AreEqual(request.MessageLink.Description, "公众平台官网链接"); 171             Assert.AreEqual(request.MessageLink.Url, "url"); 172             Assert.AreEqual(request.MsgId, 1234567890123456); 173  174         } 175  176         [TestMethod] 177         public void WeChatRequestEventTest() 178         { 179             var subscribeEvent = @"<xml> 180 <ToUserName><![CDATA[toUser]]></ToUserName> 181 <FromUserName><![CDATA[FromUser]]></FromUserName> 182 <CreateTime>123456789</CreateTime> 183 <MsgType><![CDATA[event]]></MsgType> 184 <Event><![CDATA[subscribe]]></Event> 185 </xml>"; 186             var request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 187             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 188             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event关注); 189             Assert.AreEqual(request.ToUserName, "toUser"); 190             Assert.AreEqual(request.FromUserName, "FromUser"); 191             Assert.AreEqual(request.CreateTime, 123456789); 192             Assert.IsTrue(request.Event关注); 193  194             subscribeEvent = @"<xml> 195 <ToUserName><![CDATA[toUser]]></ToUserName> 196 <FromUserName><![CDATA[FromUser]]></FromUserName> 197 <CreateTime>123456789</CreateTime> 198 <MsgType><![CDATA[event]]></MsgType> 199 <Event><![CDATA[unsubscribe]]></Event> 200 </xml>"; 201             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 202             Assert.IsTrue(request.Event取消关注); 203  204             subscribeEvent = @"<xml> 205 <ToUserName><![CDATA[toUser]]></ToUserName> 206 <FromUserName><![CDATA[FromUser]]></FromUserName> 207 <CreateTime>123456789</CreateTime> 208 <MsgType><![CDATA[event]]></MsgType> 209 <Event><![CDATA[subscribe]]></Event> 210 <EventKey><![CDATA[qrscene_123123]]></EventKey> 211 <Ticket><![CDATA[TICKET]]></Ticket> 212 </xml>"; 213             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 214             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 215             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event关注); 216             Assert.AreEqual(request.ToUserName, "toUser"); 217             Assert.AreEqual(request.FromUserName, "FromUser"); 218             Assert.AreEqual(request.CreateTime, 123456789); 219             Assert.AreEqual(request.Event扫描带参数二维码.EventKey, "qrscene_123123"); 220             Assert.AreEqual(request.Event扫描带参数二维码.SceneId, "123123"); 221             Assert.AreEqual(request.Event扫描带参数二维码.Ticket, "TICKET"); 222  223             subscribeEvent = @"<xml> 224 <ToUserName><![CDATA[toUser]]></ToUserName> 225 <FromUserName><![CDATA[FromUser]]></FromUserName> 226 <CreateTime>123456789</CreateTime> 227 <MsgType><![CDATA[event]]></MsgType> 228 <Event><![CDATA[SCAN]]></Event> 229 <EventKey><![CDATA[SCENE_VALUE]]></EventKey> 230 <Ticket><![CDATA[TICKET]]></Ticket> 231 </xml>"; 232             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 233             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 234             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event扫描带参数二维码); 235             Assert.AreEqual(request.ToUserName, "toUser"); 236             Assert.AreEqual(request.FromUserName, "FromUser"); 237             Assert.AreEqual(request.CreateTime, 123456789); 238             Assert.AreEqual(request.Event扫描带参数二维码.EventKey, "SCENE_VALUE"); 239             Assert.AreEqual(request.Event扫描带参数二维码.SceneId, "SCENE_VALUE"); 240             Assert.AreEqual(request.Event扫描带参数二维码.Ticket, "TICKET"); 241  242  243             var locationEvent = @"<xml> 244 <ToUserName><![CDATA[toUser]]></ToUserName> 245 <FromUserName><![CDATA[fromUser]]></FromUserName> 246 <CreateTime>123456789</CreateTime> 247 <MsgType><![CDATA[event]]></MsgType> 248 <Event><![CDATA[LOCATION]]></Event> 249 <Latitude>23.137466</Latitude> 250 <Longitude>113.352425</Longitude> 251 <Precision>119.385040</Precision> 252 </xml>"; 253             request = WeChatRequest.Build(Config, PostModel, locationEvent); 254             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 255             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event上报地理位置); 256             Assert.AreEqual(request.ToUserName, "toUser"); 257             Assert.AreEqual(request.FromUserName, "fromUser"); 258             Assert.AreEqual(request.CreateTime, 123456789); 259             Assert.AreEqual(request.Event上报地理位置.Latitude, 23.137466); 260             Assert.AreEqual(request.Event上报地理位置.Longitude, 113.352425); 261             Assert.AreEqual(request.Event上报地理位置.Precision, 119.385040); 262  263             var clickEvent = @"<xml> 264 <ToUserName><![CDATA[toUser]]></ToUserName> 265 <FromUserName><![CDATA[FromUser]]></FromUserName> 266 <CreateTime>123456789</CreateTime> 267 <MsgType><![CDATA[event]]></MsgType> 268 <Event><![CDATA[CLICK]]></Event> 269 <EventKey><![CDATA[EVENTKEY]]></EventKey> 270 </xml>"; 271             request = WeChatRequest.Build(Config, PostModel, clickEvent); 272             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 273             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event点击自定义菜单); 274             Assert.AreEqual(request.ToUserName, "toUser"); 275             Assert.AreEqual(request.FromUserName, "FromUser"); 276             Assert.AreEqual(request.CreateTime, 123456789); 277             Assert.AreEqual(request.Event点击自定义菜单.EventKey, "EVENTKEY"); 278  279             var linkEvent = @"<xml> 280 <ToUserName><![CDATA[toUser]]></ToUserName> 281 <FromUserName><![CDATA[FromUser]]></FromUserName> 282 <CreateTime>123456789</CreateTime> 283 <MsgType><![CDATA[event]]></MsgType> 284 <Event><![CDATA[VIEW]]></Event> 285 <EventKey><![CDATA[www.qq.com]]></EventKey> 286 <MenuId>123</MenuId> 287 </xml>"; 288             request = WeChatRequest.Build(Config, PostModel, linkEvent); 289             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 290             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event点击菜单跳转链接); 291             Assert.AreEqual(request.ToUserName, "toUser"); 292             Assert.AreEqual(request.FromUserName, "FromUser"); 293             Assert.AreEqual(request.CreateTime, 123456789); 294             Assert.AreEqual(request.Event点击菜单跳转链接.Url, "www.qq.com"); 295             Assert.AreEqual(request.Event点击菜单跳转链接.MenuId, 123); 296  297         } 298  299         [TestMethod] 300         public void WeChatRequestPushEventTest() 301         { 302             #region 模板消息发送结果 303             var tempplatePush = @"<xml> 304 <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName> 305 <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName> 306 <CreateTime>1395658920</CreateTime> 307 <MsgType><![CDATA[event]]></MsgType> 308 <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event> 309 <MsgID>200163836</MsgID> 310 <Status><![CDATA[success]]></Status> 311 </xml>"; 312             var request = WeChatRequest.Build(Config, PostModel, tempplatePush); 313             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 314             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 315             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 316             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 317             Assert.AreEqual(request.CreateTime, 1395658920); 318             Assert.AreEqual(request.Push模板消息发送结果.Status, "success"); 319             Assert.IsTrue(request.Push模板消息发送结果.Success); 320  321             tempplatePush = @"<xml> 322 <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName> 323 <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName> 324 <CreateTime>1395658984</CreateTime> 325 <MsgType><![CDATA[event]]></MsgType> 326 <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event> 327 <MsgID>200163840</MsgID> 328 <Status><![CDATA[failed:userblock]]></Status> 329 </xml>"; 330             request = WeChatRequest.Build(Config, PostModel, tempplatePush); 331             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 332             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 333             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 334             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 335             Assert.AreEqual(request.CreateTime, 1395658984); 336             Assert.AreEqual(request.Push模板消息发送结果.Status, "failed:userblock"); 337             Assert.IsFalse(request.Push模板消息发送结果.Success); 338             tempplatePush = @"<xml> 339 <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName> 340 <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName> 341 <CreateTime>1395658984</CreateTime> 342 <MsgType><![CDATA[event]]></MsgType> 343 <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event> 344 <MsgID>200163840</MsgID> 345 <Status><![CDATA[failed:system failed]]></Status> 346 </xml>"; 347             request = WeChatRequest.Build(Config, PostModel, tempplatePush); 348             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 349             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 350             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 351             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 352             Assert.AreEqual(request.CreateTime, 1395658984); 353             Assert.AreEqual(request.Push模板消息发送结果.Status, "failed:system failed"); 354             Assert.IsFalse(request.Push模板消息发送结果.Success); 355             #endregion 356  357             #region 群发结果 358             var massResultPush = @"<xml> 359 <ToUserName><![CDATA[gh_4d00ed8d6399]]></ToUserName> 360 <FromUserName><![CDATA[oV5CrjpxgaGXNHIQigzNlgLTnwic]]></FromUserName> 361 <CreateTime>1481013459</CreateTime> 362 <MsgType><![CDATA[event]]></MsgType> 363 <Event><![CDATA[MASSSENDJOBFINISH]]></Event> 364 <MsgID>1000001625</MsgID> 365 <Status><![CDATA[err(30003)]]></Status> 366 <TotalCount>0</TotalCount> 367 <FilterCount>0</FilterCount> 368 <SentCount>0</SentCount> 369 <ErrorCount>0</ErrorCount> 370 <CopyrightCheckResult> 371 <Count>2</Count> 372 <ResultList> 373 <item> 374 <ArticleIdx>1</ArticleIdx> 375 <UserDeclareState>0</UserDeclareState> 376 <AuditState>2</AuditState> 377 <OriginalArticleUrl><![CDATA[Url_1]]></OriginalArticleUrl> 378 <OriginalArticleType>1</OriginalArticleType> 379 <CanReprint>1</CanReprint> 380 <NeedReplaceContent>1</NeedReplaceContent> 381 <NeedShowReprintSource>1</NeedShowReprintSource> 382 </item> 383 <item> 384 <ArticleIdx>2</ArticleIdx> 385 <UserDeclareState>0</UserDeclareState> 386 <AuditState>2</AuditState> 387 <OriginalArticleUrl><![CDATA[Url_2]]></OriginalArticleUrl> 388 <OriginalArticleType>1</OriginalArticleType> 389 <CanReprint>1</CanReprint> 390 <NeedReplaceContent>1</NeedReplaceContent> 391 <NeedShowReprintSource>1</NeedShowReprintSource> 392 </item> 393 </ResultList> 394 <CheckState>2</CheckState> 395 </CopyrightCheckResult> 396 </xml>"; 397             request = WeChatRequest.Build(Config, PostModel, massResultPush); 398             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 399             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push群发结果); 400             Assert.AreEqual(request.ToUserName, "gh_4d00ed8d6399"); 401             Assert.AreEqual(request.FromUserName, "oV5CrjpxgaGXNHIQigzNlgLTnwic"); 402             Assert.AreEqual(request.CreateTime, 1481013459); 403             Assert.AreEqual(request.Push群发结果.MsgID, 1000001625); 404             Assert.AreEqual(request.Push群发结果.Status, "err(30003)"); 405             Assert.IsFalse(request.Push群发结果.Success); 406             Assert.AreEqual(request.Push群发结果.ErrorReason, "原创校验被判定为转载文且用户选择了被判为转载就不群发"); 407             Assert.AreEqual(request.Push群发结果.TotalCount, 0); 408             Assert.AreEqual(request.Push群发结果.FilterCount, 0); 409             Assert.AreEqual(request.Push群发结果.SentCount, 0); 410             Assert.AreEqual(request.Push群发结果.ErrorCount, 0); 411             var checkResult = request.Push群发结果.CopyrightCheckResult; 412             var item = checkResult.First(); 413             Assert.AreEqual(item.ArticleIdx, 1); 414             Assert.AreEqual(item.UserDeclareState, 0); 415             Assert.AreEqual(item.AuditState, 2); 416             Assert.AreEqual(item.OriginalArticleUrl, "Url_1"); 417             Assert.AreEqual(item.OriginalArticleType, 1); 418             Assert.AreEqual(item.CanReprint, true); 419             Assert.AreEqual(item.NeedReplaceContent, true); 420             Assert.AreEqual(item.NeedShowReprintSource, true); 421             item = checkResult.Last(); 422             Assert.AreEqual(item.ArticleIdx, 2); 423             Assert.AreEqual(item.UserDeclareState, 0); 424             Assert.AreEqual(item.AuditState, 2); 425             Assert.AreEqual(item.OriginalArticleUrl, "Url_2"); 426             Assert.AreEqual(item.OriginalArticleType, 1); 427             Assert.AreEqual(item.CanReprint, true); 428             Assert.AreEqual(item.NeedReplaceContent, true); 429             Assert.AreEqual(item.NeedShowReprintSource, true); 430             Assert.AreEqual(request.Push群发结果.CheckState, 2); 431             #endregion 432  433             #region 菜单相关推送 434             var scancodePush = @"<xml> 435 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 436 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 437 <CreateTime>1408090502</CreateTime> 438 <MsgType><![CDATA[event]]></MsgType> 439 <Event><![CDATA[scancode_push]]></Event> 440 <EventKey><![CDATA[6]]></EventKey> 441 <ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType> 442 <ScanResult><![CDATA[1]]></ScanResult> 443 </ScanCodeInfo> 444 </xml>"; 445             request = WeChatRequest.Build(Config, PostModel, scancodePush); 446             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 447             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush扫码推事件); 448             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 449             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 450             Assert.AreEqual(request.CreateTime, 1408090502); 451             Assert.AreEqual(request.MenuPush扫码推事件.EventKey, "6"); 452             Assert.AreEqual(request.MenuPush扫码推事件.ScanType, "qrcode"); 453             Assert.AreEqual(request.MenuPush扫码推事件.ScanResult, "1"); 454             scancodePush = @"<xml> 455 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 456 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 457 <CreateTime>1408090606</CreateTime> 458 <MsgType><![CDATA[event]]></MsgType> 459 <Event><![CDATA[scancode_waitmsg]]></Event> 460 <EventKey><![CDATA[6]]></EventKey> 461 <ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType> 462 <ScanResult><![CDATA[2]]></ScanResult> 463 </ScanCodeInfo> 464 </xml>"; 465             request = WeChatRequest.Build(Config, PostModel, scancodePush); 466             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 467             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush扫码推等待事件); 468             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 469             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 470             Assert.AreEqual(request.CreateTime, 1408090606); 471             Assert.AreEqual(request.MenuPush扫码推等待事件.EventKey, "6"); 472             Assert.AreEqual(request.MenuPush扫码推等待事件.ScanType, "qrcode"); 473             Assert.AreEqual(request.MenuPush扫码推等待事件.ScanResult, "2"); 474  475             var imagePush = @"<xml> 476 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 477 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 478 <CreateTime>1408090651</CreateTime> 479 <MsgType><![CDATA[event]]></MsgType> 480 <Event><![CDATA[pic_sysphoto]]></Event> 481 <EventKey><![CDATA[6]]></EventKey> 482 <SendPicsInfo><Count>1</Count> 483 <PicList><item><PicMd5Sum><![CDATA[1b5f7c23b5bf75682a53e7b6d163e185]]></PicMd5Sum> 484 </item> 485 </PicList> 486 </SendPicsInfo> 487 </xml>"; 488             request = WeChatRequest.Build(Config, PostModel, imagePush); 489             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 490             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush系统拍照发图); 491             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 492             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 493             Assert.AreEqual(request.CreateTime, 1408090651); 494             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 495             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 496             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "1b5f7c23b5bf75682a53e7b6d163e185" }); 497             imagePush = @"<xml> 498 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 499 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 500 <CreateTime>1408090816</CreateTime> 501 <MsgType><![CDATA[event]]></MsgType> 502 <Event><![CDATA[pic_photo_or_album]]></Event> 503 <EventKey><![CDATA[6]]></EventKey> 504 <SendPicsInfo><Count>1</Count> 505 <PicList><item><PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum> 506 </item> 507 </PicList> 508 </SendPicsInfo> 509 </xml>"; 510             request = WeChatRequest.Build(Config, PostModel, imagePush); 511             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 512             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush拍照或者相册发图); 513             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 514             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 515             Assert.AreEqual(request.CreateTime, 1408090816); 516             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 517             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 518             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "5a75aaca956d97be686719218f275c6b" }); 519             imagePush = @"<xml> 520 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 521 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 522 <CreateTime>1408090816</CreateTime> 523 <MsgType><![CDATA[event]]></MsgType> 524 <Event><![CDATA[pic_weixin]]></Event> 525 <EventKey><![CDATA[6]]></EventKey> 526 <SendPicsInfo><Count>1</Count> 527 <PicList><item><PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum> 528 </item> 529 </PicList> 530 </SendPicsInfo> 531 </xml>"; 532             request = WeChatRequest.Build(Config, PostModel, imagePush); 533             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 534             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush微信相册发图); 535             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 536             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 537             Assert.AreEqual(request.CreateTime, 1408090816); 538             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 539             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 540             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "5a75aaca956d97be686719218f275c6b" }); 541  542             var locationPush = @"<xml> 543 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 544 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 545 <CreateTime>1408091189</CreateTime> 546 <MsgType><![CDATA[event]]></MsgType> 547 <Event><![CDATA[location_select]]></Event> 548 <EventKey><![CDATA[6]]></EventKey> 549 <SendLocationInfo><Location_X><![CDATA[23]]></Location_X> 550 <Location_Y><![CDATA[113]]></Location_Y> 551 <Scale><![CDATA[15]]></Scale> 552 <Label><![CDATA[广州市海珠区客村艺苑路 106号]]></Label> 553 <Poiname><![CDATA[]]></Poiname> 554 </SendLocationInfo> 555 </xml>"; 556             request = WeChatRequest.Build(Config, PostModel, locationPush); 557             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 558             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush位置选择); 559             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 560             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 561             Assert.AreEqual(request.CreateTime, 1408091189); 562             Assert.AreEqual(request.MenuPush位置选择.EventKey, "6"); 563             Assert.AreEqual(request.MenuPush位置选择.Location_X, 23); 564             Assert.AreEqual(request.MenuPush位置选择.Location_Y, 113); 565             Assert.AreEqual(request.MenuPush位置选择.Scale, 15); 566             Assert.AreEqual(request.MenuPush位置选择.Label, "广州市海珠区客村艺苑路 106号"); 567             Assert.AreEqual(request.MenuPush位置选择.Poiname, ""); 568             #endregion 569         } 570  571         [TestMethod] 572         public void WeChatResponseTest() 573         { 574             var demoMsg = @"<xml> 575 <ToUserName><![CDATA[AppId]]></ToUserName> 576 <FromUserName><![CDATA[OpenId]]></FromUserName> 577 <CreateTime>12345678</CreateTime> 578 </xml>"; 579             var request = WeChatRequest.Build(Config, PostModel, demoMsg); 580             Assert.AreEqual(WeChatResponse.ResponseText(request, "你好").Source, @"<xml> 581 <ToUserName><![CDATA[OpenId]]></ToUserName> 582 <FromUserName><![CDATA[AppId]]></FromUserName> 583 <CreateTime>12345678</CreateTime> 584 <MsgType><![CDATA[text]]></MsgType> 585 <Content><![CDATA[你好]]></Content> 586 </xml>".RemoveBr()); 587             Assert.AreEqual(WeChatResponse.ResponseImage(request, "media_id").Source, @"<xml> 588 <ToUserName><![CDATA[OpenId]]></ToUserName> 589 <FromUserName><![CDATA[AppId]]></FromUserName> 590 <CreateTime>12345678</CreateTime> 591 <MsgType><![CDATA[image]]></MsgType> 592 <Image> 593 <MediaId><![CDATA[media_id]]></MediaId> 594 </Image> 595 </xml>".RemoveBr()); 596             Assert.AreEqual(WeChatResponse.ResponseVoice(request, "media_id").Source, @"<xml> 597 <ToUserName><![CDATA[OpenId]]></ToUserName> 598 <FromUserName><![CDATA[AppId]]></FromUserName> 599 <CreateTime>12345678</CreateTime> 600 <MsgType><![CDATA[voice]]></MsgType> 601 <Voice> 602 <MediaId><![CDATA[media_id]]></MediaId> 603 </Voice> 604 </xml>".RemoveBr()); 605             Assert.AreEqual(WeChatResponse.ResponseVideo(request, "media_id", "title", "description").Source, @"<xml> 606 <ToUserName><![CDATA[OpenId]]></ToUserName> 607 <FromUserName><![CDATA[AppId]]></FromUserName> 608 <CreateTime>12345678</CreateTime> 609 <MsgType><![CDATA[video]]></MsgType> 610 <Video> 611 <MediaId><![CDATA[media_id]]></MediaId> 612 <Title><![CDATA[title]]></Title> 613 <Description><![CDATA[description]]></Description> 614 </Video> 615 </xml>".RemoveBr()); 616             Assert.AreEqual(WeChatResponse.ResponseMusic(request, "media_id", "MUSIC_Url", "TITLE", "DESCRIPTION", "HQ_MUSIC_Url").Source, @"<xml> 617 <ToUserName><![CDATA[OpenId]]></ToUserName> 618 <FromUserName><![CDATA[AppId]]></FromUserName> 619 <CreateTime>12345678</CreateTime> 620 <MsgType><![CDATA[music]]></MsgType> 621 <Music> 622 <Title><![CDATA[TITLE]]></Title> 623 <Description><![CDATA[DESCRIPTION]]></Description> 624 <MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl> 625 <HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl> 626 <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId> 627 </Music> 628 </xml>".RemoveBr()); 629  630             Assert.AreEqual(WeChatResponse.ResponseNews(request, 631                 new WeChatResponse.Article("title1", "description1", "picurl", "url"), 632                 new WeChatResponse.Article("title", "description", "picurl", "url") 633                 ).Source, @"<xml> 634 <ToUserName><![CDATA[OpenId]]></ToUserName> 635 <FromUserName><![CDATA[AppId]]></FromUserName> 636 <CreateTime>12345678</CreateTime> 637 <MsgType><![CDATA[news]]></MsgType> 638 <ArticleCount>2</ArticleCount> 639 <Articles> 640 <item> 641 <Title><![CDATA[title1]]></Title> 642 <Description><![CDATA[description1]]></Description> 643 <PicUrl><![CDATA[picurl]]></PicUrl> 644 <Url><![CDATA[url]]></Url> 645 </item> 646 <item> 647 <Title><![CDATA[title]]></Title> 648 <Description><![CDATA[description]]></Description> 649 <PicUrl><![CDATA[picurl]]></PicUrl> 650 <Url><![CDATA[url]]></Url> 651 </item> 652 </Articles> 653 </xml>".RemoveBr()); 654             Assert.AreEqual(WeChatResponse.TransferToCustomerService(request).Source, @"<xml> 655 <ToUserName><![CDATA[OpenId]]></ToUserName> 656 <FromUserName><![CDATA[AppId]]></FromUserName> 657 <CreateTime>12345678</CreateTime> 658 <MsgType><![CDATA[transfer_customer_service]]></MsgType> 659 </xml>".RemoveBr()); 660             Assert.AreEqual(WeChatResponse.TransferToCustomerService(request, "test1").Source, @"<xml> 661 <ToUserName><![CDATA[OpenId]]></ToUserName> 662 <FromUserName><![CDATA[AppId]]></FromUserName> 663 <CreateTime>12345678</CreateTime> 664 <MsgType><![CDATA[transfer_customer_service]]></MsgType> 665 <TransInfo><KfAccount><![CDATA[test1@test]]></KfAccount></TransInfo> 666 </xml>".RemoveBr()); 667         } 668  669         [TestMethod] 670         public void WeChatExtensionTest() 671         { 672             var demoQuery = new Dictionary<string, string>() 673             { 674                 {"nonce","1362870167"}, 675                 {"openid","oOk2XjhrbcHP3tGgzDGAVHppo3Bs"}, 676                 {"signature","7940891098b505c22f99b0e3708627ec715aa832"}, 677                 {"timestamp","1496218735"}, 678                 {"echostr","demoEchostr"} 679             }; 680             Assert.IsNotNull(demoQuery.IsValidRequest(Config)); 681             Assert.AreEqual(demoQuery.ConfigVerify(Config), "demoEchostr"); 682             demoQuery = new Dictionary<string, string>(); 683             Assert.IsNull(demoQuery.IsValidRequest(Config)); 684             Assert.AreEqual(demoQuery.ConfigVerify(Config), ""); 685         } 686  687         [TestMethod] 688         public void WeChatHanddlerTest() 689         { 690             string demoMsg = @"<xml> 691  <ToUserName><![CDATA[toUser]]></ToUserName> 692  <FromUserName><![CDATA[fromUser]]></FromUserName> 693  <CreateTime>1348831860</CreateTime> 694  <MsgType><![CDATA[test]]></MsgType> 695  <Content><![CDATA[this is a test]]></Content> 696  <MsgId>1234567890123456</MsgId> 697  </xml>"; 698             bool IsOkReponse(HandlerDemo demo, string text) => demo.Result.ToXDocument().SelectValue("Content").Equals(text); 699  700             var request = WeChatRequest.Build(Config, PostModel, demoMsg); 701             var demoHandler = new HandlerDemo(request); 702             Assert.AreEqual(demoHandler.Result, "");//未支持消息,直接回复空字符串 703  704             #region 微信消息 705             demoMsg = @"<xml> 706  <ToUserName><![CDATA[toUser]]></ToUserName> 707  <FromUserName><![CDATA[fromUser]]></FromUserName> 708  <CreateTime>1348831860</CreateTime> 709  <MsgType><![CDATA[text]]></MsgType> 710  <Content><![CDATA[this is a test]]></Content> 711  <MsgId>1234567890123456</MsgId> 712  </xml>"; 713             request = WeChatRequest.Build(Config, PostModel, demoMsg); 714             demoHandler = new HandlerDemo(request); 715             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageText")); 716  717             demoMsg = @"<xml> 718 <ToUserName><![CDATA[toUser]]></ToUserName> 719 <FromUserName><![CDATA[fromUser]]></FromUserName> 720 <CreateTime>1348831860</CreateTime> 721 <MsgType><![CDATA[image]]></MsgType> 722 <PicUrl><![CDATA[this is a url]]></PicUrl> 723 <MediaId><![CDATA[media_id]]></MediaId> 724 <MsgId>1234567890123456</MsgId> 725 </xml>"; 726             request = WeChatRequest.Build(Config, PostModel, demoMsg); 727             demoHandler = new HandlerDemo(request); 728             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageImage")); 729  730             demoMsg = @"<xml> 731 <ToUserName><![CDATA[toUser]]></ToUserName> 732 <FromUserName><![CDATA[fromUser]]></FromUserName> 733 <CreateTime>1357290913</CreateTime> 734 <MsgType><![CDATA[voice]]></MsgType> 735 <MediaId><![CDATA[media_id]]></MediaId> 736 <Format><![CDATA[Format]]></Format> 737 <Recognition><![CDATA[腾讯微信团队]]></Recognition> 738 <MsgId>1234567890123456</MsgId> 739 </xml>"; 740             request = WeChatRequest.Build(Config, PostModel, demoMsg); 741             demoHandler = new HandlerDemo(request); 742             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVoice")); 743  744             demoMsg = @"<xml> 745 <ToUserName><![CDATA[toUser]]></ToUserName> 746 <FromUserName><![CDATA[fromUser]]></FromUserName> 747 <CreateTime>1357290913</CreateTime> 748 <MsgType><![CDATA[video]]></MsgType> 749 <MediaId><![CDATA[media_id]]></MediaId> 750 <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> 751 <MsgId>1234567890123456</MsgId> 752 </xml>"; 753             request = WeChatRequest.Build(Config, PostModel, demoMsg); 754             demoHandler = new HandlerDemo(request); 755             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVideo")); 756  757             demoMsg = @"<xml> 758 <ToUserName><![CDATA[toUser]]></ToUserName> 759 <FromUserName><![CDATA[fromUser]]></FromUserName> 760 <CreateTime>1357290913</CreateTime> 761 <MsgType><![CDATA[shortvideo]]></MsgType> 762 <MediaId><![CDATA[media_id]]></MediaId> 763 <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> 764 <MsgId>1234567890123456</MsgId> 765 </xml>"; 766             request = WeChatRequest.Build(Config, PostModel, demoMsg); 767             demoHandler = new HandlerDemo(request); 768             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageShortVideo")); 769  770             demoMsg = @"<xml> 771 <ToUserName><![CDATA[toUser]]></ToUserName> 772 <FromUserName><![CDATA[fromUser]]></FromUserName> 773 <CreateTime>1351776360</CreateTime> 774 <MsgType><![CDATA[location]]></MsgType> 775 <Location_X>23.134521</Location_X> 776 <Location_Y>113.358803</Location_Y> 777 <Scale>20</Scale> 778 <Label><![CDATA[位置信息]]></Label> 779 <MsgId>1234567890123456</MsgId> 780 </xml>"; 781             request = WeChatRequest.Build(Config, PostModel, demoMsg); 782             demoHandler = new HandlerDemo(request); 783             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageLocaltion")); 784  785             demoMsg = @"<xml> 786 <ToUserName><![CDATA[toUser]]></ToUserName> 787 <FromUserName><![CDATA[fromUser]]></FromUserName> 788 <CreateTime>1351776360</CreateTime> 789 <MsgType><![CDATA[link]]></MsgType> 790 <Title><![CDATA[公众平台官网链接]]></Title> 791 <Description><![CDATA[公众平台官网链接]]></Description> 792 <Url><![CDATA[url]]></Url> 793 <MsgId>1234567890123456</MsgId> 794 </xml>"; 795             request = WeChatRequest.Build(Config, PostModel, demoMsg); 796             demoHandler = new HandlerDemo(request); 797             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageLink")); 798             #endregion 799  800             #region 事件消息 801             demoMsg = @"<xml> 802 <ToUserName><![CDATA[toUser]]></ToUserName> 803 <FromUserName><![CDATA[FromUser]]></FromUserName> 804 <CreateTime>123456789</CreateTime> 805 <MsgType><![CDATA[event]]></MsgType> 806 <Event><![CDATA[subscribe]]></Event> 807 </xml>"; 808             request = WeChatRequest.Build(Config, PostModel, demoMsg); 809             demoHandler = new HandlerDemo(request); 810             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注")); 811  812  813             demoMsg = @"<xml> 814 <ToUserName><![CDATA[toUser]]></ToUserName> 815 <FromUserName><![CDATA[FromUser]]></FromUserName> 816 <CreateTime>123456789</CreateTime> 817 <MsgType><![CDATA[event]]></MsgType> 818 <Event><![CDATA[unsubscribe]]></Event> 819 </xml>"; 820             request = WeChatRequest.Build(Config, PostModel, demoMsg); 821             demoHandler = new HandlerDemo(request); 822             Assert.ThrowsException<NotImplementedException>(() => { string result = demoHandler.Result; }, "OnEvent取消关注"); 823  824             demoMsg = @"<xml> 825 <ToUserName><![CDATA[toUser]]></ToUserName> 826 <FromUserName><![CDATA[FromUser]]></FromUserName> 827 <CreateTime>123456789</CreateTime> 828 <MsgType><![CDATA[event]]></MsgType> 829 <Event><![CDATA[subscribe]]></Event> 830 <EventKey><![CDATA[qrscene_123123]]></EventKey> 831 <Ticket><![CDATA[TICKET]]></Ticket> 832 </xml>"; 833             request = WeChatRequest.Build(Config, PostModel, demoMsg); 834             demoHandler = new HandlerDemo(request); 835             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注-扫描带参数二维码")); 836  837             demoMsg = @"<xml> 838 <ToUserName><![CDATA[toUser]]></ToUserName> 839 <FromUserName><![CDATA[FromUser]]></FromUserName> 840 <CreateTime>123456789</CreateTime> 841 <MsgType><![CDATA[event]]></MsgType> 842 <Event><![CDATA[SCAN]]></Event> 843 <EventKey><![CDATA[SCENE_VALUE]]></EventKey> 844 <Ticket><![CDATA[TICKET]]></Ticket> 845 </xml>"; 846             request = WeChatRequest.Build(Config, PostModel, demoMsg); 847             demoHandler = new HandlerDemo(request); 848             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent扫描带参数二维码")); 849  850             demoMsg = @"<xml> 851 <ToUserName><![CDATA[toUser]]></ToUserName> 852 <FromUserName><![CDATA[fromUser]]></FromUserName> 853 <CreateTime>123456789</CreateTime> 854 <MsgType><![CDATA[event]]></MsgType> 855 <Event><![CDATA[LOCATION]]></Event> 856 <Latitude>23.137466</Latitude> 857 <Longitude>113.352425</Longitude> 858 <Precision>119.385040</Precision> 859 </xml>"; 860             request = WeChatRequest.Build(Config, PostModel, demoMsg); 861             demoHandler = new HandlerDemo(request); 862             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent上报地理位置")); 863  864             demoMsg = @"<xml> 865 <ToUserName><![CDATA[toUser]]></ToUserName> 866 <FromUserName><![CDATA[FromUser]]></FromUserName> 867 <CreateTime>123456789</CreateTime> 868 <MsgType><![CDATA[event]]></MsgType> 869 <Event><![CDATA[CLICK]]></Event> 870 <EventKey><![CDATA[EVENTKEY]]></EventKey> 871 </xml>"; 872             request = WeChatRequest.Build(Config, PostModel, demoMsg); 873             demoHandler = new HandlerDemo(request); 874             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent点击自定义菜单")); 875  876             demoMsg = @"<xml> 877 <ToUserName><![CDATA[toUser]]></ToUserName> 878 <FromUserName><![CDATA[FromUser]]></FromUserName> 879 <CreateTime>123456789</CreateTime> 880 <MsgType><![CDATA[event]]></MsgType> 881 <Event><![CDATA[VIEW]]></Event> 882 <EventKey><![CDATA[www.qq.com]]></EventKey> 883 <MenuId>123</MenuId> 884 </xml>"; 885             request = WeChatRequest.Build(Config, PostModel, demoMsg); 886             demoHandler = new HandlerDemo(request); 887             Assert.ThrowsException<NotImplementedException>(() => { string result = demoHandler.Result; }, "OnEvent点击菜单跳转链接"); 888             #endregion 889  890             #region 微信服务相关 891             demoMsg = @"<xml> 892 <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName> 893 <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName> 894 <CreateTime>1395658920</CreateTime> 895 <MsgType><![CDATA[event]]></MsgType> 896 <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event> 897 <MsgID>200163836</MsgID> 898 <Status><![CDATA[success]]></Status> 899 </xml>"; 900             request = WeChatRequest.Build(Config, PostModel, demoMsg); 901             demoHandler = new HandlerDemo(request); 902             Assert.ThrowsException<NotImplementedException>(() => { string result = demoHandler.Result; }, "OnPush模板消息发送结果"); 903  904             demoMsg = @"<xml> 905 <ToUserName><![CDATA[gh_4d00ed8d6399]]></ToUserName> 906 <FromUserName><![CDATA[oV5CrjpxgaGXNHIQigzNlgLTnwic]]></FromUserName> 907 <CreateTime>1481013459</CreateTime> 908 <MsgType><![CDATA[event]]></MsgType> 909 <Event><![CDATA[MASSSENDJOBFINISH]]></Event> 910 <MsgID>1000001625</MsgID> 911 <Status><![CDATA[err(30003)]]></Status> 912 <TotalCount>0</TotalCount> 913 <FilterCount>0</FilterCount> 914 <SentCount>0</SentCount> 915 <ErrorCount>0</ErrorCount> 916 </xml>"; 917             request = WeChatRequest.Build(Config, PostModel, demoMsg); 918             demoHandler = new HandlerDemo(request); 919             Assert.ThrowsException<NotImplementedException>(() => { string result = demoHandler.Result; }, "OnPush群发结果"); 920             #endregion 921  922             #region 菜单相关推送 923             demoMsg = @"<xml> 924 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 925 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 926 <CreateTime>1408090502</CreateTime> 927 <MsgType><![CDATA[event]]></MsgType> 928 <Event><![CDATA[scancode_push]]></Event> 929 <EventKey><![CDATA[6]]></EventKey> 930 <ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType> 931 <ScanResult><![CDATA[1]]></ScanResult> 932 </ScanCodeInfo> 933 </xml>"; 934             request = WeChatRequest.Build(Config, PostModel, demoMsg); 935             demoHandler = new HandlerDemo(request); 936             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推事件")); 937  938             demoMsg = @"<xml> 939 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 940 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 941 <CreateTime>1408090606</CreateTime> 942 <MsgType><![CDATA[event]]></MsgType> 943 <Event><![CDATA[scancode_waitmsg]]></Event> 944 <EventKey><![CDATA[6]]></EventKey> 945 <ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType> 946 <ScanResult><![CDATA[2]]></ScanResult> 947 </ScanCodeInfo> 948 </xml>"; 949             request = WeChatRequest.Build(Config, PostModel, demoMsg); 950             demoHandler = new HandlerDemo(request); 951             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推等待事件")); 952  953             demoMsg = @"<xml> 954 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 955 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 956 <CreateTime>1408090651</CreateTime> 957 <MsgType><![CDATA[event]]></MsgType> 958 <Event><![CDATA[pic_sysphoto]]></Event> 959 <EventKey><![CDATA[6]]></EventKey> 960 <SendPicsInfo><Count>1</Count> 961 <PicList><item><PicMd5Sum><![CDATA[1b5f7c23b5bf75682a53e7b6d163e185]]></PicMd5Sum> 962 </item> 963 </PicList> 964 </SendPicsInfo> 965 </xml>"; 966             request = WeChatRequest.Build(Config, PostModel, demoMsg); 967             demoHandler = new HandlerDemo(request); 968             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu系统拍照发图")); 969  970             demoMsg = @"<xml> 971 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 972 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 973 <CreateTime>1408090816</CreateTime> 974 <MsgType><![CDATA[event]]></MsgType> 975 <Event><![CDATA[pic_photo_or_album]]></Event> 976 <EventKey><![CDATA[6]]></EventKey> 977 <SendPicsInfo><Count>1</Count> 978 <PicList><item><PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum> 979 </item> 980 </PicList> 981 </SendPicsInfo> 982 </xml>"; 983             request = WeChatRequest.Build(Config, PostModel, demoMsg); 984             demoHandler = new HandlerDemo(request); 985             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu拍照或者相册发图")); 986  987             demoMsg = @"<xml> 988 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName> 989 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName> 990 <CreateTime>1408090816</CreateTime> 991 <MsgType><![CDATA[event]]></MsgType> 992 <Event><![CDATA[pic_weixin]]></Event> 993 <EventKey><![CDATA[6]]></EventKey> 994 <SendPicsInfo><Count>1</Count> 995 <PicList><item><PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum> 996 </item> 997 </PicList> 998 </SendPicsInfo> 999 </xml>";1000             request = WeChatRequest.Build(Config, PostModel, demoMsg);1001             demoHandler = new HandlerDemo(request);1002             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu微信相册发图"));1003 1004             demoMsg = @"<xml>1005 <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>1006 <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>1007 <CreateTime>1408091189</CreateTime>1008 <MsgType><![CDATA[event]]></MsgType>1009 <Event><![CDATA[location_select]]></Event>1010 <EventKey><![CDATA[6]]></EventKey>1011 <SendLocationInfo><Location_X><![CDATA[23]]></Location_X>1012 <Location_Y><![CDATA[113]]></Location_Y>1013 <Scale><![CDATA[15]]></Scale>1014 <Label><![CDATA[广州市海珠区客村艺苑路 106号]]></Label>1015 <Poiname><![CDATA[]]></Poiname>1016 </SendLocationInfo>1017 </xml>";1018             request = WeChatRequest.Build(Config, PostModel, demoMsg);1019             demoHandler = new HandlerDemo(request);1020             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu位置选择"));1021             #endregion1022 1023 1024             string FillDemoContent(string text) => $@"<xml>1025  <ToUserName><![CDATA[toUser]]></ToUserName>1026  <FromUserName><![CDATA[fromUser]]></FromUserName>1027  <CreateTime>1348831860</CreateTime>1028  <MsgType><![CDATA[text]]></MsgType>1029  <Content><![CDATA[{text}]]></Content>1030  <MsgId>1234567890123456</MsgId>1031  </xml>";1032             demoMsg = FillDemoContent("123");1033             request = WeChatRequest.Build(Config, PostModel, demoMsg);1034             demoHandler = new HandlerDemo(request);1035             Assert.IsTrue(IsOkReponse(demoHandler, "123"));1036 1037             demoMsg = FillDemoContent("34567");1038             request = WeChatRequest.Build(Config, PostModel, demoMsg);1039             demoHandler = new HandlerDemo(request);1040             Assert.IsTrue(IsOkReponse(demoHandler, "123456"));1041 1042             demoMsg = FillDemoContent("aBc");1043             request = WeChatRequest.Build(Config, PostModel, demoMsg);1044             demoHandler = new HandlerDemo(request);1045             Assert.IsTrue(IsOkReponse(demoHandler, "ABc"));1046 1047             demoMsg = FillDemoContent("BcDeFgh");1048             request = WeChatRequest.Build(Config, PostModel, demoMsg);1049             demoHandler = new HandlerDemo(request);1050             Assert.IsTrue(IsOkReponse(demoHandler, "ABcdEf"));1051 1052 1053             demoMsg = @"<xml>1054 <ToUserName><![CDATA[toUser]]></ToUserName>1055 <FromUserName><![CDATA[FromUser]]></FromUserName>1056 <CreateTime>123456789</CreateTime>1057 <MsgType><![CDATA[event]]></MsgType>1058 <Event><![CDATA[CLICK]]></Event>1059 <EventKey><![CDATA[test_menu]]></EventKey>1060 </xml>";1061             request = WeChatRequest.Build(Config, PostModel, demoMsg);1062             demoHandler = new HandlerDemo(request);1063             Assert.IsTrue(IsOkReponse(demoHandler, "Event点击自定义菜单Test"));1064 1065             demoMsg = @"<xml>1066 <ToUserName><![CDATA[toUser]]></ToUserName>1067 <FromUserName><![CDATA[FromUser]]></FromUserName>1068 <CreateTime>123456789</CreateTime>1069 <MsgType><![CDATA[event]]></MsgType>1070 <Event><![CDATA[subscribe]]></Event>1071 <EventKey><![CDATA[qrscene_scanId]]></EventKey>1072 <Ticket><![CDATA[TICKET]]></Ticket>1073 </xml>";1074             request = WeChatRequest.Build(Config, PostModel, demoMsg);1075             demoHandler = new HandlerDemo(request);1076             Assert.IsTrue(IsOkReponse(demoHandler, "Event扫描带参数二维码scanId"));1077 1078             demoMsg = @"<xml>1079 <ToUserName><![CDATA[toUser]]></ToUserName>1080 <FromUserName><![CDATA[FromUser]]></FromUserName>1081 <CreateTime>123456789</CreateTime>1082 <MsgType><![CDATA[event]]></MsgType>1083 <Event><![CDATA[SCAN]]></Event>1084 <EventKey><![CDATA[scanId]]></EventKey>1085 <Ticket><![CDATA[TICKET]]></Ticket>1086 </xml>";1087             request = WeChatRequest.Build(Config, PostModel, demoMsg);1088             demoHandler = new HandlerDemo(request);1089             Assert.IsTrue(IsOkReponse(demoHandler, "Event扫描带参数二维码scanId"));1090 1091         }1092         public class HandlerDemo : MessageHandler1093         {1094             public HandlerDemo(WeChatRequest request) : base(request)1095             {1096                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Equal, "123"), (req, msg) =>1097                  {1098                      return WeChatResponse.ResponseText(req, "123");1099                  });1100                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Contain, "456"), (req, msg) =>1101                 {1102                     return WeChatResponse.ResponseText(req, "123456");1103                 });1104                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Superficial, "abc"), (req, msg) =>1105                 {1106                     return WeChatResponse.ResponseText(req, "ABc");1107                 });1108                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.ContainSuperficial, "def"), (req, msg) =>1109                 {1110                     return WeChatResponse.ResponseText(req, "ABcdEf");1111                 });1112 1113                 AddEventMenuClickHandler("test_menu", (req, msg) =>1114                 {1115                     return WeChatResponse.ResponseText(req, "Event点击自定义菜单Test");1116                 });1117                 AddEventScanIdHandler("scanId", (req, msg) =>1118                 {1119                     return WeChatResponse.ResponseText(req, "Event扫描带参数二维码scanId");1120                 });1121             }1122 1123             public override WeChatResponse DefaultResponseMessage(WeChatRequest request)1124             {1125                 return null; //未定义处理的默认方法,不处理则直接返回Null以回复空字符串给微信服务器1126             }1127             public override WeChatResponse OnMessageText(WeChatRequest request, WeChatRequest.WeChatMessageText text)1128             {1129                 return WeChatResponse.ResponseText(request, "OnMessageText");1130             }1131             public override WeChatResponse OnMessageImage(WeChatRequest request, WeChatRequest.WeChatMessageImage image)1132             {1133                 return WeChatResponse.ResponseText(request, "OnMessageImage");1134             }1135             public override WeChatResponse OnMessageVoice(WeChatRequest request, WeChatRequest.WeChatMessageVoice voice)1136             {1137                 return WeChatResponse.ResponseText(request, "OnMessageVoice");1138             }1139             public override WeChatResponse OnMessageLocaltion(WeChatRequest request, WeChatRequest.WeChatMessageLocation location)1140             {1141                 return WeChatResponse.ResponseText(request, "OnMessageLocaltion");1142             }1143             public override WeChatResponse OnMessageVideo(WeChatRequest request, WeChatRequest.WeChatMessageVideo video)1144             {1145                 return WeChatResponse.ResponseText(request, "OnMessageVideo");1146             }1147             public override WeChatResponse OnMessageShortVideo(WeChatRequest request, WeChatRequest.WeChatMessageVideo shortVideo)1148             {1149                 return WeChatResponse.ResponseText(request, "OnMessageShortVideo");1150             }1151             public override WeChatResponse OnMessageLink(WeChatRequest request, WeChatRequest.WeChatMessageLink link)1152             {1153                 return WeChatResponse.ResponseText(request, "OnMessageLink");1154             }1155 1156             public override WeChatResponse OnEvent关注(WeChatRequest request)1157             {1158                 return WeChatResponse.ResponseText(request, "OnEvent关注");1159             }1160             public override WeChatResponse OnEvent关注(WeChatRequest request, WeChatRequest.WeChatEvent扫描带参数二维码 msg)1161             {1162                 return WeChatResponse.ResponseText(request, "OnEvent关注-扫描带参数二维码");1163             }1164             public override void OnEvent取消关注(WeChatRequest request)1165             {1166                 throw new NotImplementedException("OnEvent取消关注");1167             }1168             public override WeChatResponse OnEvent扫描带参数二维码(WeChatRequest request, WeChatRequest.WeChatEvent扫描带参数二维码 msg)1169             {1170                 return WeChatResponse.ResponseText(request, "OnEvent扫描带参数二维码");1171             }1172             public override WeChatResponse OnEvent上报地理位置(WeChatRequest request, WeChatRequest.WeChatEvent上报地理位置 msg)1173             {1174                 return WeChatResponse.ResponseText(request, "OnEvent上报地理位置");1175             }1176             public override WeChatResponse OnEvent点击自定义菜单(WeChatRequest request, WeChatRequest.WeChatEvent点击自定义菜单 msg)1177             {1178                 return WeChatResponse.ResponseText(request, "OnEvent点击自定义菜单");1179             }1180             public override void OnEvent点击菜单跳转链接(WeChatRequest request, WeChatRequest.WeChatEvent点击菜单跳转链接 msg)1181             {1182                 throw new NotImplementedException("OnEvent点击菜单跳转链接");1183             }1184 1185             public override WeChatResponse OnPushMenu位置选择(WeChatRequest request, WeChatRequest.WeChatMenuPush位置选择 msg)1186             {1187                 return WeChatResponse.ResponseText(request, "OnPushMenu位置选择");1188             }1189             public override WeChatResponse OnPushMenu扫码推事件(WeChatRequest request, WeChatRequest.WeChatMenuPush扫码事件 msg)1190             {1191                 return WeChatResponse.ResponseText(request, "OnPushMenu扫码推事件");1192             }1193             public override WeChatResponse OnPushMenu扫码推等待事件(WeChatRequest request, WeChatRequest.WeChatMenuPush扫码事件 msg)1194             {1195                 return WeChatResponse.ResponseText(request, "OnPushMenu扫码推等待事件");1196             }1197             public override WeChatResponse OnPushMenu系统拍照发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1198             {1199                 return WeChatResponse.ResponseText(request, "OnPushMenu系统拍照发图");1200             }1201             public override WeChatResponse OnPushMenu微信相册发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1202             {1203                 return WeChatResponse.ResponseText(request, "OnPushMenu微信相册发图");1204             }1205             public override WeChatResponse OnPushMenu拍照或者相册发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1206             {1207                 return WeChatResponse.ResponseText(request, "OnPushMenu拍照或者相册发图");1208             }1209 1210             public override void OnPush模板消息发送结果(WeChatRequest request, WeChatRequest.WeChatPush模板消息发送结果 msg)1211             {1212                 throw new NotImplementedException("OnPush模板消息发送结果");1213             }1214             public override void OnPush群发结果(WeChatRequest request, WeChatRequest.WeChatPush群发结果 msg)1215             {1216                 throw new NotImplementedException("OnPush群发结果");1217             }1218         }1219 1220     }1221 }
単体テストのサンプルコード

 <br>
rree

以上がWeChatパブリックプラットフォームSDKコアライブラリの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。