/// <summary> /// 根据accountPageUrl或者wechatid或者wechatname 解析最近文章页并解析历史消息记录(只需要指明一个参数即可) /// </summary> /// <param name="accountPageUrl">最近文章地址</param> /// <param name="wechatId">微信号</param> /// <param name="wechatName">微信昵称(不推荐,因为不唯一)</param> /// <remarks> 最保险的做法是提供url或者wechatid</remarks> /// <returns> list of batchMessage 一定含有字段qunfa_id,datetime,type 当type不同时,含有不同的字段,具体见文档</returns> public async Task <List <BatchMessage> > GetOfficialAccountMessagesAsync(string accountPageUrl = "", string wechatId = "", string wechatName = "") { string htmlStr = ""; if (!string.IsNullOrEmpty(accountPageUrl)) { htmlStr = await _GetRecentArticle_Html(accountPageUrl); } else if (!string.IsNullOrEmpty(wechatId)) { var account = await GetAccountInfoByIdAsync(wechatId); accountPageUrl = account.AccountPageurl; htmlStr = await _GetRecentArticle_Html(accountPageUrl); } else if (!string.IsNullOrEmpty(wechatName)) { var account = await GetAccountInfoByIdAsync(wechatName); accountPageUrl = account.AccountPageurl; htmlStr = await _GetRecentArticle_Html(accountPageUrl); } var encry = new EncryptArgs(); var articleJsonString = _ExtracJson(htmlStr); return(_ResolveBatchMessageFromJson(articleJsonString, encry)); }
/// <summary> /// 根据accountPageUrl或者wechatid或者wechatname 解析最近文章页并解析历史消息记录(只需要指明一个参数即可) /// </summary> /// <param name="accountPageUrl">最近文章地址</param> /// <param name="wechatId">微信号</param> /// <param name="wechatName">微信昵称(不推荐,因为不唯一)</param> /// <remarks> 最保险的做法是提供url或者wechatid</remarks> /// <returns> list of batchMessage 一定含有字段qunfa_id,datetime,type 当type不同时,含有不同的字段,具体见文档</returns> public List <BatchMessage> GetOfficialAccountMessages(string accountPageUrl = "", string wechatId = "", string wechatName = "") { string htmlStr = ""; if (!string.IsNullOrEmpty(accountPageUrl)) { htmlStr = this._GetRecentArticle_Html(accountPageUrl); } else if (!string.IsNullOrEmpty(wechatId)) { var account = this.GetAccountInfoById(wechatId); accountPageUrl = account.AccountPageurl; htmlStr = this._GetRecentArticle_Html(accountPageUrl); } else if (!string.IsNullOrEmpty(wechatName)) { var account = this.GetAccountInfoById(wechatName); accountPageUrl = account.RecentArticleUrl; htmlStr = this._GetRecentArticle_Html(accountPageUrl); } EncryptArgs encry = new EncryptArgs(); string articleJsonString = this._ExtracJson(htmlStr); return(this._ResolveBatchMessageFromJson(articleJsonString, encry)); }
public void Encrypt(EncryptArgs args) { IFileHandler publicKeyFileHandler = new FileHandler(args.PublicKeyFileName); IFileHandler unEncryptedFileHandler = new FileHandler(args.UnencryptedFileName); IFileHandler encryptedFileHandler = new FileHandler(args.EncryptedFileName); var cryptography = new RsaCrypoProvider(); string publicKey = publicKeyFileHandler.ReadFile(); string unencryptedText = unEncryptedFileHandler.ReadFile(); Byte[] encryptedBytes = cryptography.Encrypt(publicKey, unencryptedText); if (args.ShowKeys) { Console.WriteLine("Public Key: "); Console.WriteLine(publicKey); Console.WriteLine(); Console.WriteLine("Unencrypted: "); Console.WriteLine(unencryptedText); Console.WriteLine(); Console.WriteLine("Encrypted: "); string encryptedText = Encoding.Unicode.GetString(encryptedBytes); Console.WriteLine(encryptedText); return; } encryptedFileHandler.WriteToFile(encryptedBytes, 0, encryptedBytes.Length); }
EncryptArgs _uinkeybiz(string keyword, string uin, string key, string biz, string pass_ticket, string msgid) { EncryptArgs encrpt = new EncryptArgs(); if (string.IsNullOrEmpty(uin)) { this.weChatCache.Update(keyword + "uin", uin, 36000); this.weChatCache.Update(keyword + "key", key, 36000); this.weChatCache.Update(keyword + "biz", biz, 36000); this.weChatCache.Update(keyword + "pass_ticket", pass_ticket, 36000); this.weChatCache.Update(keyword + "msgid", msgid, 36000); } else { uin = weChatCache.Get <object>(keyword + "uin").ToString(); key = this.weChatCache.Get <object>(keyword + "key").ToString(); biz = this.weChatCache.Get <object>(keyword + "biz").ToString(); pass_ticket = this.weChatCache.Get <object>(keyword + "pass_ticket").ToString(); msgid = this.weChatCache.Get <object>(keyword + "msgid").ToString(); encrpt.uin = uin; encrpt.key = key; encrpt.biz = biz; encrpt.pass_ticket = pass_ticket; encrpt.msgid = msgid; encrpt.uin = uin; } return(encrpt); // def _uinkeybiz(self, keyword, uin= None, key = None, biz = None, pass_ticket = None, msgid = None): //if uin: // self._cache.set(keyword + 'uin', uin, 36000) // self._cache.set(keyword + 'key', key, 36000) // self._cache.set(keyword + 'biz', biz, 36000) // self._cache.set(keyword + 'pass_ticket', pass_ticket, 36000) // self._cache.set(keyword + 'msgid', msgid, 36000) //else: // uin = self._cache.get(keyword + 'uin') // key = self._cache.get(keyword + 'key') // biz = self._cache.get(keyword + 'biz') // pass_ticket = self._cache.get(keyword + 'pass_ticket') // msgid = self._cache.get(keyword + 'msgid') // return uin, key, biz, pass_ticket, msgid }
/// <summary> /// 根据accountPageUrl,或者wechatid或者 wechatname获取公众号相关信息及已发消息(视频,语音或者文章)json格式 /// </summary> /// <param name="accountPageUrl"></param> /// <param name="wechatId"></param> /// <param name="wechatName"></param> /// <remarks>get_gzh_message_and_info</remarks> /// <returns>公众号相关信息及已发消息(json)</returns> public async Task <string> GetOfficialAccountInfoAndMessagesAsync(string accountPageUrl = "", string wechatId = "", string wechatName = "") { string text = ""; string url = ""; if (!string.IsNullOrEmpty(accountPageUrl)) { url = accountPageUrl; text = await _GetRecentArticle_Html(url); } else if (!string.IsNullOrEmpty(wechatId)) { var officialAccount = await GetAccountInfoByIdAsync(wechatId); url = officialAccount.AccountPageurl; text = await _GetRecentArticle_Html(url); } else if (!string.IsNullOrEmpty(wechatName)) { var officialAccount = await GetAccountInfoByIdAsync(wechatName); url = officialAccount.AccountPageurl; text = await _GetRecentArticle_Html(url); } var encryp = new EncryptArgs(); var json = Newtonsoft.Json.JsonConvert.SerializeObject ( new { OfficialAccount = _ResolveOfficialAccount(text, url), Message = _ResolveBatchMessageFromJson(_ExtracJson(text), encryp) } , Newtonsoft.Json.Formatting.Indented ); return(json); }
/// <summary> /// 解析 公众号 群发消息 /// </summary> /// <param name="jsonText"></param> /// <param name="encryp"></param> /// <returns></returns> protected List <BatchMessage> _ResolveBatchMessageFromJson(string jsonText, EncryptArgs encryp) { var messages = new List <BatchMessage>(); string biz = encryp.biz; string uin = encryp.uin; string key = encryp.key; var root = JsonConvert.DeserializeObject <MsgRoot>(jsonText); var msgs = HandleHtmlCode(root.list); msgs.ForEach(m => { var message = new BatchMessage() { Author = m.app_msg_ext_info.author, Meaasgeid = m.comm_msg_info.id, SendDate = m.comm_msg_info.datetime.ToString(), Type = m.comm_msg_info.type.ToString() }; switch (message.Type) { case "1": //文字 message.Content = m.comm_msg_info.content; break; case "3": //图片 message.ImageUrl = $"https://mp.weixin.qq.com/mp/getmediadata?biz={biz}&type=img&mode=small&msgid={message.Meaasgeid}&uin=uin&key={key}"; break; case "34": //音频 message.PlayLength = m.voice_msg_ext_info.play_length; message.FileId = m.voice_msg_ext_info.fileid; message.AudioSrc = $"https://mp.weixin.qq.com/mp/getmediadata?biz={biz}&type=voice&msgid={message.Meaasgeid}&uin={uin}&key={key}"; break; case "49": //图文 message.ContentUrl = m.app_msg_ext_info.content_url.Contains("http://mp.weixin.qq.com") ? m.app_msg_ext_info.content_url : "http://mp.weixin.qq.com" + m.app_msg_ext_info.content_url; message.Main = 1; message.Title = m.app_msg_ext_info.title; message.Digest = m.app_msg_ext_info.digest; message.FileId = m.app_msg_ext_info.fileid; message.SourceUrl = m.app_msg_ext_info.source_url; message.Cover = m.app_msg_ext_info.cover; message.Author = m.app_msg_ext_info.author; message.CopyrightStat = m.app_msg_ext_info.copyright_stat.ToString(); messages.Add(message); if (m.app_msg_ext_info.is_multi == 1) //源群发消息文章还包含多个副文章 { var subMsgs = ParseSubArticle(m.app_msg_ext_info.multi_app_msg_item_list); subMsgs.ForEach(subMsg => subMsg.SendDate = message.SendDate); messages.AddRange(subMsgs); } break; case "62": //视频 message.CdnVideoId = m.video_msg_ext_info.cdn_videoid; message.Thumb = m.video_msg_ext_info.thumb; message.VideoSrc = $"https://mp.weixin.qq.com/mp/getcdnvideourl?biz={biz}&cdn_videoid={message.CdnVideoId}&thumb={message.Thumb}&uin=&key={key}"; break; } }); // 删除搜狗本身携带的空数据,(type=49以及contentUrl为空的是无效数据) var finalMessages = messages.Where(msg => !(msg.Type == "49" && string.IsNullOrEmpty(msg.ContentUrl))).ToList(); return(finalMessages); }
/// <summary> /// 解析 公众号 群发消息 /// </summary> /// <param name="text"></param> /// <param name="encryp"></param> /// <returns></returns> protected List <BatchMessage> _ResolveBatchMessageFromJson(string jsonText, EncryptArgs encryp) { List <BatchMessage> messages = new List <BatchMessage>(); string biz = encryp.biz; string uin = encryp.uin; string key = encryp.key; JObject jo = JObject.Parse(jsonText);//把json字符串转化为json对象 JArray msgList = (JArray)jo.GetValue("list"); foreach (JObject msg in msgList) { BatchMessage aMessage = new BatchMessage(); JObject commMsgInfo = (JObject)msg.GetValue("comm_msg_info"); aMessage.Meaasgeid = (int)commMsgInfo.GetValue("id"); aMessage.SendDate = (string)commMsgInfo.GetValue("datetime"); aMessage.Type = (string)commMsgInfo.GetValue("type"); switch (aMessage.Type) { case "1": //文字 aMessage.Content = (string)commMsgInfo.GetValue(""); break; case "3": //图片 aMessage.ImageUrl = "https://mp.weixin.qq.com/mp/getmediadata?__biz=" + biz + "&type=img&mode=small&msgid=" + aMessage.Meaasgeid + "&uin=" + uin + "&key=" + key; break; case "34": //音频 aMessage.PlayLength = (string)msg.SelectToken("voice_msg_ext_info.play_length"); aMessage.FileId = (int)msg.SelectToken("voice_msg_ext_info.fileid"); aMessage.AudioSrc = "https://mp.weixin.qq.com/mp/getmediadata?biz=" + biz + "&type=voice&msgid=" + aMessage.Meaasgeid + "&uin=" + uin + "&key=" + key; break; case "49": //图文 JObject AppMsgExtInfo = (JObject)msg.GetValue("app_msg_ext_info"); string url = (string)AppMsgExtInfo.GetValue("content_url"); if (!String.IsNullOrEmpty(url)) { if (!url.Contains("http://mp.weixin.qq.com")) { url = "http://mp.weixin.qq.com" + url; } } else { url = ""; } aMessage.Main = 1; aMessage.Title = (string)AppMsgExtInfo.GetValue("title"); aMessage.Digest = (string)AppMsgExtInfo.GetValue("digest"); aMessage.FileId = (int)AppMsgExtInfo.GetValue("fileid"); aMessage.ContentUrl = url; aMessage.SourceUrl = (string)AppMsgExtInfo.GetValue("source_url"); aMessage.Cover = (string)AppMsgExtInfo.GetValue("cover"); aMessage.Author = (string)AppMsgExtInfo.GetValue("author"); aMessage.CopyrightStat = (string)AppMsgExtInfo.GetValue("copyright_stat"); messages.Add(aMessage); if ((int)AppMsgExtInfo.GetValue("is_multi") == 1) { JArray multi_app_msg_item_list = (JArray)AppMsgExtInfo.GetValue("multi_app_msg_item_list"); BatchMessage moreMessage = new BatchMessage(); foreach (JObject subMsg in multi_app_msg_item_list) { url = (string)subMsg.GetValue("content_url"); if (!string.IsNullOrEmpty(url)) { if (!url.Contains("http://mp.weixin.qq.com")) { url = "http://mp.weixin.qq.com" + url; } } else { url = ""; } moreMessage.Title = (string)subMsg.GetValue("title"); moreMessage.Digest = (string)subMsg.GetValue("digest"); moreMessage.FileId = (int)subMsg.GetValue("fileid"); moreMessage.ContentUrl = url; moreMessage.SourceUrl = (string)subMsg.GetValue("source_url"); moreMessage.Cover = (string)subMsg.GetValue("cover"); moreMessage.Author = (string)subMsg.GetValue("author"); moreMessage.CopyrightStat = (string)subMsg.GetValue("copyright_stat"); messages.Add(moreMessage); } } continue; case "62": //视频 aMessage.CdnVideoId = (string)msg.SelectToken("video_msg_ext_info.cdn_videoid"); aMessage.Thumb = (string)msg.SelectToken("video_msg_ext_info.thumb"); aMessage.VideoSrc = "https://mp.weixin.qq.com/mp/getcdnvideourl?__biz=" + biz + "&cdn_videoid=" + aMessage.CdnVideoId + "&thumb=" + aMessage.Thumb + "&uin=" + "&key=" + key; break; default: break; } messages.Add(aMessage); } // 删除搜狗本身携带的空数据 List <BatchMessage> FinalMessages = new List <BatchMessage>(); foreach (var m in messages) { if (m.Type == "49" && string.IsNullOrEmpty(m.ContentUrl)) { } else { FinalMessages.Add(m); } } return(FinalMessages); }