public void SendNewsTest() { var accessToken = AccessTokenContainer.GetAccessToken(_appId); var articles = new List <Article>(); articles.Add(new Article() { Title = "测试标题", Description = "测试描述", Url = "http://weixin.Lime.com", PicUrl = "http://weixin.Lime.com/Images/qrcode.jpg" }); articles.Add(new Article() { Title = "测试更多标题", Description = "测试更多描述", Url = "http://weixin.Lime.com", PicUrl = "http://weixin.Lime.com/Images/qrcode.jpg" }); var result = CustomApi.SendNews(accessToken, openId, articles); Assert.IsNotNull(result); Assert.AreEqual("ok", result.errmsg); }
/* 参数范例: * articles.Add(new Article() * { * Title = "标题信息", * Description = "描述信息", * Url = "http://webchat.cloudapp.net/", * PicUrl = "http://webchat.cloudapp.net/Images/qrcode.jpg" * }); */ /// <summary> /// 发送文章 /// </summary> /// <param name="openId">OPENID</param> /// <param name="articles">Article集合。内容可以参考<seealso cref="Article"/>类。</param> public void SendArticle(string openId, List <Article> articles) { var accessToken = GetAccessToken(); var result = CustomApi.SendNews(accessToken, openId, articles); LogErrorInfo(result, openId, "SendArticle"); }
public void SendActivity(Wx_App app, Wx_Activity activity, string openId) { var articles = new List <Article>(); articles.Add(new Article { Title = activity.Name, Description = activity.Remark, PicUrl = activity.Poster, Url = activity.Url }); CustomApi.SendNews(app.AppId, openId, articles); }
/// <summary> /// 事件消息回复 /// </summary> public void responseMsg() { string content = ""; //发送的内容 CreateMenu(); //创建菜单 XmlDocument EventOrNews_XML = GetMsgXML(); string EventName = (object)EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("Event") == null ? "" : EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("Event").InnerText; //事件类型 string EventKey = (object)EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("EventKey") == null ? "" : EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText; //事件KEY值,与自定义菜单接口中KEY值对应 string MsgType = (object)EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("MsgType") == null ? "" : EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText; //消息类型 语音为voice 文本为 text string UserOpenId = EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText; LogHelper.WriteLog(UserOpenId); #region 关注 if (!string.IsNullOrEmpty(EventName) && EventName.Trim().ToLower() == "subscribe")//关注 - 事件类型 { //发送图文 string newsTitle2 = "欢迎加入傲鲨汽车生活馆"; string newsDescription2 = "“欢迎加入傲鲨汽车生活馆“ "; string newsPicUrl2 = "http://mmbiz.qpic.cn/mmbiz_jpg/x2FJcrdnw52ibeuGbIOJpiadG3ZjERJZDVewfKaklpxlnMOeM8gdcIKzvtWQXdiaDMibxfibkLWmvBJeicgRdhuwaPOw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1"; string newsUrl2 = "http://mp.weixin.qq.com/s/LbQOK2MbaY3pM6eUQfW_sQ"; Senparc.Weixin.MP.Entities.Article arc2 = new Senparc.Weixin.MP.Entities.Article(); arc2.Title = newsTitle2; arc2.Description = newsDescription2; arc2.PicUrl = newsPicUrl2; arc2.Url = newsUrl2; List <Senparc.Weixin.MP.Entities.Article> list2 = new List <Senparc.Weixin.MP.Entities.Article>(); list2.Add(arc2); CustomApi.SendNews(WeChatConfig.IsExistAccess_Token2(), UserOpenId, list2); //发送图文结束 LogHelper.WriteLog("subscribe"); int isfenxiao = 0; // int FId = 0; if (EventKey.Contains("qrscene_"))//、用户通过带有场景值的二维码进行的关注事件 添加分享表 上下级关系 { isfenxiao = 1; string[] Uids = EventKey.Split('_');//获取情景值 LogHelper.WriteLog("进去了" + EventKey + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + Uids.Length.ToString()); try { FId = Convert.ToInt32(Uids[1]); } catch { FId = 0; } } Senparc.Weixin.MP.AdvancedAPIs.User.UserInfoJson dic = UserApi.Info(WeChatConfig.IsExistAccess_Token2(), UserOpenId); if (weiuserB.GetListasNoTrackingBy(w => w.openid == dic.openid).Count <= 0) { var wechatuser = new MODEL.YX_weiUser(); wechatuser.subscribe = dic.subscribe; wechatuser.openid = dic.openid; wechatuser.nickname = dic.nickname; wechatuser.sex = dic.sex; wechatuser.U_language = dic.language; wechatuser.city = dic.city; wechatuser.province = dic.province; wechatuser.country = dic.country; wechatuser.headimgurl = dic.headimgurl; wechatuser.subscribe_time = DateTime.Now; wechatuser.userSex = dic.sex == 2 ? "女" : "男"; wechatuser.userNum = Common.Tools.Get8Digits(); wechatuser.F_id = 0; wechatuser.isfenxiao = 0; wechatuser.userMoney = 0; wechatuser.userYongJin = 0; wechatuser.TREE_NODE_ID = "144e42158f676695"; weiuserB.Add(wechatuser); } } #endregion #region 其他 if (!string.IsNullOrEmpty(EventName) && EventName.Trim().ToLower() == "unsubscribe")//取消订阅。 { // UserBll.upState(UserOpenId, 1, 0);//修改当前状态 } if (!string.IsNullOrEmpty(MsgType) && MsgType.Trim().ToLower() == "voice") { } if (!string.IsNullOrEmpty(MsgType) && MsgType.Trim().ToLower() == "image") { string touser = UserOpenId; string fromuser = EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText; string CreateTime = EventOrNews_XML.SelectSingleNode("xml").SelectSingleNode("CreateTime ").InnerText; HttpContext.Current.Response.Write(GetKeFuXML(touser, fromuser, CreateTime)); } #region CliCK if (!string.IsNullOrEmpty(EventName) && EventName.Trim().ToLower() == "click")//菜单单击事件 { LogHelper.WriteLog("click"); List <WeChatMenus> ClickMenus = new List <WeChatMenus>(); object ClickMenusList = CacheHelper.GetCache("ClickMenusList");//设置单击菜单缓存 防止多次访问数据库 if (ClickMenusList != null) { ClickMenus = (List <WeChatMenus>)ClickMenusList; } else { ClickMenus = wechatMenus.GetClickMenus(); if (ClickMenus != null) { CacheHelper.SetCache("ClickMenusList", ClickMenus, TimeSpan.FromMinutes(60)); } } foreach (var item in ClickMenus) { if (!string.IsNullOrEmpty(EventName) && EventKey.Trim().ToUpper() == item.WX_MenusKey_URL.ToUpper()) { string flat1 = item.flat1.ToString(); //用于匹配那张消息表:例如,YX_text YX_news YX_image等 int Id = item.Id; // string tableName = ""; //表名 switch (flat1) { case "1": tableName = "YX_text"; break; case "2": tableName = "YX_news"; break; case "3": tableName = "YX_image"; break; case "4": tableName = "YX_voice"; break; case "5": tableName = "YX_video"; break; case "6": tableName = "YX_music"; break; } if (tableName == "YX_text")//发送文本消息 { LogHelper.WriteLog(Id.ToString()); LogHelper.WriteLog("YX_text-" + EventKey); MODEL.ViewModel.VIEW_YX_text textdt = new MODEL.ViewModel.VIEW_YX_text(); object text_dt = CacheHelper.GetCache(EventKey);//设置主菜单缓存 防止多次访问数据库V1003_TODAY_MUSIC if (text_dt != null) { textdt = (MODEL.ViewModel.VIEW_YX_text)text_dt; } else { textdt = textM.GetClickMenusText(Id); if (textdt != null) { CacheHelper.SetCache(EventKey, textdt, TimeSpan.FromMinutes(60)); } } if (textdt != null) { string msgContent = textdt.msgContent; Common.Tools.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + WeChatConfig.IsExistAccess_Token2(), GetSubcribePostData(UserOpenId, msgContent)); } } if (tableName == "YX_news")//发送图文消息 { LogHelper.WriteLog("YX_news-" + EventKey); MODEL.ViewModel.VIEW_YX_news newsdt = new MODEL.ViewModel.VIEW_YX_news(); object news_dt = CacheHelper.GetCache(EventKey);//设置主菜单缓存 防止多次访问数据库V1003_TODAY_MUSIC if (news_dt != null) { newsdt = (MODEL.ViewModel.VIEW_YX_news)news_dt; } else { newsdt = newsM.GetClickMenusNews(Id); if (newsdt != null) { CacheHelper.SetCache(EventKey, newsdt, TimeSpan.FromMinutes(60)); } } if (newsdt != null) { string newsTitle = newsdt.newsTitle; string newsDescription = newsdt.newsDescription; string newsPicUrl = newsdt.newsPicUrl; string newsUrl = newsdt.newsUrl; Senparc.Weixin.MP.Entities.Article arc = new Senparc.Weixin.MP.Entities.Article(); arc.Title = newsTitle; arc.Description = newsDescription; arc.PicUrl = newsPicUrl; arc.Url = newsUrl; List <Senparc.Weixin.MP.Entities.Article> list = new List <Senparc.Weixin.MP.Entities.Article>(); list.Add(arc); CustomApi.SendNews(WeChatConfig.IsExistAccess_Token2(), UserOpenId, list); } } if (tableName == "YX_image")//发送图片消息 { //DataTable imagedt = new DataTable(); //imagedt = wxdb.GetClickMenusMsg(tableName, Id); //if (imagedt != null) //{ //} } if (tableName == "YX_voice")//发送语音消息 { //DataTable voicedt = new DataTable(); //voicedt = wxdb.GetClickMenusMsg(tableName, Id); } if (tableName == "YX_video")//发送视频消息 { //DataTable videodt = new DataTable(); //videodt = wxdb.GetClickMenusMsg(tableName, Id); } if (tableName == "YX_music")//发送音乐消息 { //DataTable musicdt = new DataTable(); //musicdt = wxdb.GetClickMenusMsg(tableName, Id); } } } } #endregion if (!string.IsNullOrEmpty(EventName) && EventName.Trim().ToLower() == "scancode_push")//菜单单击事件 { LogHelper.WriteLog("scancode_push"); } #endregion }
public static void SendNews(List <Guid?> userIds, List <Article> articles) { QueryOpenId(userIds).ForEach(openId => CustomApi.SendNews(AppId, openId, articles)); }
/// <summary> /// 服务号通用的发送消息 /// </summary> /// <param name="strMsgType"></param> /// <param name="strContent"></param> /// <param name="lstContent"></param> /// <param name="strTags"></param> /// <param name="strOpenids"></param> /// <param name="PreviewOpenids"></param> /// <returns></returns> public static SendResult SendMsg(string strMsgType, string strContent, List <ArticleInfoView> lstContent, string strTags, string[] strOpenids, string[] PreviewOpenids, bool isKefu = false /*是否客服消息*/) { SendResult returnResult = null; WxJsonResult retCustom = null; var objConfig = WeChatCommonService.GetWeChatConfigByID(lstContent[0].AppId.Value); //log.Debug("strUser:{0} strDept:{1} strTags:{2} Msg:{3} APPID:{4}", strUser, strDept, strTags, strContent, iAppID); // string strToken = Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); switch (strMsgType) { case "text": returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.NewsContent, Weixin.MP.GroupMessageType.text, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "news": //因为历史原因,news和mpnews对应反了.此处实际就是mpnews业务 List <NewsModel> articles = new List <NewsModel>(); string host = CommonService.GetSysConfig("Content Server", ""); if (host.EndsWith("/")) { host = host.Substring(0, host.Length - 1); } for (int i = 0; i < lstContent.Count; i++) { var item = lstContent[i]; var filePath = ""; if (0 == i) { //不存在就创建 filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_B"); } else { filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_T"); } var ret0 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, Weixin.MP.UploadMediaFileType.thumb, HttpRuntime.AppDomainAppPath + filePath); var picUrl = (host + filePath).Replace("\\", "/"); var url = string.Format("{0}/MPNews/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", host, item.Id, item.AppId, PreviewOpenids == null ? false : true); articles.Add(new NewsModel() { title = item.ArticleTitle, content = WechatCommonMP.ImageConvert(item.ArticleContent, objConfig.Id), thumb_url = picUrl, thumb_media_id = ret0.thumb_media_id, content_source_url = url }); } var ret = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, 10000, articles.ToArray()); returnResult = MPSendMessage(objConfig, ret.media_id, Weixin.MP.GroupMessageType.mpnews, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "mpnews": //因为历史原因,news和mpnews对应反了。此处实际就是news业务 List <Article> articles1 = new List <Article>(); string host1 = CommonService.GetSysConfig("Content Server", ""); if (host1.EndsWith("/")) { host1 = host1.Substring(0, host1.Length - 1); } for (int i = 0; i < lstContent.Count; i++) { var item = lstContent[i]; var filePath = ""; if (0 == i) { //不存在就创建 filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_B"); } else { filePath = WechatCommon.doGetFileCover(item.ImageCoverUrl, "_T"); } // var ret0 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, Weixin.MP.UploadMediaFileType.thumb, filePath); var picUrl = (host1 + filePath).Replace("\\", "/"); var url = string.Format("{0}/MPNews/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", host1, item.Id, item.AppId, PreviewOpenids == null ? false : true); articles1.Add(new Article() { Title = item.ArticleTitle, PicUrl = picUrl, Url = url, Description = item.ArticleComment }); } // var ret1 = Innocellence.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, 10000, articles1.ToArray()); // retCustom=SendMsgKF(strMsgType, objConfig,strOpenids, articles1); WxJsonResult retCustom1; foreach (var openId in strOpenids) { retCustom1 = CustomApi.SendNews(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, openId, articles1); if (retCustom1.errcode != ReturnCode.请求成功) { retCustom = retCustom1; } else if (retCustom == null) { retCustom = retCustom1; } } returnResult = new SendResult() { errcode = retCustom.errcode, errmsg = retCustom.errmsg }; // returnResult = MPSendMessage(objConfig, ret1.media_id, Weixin.MP.GroupMessageType.mpnews, strTags, strOpenids, PreviewOpenids, "", ""); break; case "image": lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.IMAGE, lstContent[0].NewsInfo.ImageContent); //WechatCommon.GetMediaInfo(AutoReplyContentEnum.IMAGE, lstContent[0].NewsInfo, objConfig.Id); returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.image, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; case "video": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VIDEO, lstContent[0].NewsInfo, lstContent[0].NewsInfo.AppId); lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.VIDEO, lstContent[0].NewsInfo.VideoContent); if (!isKefu) { var ret2 = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.GetOpenIdVideoMediaIdResult(objConfig.WeixinAppId, objConfig.WeixinCorpSecret, lstContent[0].NewsInfo.MediaId, lstContent[0].NewsInfo.NewsTitle, lstContent[0].NewsInfo.NewsComment); lstContent[0].NewsInfo.MediaId = ret2.media_id; lstContent[0].NewsInfo.MediaCreateTime = ret2.created_at; } MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.video, strTags, strOpenids, PreviewOpenids, lstContent[0].NewsInfo.NewsTitle, lstContent[0].NewsInfo.NewsComment, isKefu); break; case "voice": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VOICE, lstContent[0].NewsInfo, lstContent[0].NewsInfo.AppId); lstContent[0].NewsInfo.MediaId = WechatCommon.DoNewsInfo(lstContent[0].NewsInfo, objConfig, AutoReplyContentEnum.VOICE, lstContent[0].NewsInfo.SoundSrc); returnResult = MPSendMessage(objConfig, lstContent[0].NewsInfo.MediaId, Weixin.MP.GroupMessageType.voice, strTags, strOpenids, PreviewOpenids, "", "", isKefu); break; } return(returnResult); }