详细介绍微信公众平台的核心库SDK
发表于:2024-11-28 作者:热门IT资讯网编辑
编辑最后更新 2024年11月28日,今天小编就为大家带来一篇介绍微信公众平台的核心库SDK的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。NuGet包地址:由于来的OYMLCN.WeChat存在深度封装,并没有
今天小编就为大家带来一篇介绍微信公众平台的核心库SDK的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。
NuGet包地址:
由于来的OYMLCN.WeChat存在深度封装,并没有做完整的测试,对于使用不友好,现已重新构建SDK的接收消息被动回复模块。
现已做到最大程度的简易化及模块化,每个模块都已完成单元测试(根据微信说明文档的示例内容)。
接下来将会重新构建微信接口的调用模块,将会在基本完成后发布。
类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的扩展方法,用于验证请求的有效性并获取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关于微信公众平台的核心库SDK介绍就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。() 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 = @" 24 "; 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 = @"25 26 1348831860 2728 29 1234567890123456 3041 "; 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 = @"42 43 1348831860 4445 46 47 1234567890123456 4859 "; 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 = @"60 61 1357290913 6263 64 65 1234567890123456 6677 "; 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 = @"78 79 1357290913 8081 82 83 84 1234567890123456 8597 "; 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 = @"98 99 1357290913 100101 102 103 1234567890123456 104115 "; 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 = @"116 117 1357290913 118119 120 121 1234567890123456 122133 "; 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 = @"134 135 1351776360 136137 23.134521 138113.358803 13920 140 1411234567890123456 142155 "; 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 = @"156 157 1351776360 158159 160 161 162 1234567890123456 163180 "; 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 = @"181 182 123456789 183184 185 195 "; 201 request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 202 Assert.IsTrue(request.Event取消关注); 203 204 subscribeEvent = @"196 197 123456789 198199 200 205 "; 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 = @"206 207 123456789 208209 210 211 212 224 "; 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 = @"225 226 123456789 227228 229 230 231 244 "; 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 = @"245 246 123456789 247248 249 23.137466 250113.352425 251119.385040 252264 "; 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 = @"265 266 123456789 267268 269 270 280 "; 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 = @"281 282 123456789 283284 285 286 123 287304 "; 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 = @"305 306 1395658920 307308 309 200163836 310311 322 "; 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 = @"323 324 1395658984 325326 327 200163840 328329 339 "; 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 = @"340 341 1395658984 342343 344 200163840 345346 359 "; 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 = @"360 361 1481013459 362363 364 1000001625 365366 0 3670 3680 3690 370371 3962 372373 394- 374
3831 3750 3762 377378 1 3791 3801 3811 382- 384
3932 3850 3862 387388 1 3891 3901 3911 3922 395435 "; 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 = @"436 437 1408090502 438439 440 441 444 442 443 455 "; 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 = @"456 457 1408090606 458459 460 461 464 462 463 476 "; 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 = @"477 478 1408090651 479480 481 482 487 1 483486 - 485
484 498 "; 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 = @"499 500 1408090816 501502 503 504 509 1 505508 - 507
506 520 "; 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 = @"521 522 1408090816 523524 525 526 531 1 527530 - 529
528 543 "; 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 = @"544 545 1408091189 546547 548 549 555 550 551 552 553 554 575 "; 579 var request = WeChatRequest.Build(Config, PostModel, demoMsg); 580 Assert.AreEqual(WeChatResponse.ResponseText(request, "你好").Source, @"576 577 12345678 578581 ".RemoveBr()); 587 Assert.AreEqual(WeChatResponse.ResponseImage(request, "media_id").Source, @"582 583 12345678 584585 586 588 ".RemoveBr()); 596 Assert.AreEqual(WeChatResponse.ResponseVoice(request, "media_id").Source, @"589 590 12345678 591592 593 595594 597 ".RemoveBr()); 605 Assert.AreEqual(WeChatResponse.ResponseVideo(request, "media_id", "title", "description").Source, @"598 599 12345678 600601 602 604603 606 ".RemoveBr()); 616 Assert.AreEqual(WeChatResponse.ResponseMusic(request, "media_id", "MUSIC_Url", "TITLE", "DESCRIPTION", "HQ_MUSIC_Url").Source, @"607 608 12345678 609610 615 617 ".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, @"618 619 12345678 620621 622 628623 624 625 626 627 634 ".RemoveBr()); 654 Assert.AreEqual(WeChatResponse.TransferToCustomerService(request).Source, @"635 636 12345678 637638 2 639640 653- 641
646642 643 644 645 - 647
652648 649 650 651 655 ".RemoveBr()); 660 Assert.AreEqual(WeChatResponse.TransferToCustomerService(request, "test1").Source, @"656 657 12345678 658659 661 ".RemoveBr()); 667 } 668 669 [TestMethod] 670 public void WeChatExtensionTest() 671 { 672 var demoQuery = new Dictionary662 663 12345678 664665 666 () 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 (); 683 Assert.IsNull(demoQuery.IsValidRequest(Config)); 684 Assert.AreEqual(demoQuery.ConfigVerify(Config), ""); 685 } 686 687 [TestMethod] 688 public void WeChatHanddlerTest() 689 { 690 string demoMsg = @" 691 "; 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 = @"692 693 1348831860 694695 696 1234567890123456 697706 "; 713 request = WeChatRequest.Build(Config, PostModel, demoMsg); 714 demoHandler = new HandlerDemo(request); 715 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageText")); 716 717 demoMsg = @"707 708 1348831860 709710 711 1234567890123456 712718 "; 726 request = WeChatRequest.Build(Config, PostModel, demoMsg); 727 demoHandler = new HandlerDemo(request); 728 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageImage")); 729 730 demoMsg = @"719 720 1348831860 721722 723 724 1234567890123456 725731 "; 740 request = WeChatRequest.Build(Config, PostModel, demoMsg); 741 demoHandler = new HandlerDemo(request); 742 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVoice")); 743 744 demoMsg = @"732 733 1357290913 734735 736 737 738 1234567890123456 739745 "; 753 request = WeChatRequest.Build(Config, PostModel, demoMsg); 754 demoHandler = new HandlerDemo(request); 755 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVideo")); 756 757 demoMsg = @"746 747 1357290913 748749 750 751 1234567890123456 752758 "; 766 request = WeChatRequest.Build(Config, PostModel, demoMsg); 767 demoHandler = new HandlerDemo(request); 768 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageShortVideo")); 769 770 demoMsg = @"759 760 1357290913 761762 763 764 1234567890123456 765771 "; 781 request = WeChatRequest.Build(Config, PostModel, demoMsg); 782 demoHandler = new HandlerDemo(request); 783 Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageLocaltion")); 784 785 demoMsg = @"772 773 1351776360 774775 23.134521 776113.358803 77720 778 7791234567890123456 780786 "; 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 = @"787 788 1351776360 789790 791 792 793 1234567890123456 794802 "; 808 request = WeChatRequest.Build(Config, PostModel, demoMsg); 809 demoHandler = new HandlerDemo(request); 810 Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注")); 811 812 813 demoMsg = @"803 804 123456789 805806 807 814 "; 820 request = WeChatRequest.Build(Config, PostModel, demoMsg); 821 demoHandler = new HandlerDemo(request); 822 Assert.ThrowsException815 816 123456789 817818 819 (() => { string result = demoHandler.Result; }, "OnEvent取消关注"); 823 824 demoMsg = @" 825 "; 833 request = WeChatRequest.Build(Config, PostModel, demoMsg); 834 demoHandler = new HandlerDemo(request); 835 Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注-扫描带参数二维码")); 836 837 demoMsg = @"826 827 123456789 828829 830 831 832 838 "; 846 request = WeChatRequest.Build(Config, PostModel, demoMsg); 847 demoHandler = new HandlerDemo(request); 848 Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent扫描带参数二维码")); 849 850 demoMsg = @"839 840 123456789 841842 843 844 845 851 "; 860 request = WeChatRequest.Build(Config, PostModel, demoMsg); 861 demoHandler = new HandlerDemo(request); 862 Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent上报地理位置")); 863 864 demoMsg = @"852 853 123456789 854855 856 23.137466 857113.352425 858119.385040 859865 "; 872 request = WeChatRequest.Build(Config, PostModel, demoMsg); 873 demoHandler = new HandlerDemo(request); 874 Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent点击自定义菜单")); 875 876 demoMsg = @"866 867 123456789 868869 870 871 877 "; 885 request = WeChatRequest.Build(Config, PostModel, demoMsg); 886 demoHandler = new HandlerDemo(request); 887 Assert.ThrowsException878 879 123456789 880881 882 883 123 884(() => { string result = demoHandler.Result; }, "OnEvent点击菜单跳转链接"); 888 #endregion 889 890 #region 微信服务相关 891 demoMsg = @" 892 "; 900 request = WeChatRequest.Build(Config, PostModel, demoMsg); 901 demoHandler = new HandlerDemo(request); 902 Assert.ThrowsException893 894 1395658920 895896 897 200163836 898899 (() => { string result = demoHandler.Result; }, "OnPush模板消息发送结果"); 903 904 demoMsg = @" 905 "; 917 request = WeChatRequest.Build(Config, PostModel, demoMsg); 918 demoHandler = new HandlerDemo(request); 919 Assert.ThrowsException906 907 1481013459 908909 910 1000001625 911912 0 9130 9140 9150 916(() => { string result = demoHandler.Result; }, "OnPush群发结果"); 920 #endregion 921 922 #region 菜单相关推送 923 demoMsg = @" 924 "; 934 request = WeChatRequest.Build(Config, PostModel, demoMsg); 935 demoHandler = new HandlerDemo(request); 936 Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推事件")); 937 938 demoMsg = @"925 926 1408090502 927928 929 930 933 931 932 939 "; 949 request = WeChatRequest.Build(Config, PostModel, demoMsg); 950 demoHandler = new HandlerDemo(request); 951 Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推等待事件")); 952 953 demoMsg = @"940 941 1408090606 942943 944 945 948 946 947 954 "; 966 request = WeChatRequest.Build(Config, PostModel, demoMsg); 967 demoHandler = new HandlerDemo(request); 968 Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu系统拍照发图")); 969 970 demoMsg = @"955 956 1408090651 957958 959 960 965 1 961964 - 963
962 971 "; 983 request = WeChatRequest.Build(Config, PostModel, demoMsg); 984 demoHandler = new HandlerDemo(request); 985 Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu拍照或者相册发图")); 986 987 demoMsg = @"972 973 1408090816 974975 976 977 982 1 978981 - 980
979 988 ";1000 request = WeChatRequest.Build(Config, PostModel, demoMsg);1001 demoHandler = new HandlerDemo(request);1002 Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu微信相册发图"));1003 1004 demoMsg = @"989 990 1408090816 991992 993 994 999 1 995998 - 997
996 1005 ";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) => $@"1006 1007 1408091189 10081009 1010 1011 1017 1012 1013 1014 1015 1016 1025 ";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 = @"1026 1027 1348831860 10281029 1030 1234567890123456 10311054 ";1061 request = WeChatRequest.Build(Config, PostModel, demoMsg);1062 demoHandler = new HandlerDemo(request);1063 Assert.IsTrue(IsOkReponse(demoHandler, "Event点击自定义菜单Test"));1064 1065 demoMsg = @"1055 1056 123456789 10571058 1059 1060 1066 ";1074 request = WeChatRequest.Build(Config, PostModel, demoMsg);1075 demoHandler = new HandlerDemo(request);1076 Assert.IsTrue(IsOkReponse(demoHandler, "Event扫描带参数二维码scanId"));1077 1078 demoMsg = @"1067 1068 123456789 10691070 1071 1072 1073 1079 ";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 }1080 1081 123456789 10821083 1084 1085 1086