/// <summary> /// news消息 /// </summary> /// <param name="toUser">UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送</param> /// <param name="toParty">PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="toTag">TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="agentId">企业应用的id,可在应用的设置页面查看</param> /// <param name="articles">图文信息内容,包括title(标题)、description(描述)、url(点击后跳转的链接。企业可根据url里面带的code参数校验员工的真实身份)和picurl(图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80。如不填,在客户端不显示图片)</param> /// <param name="safe">表示是否是保密消息,0表示否,1表示是,默认0</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static MassResult SendNews(string toUser, string toParty, string toTag, string agentId, List <Article> articles, int safe = 0, int timeOut = 10000) { try { if (toParty != null && toParty.Split(new char[] { '|' }).Length > 100) { MassResult result = new MassResult(); var list = SplitArray(toParty.Split(new char[] { '|' }), 100); for (int i = 0; i < list.Count; i++) { if (i == 0) { result = MassApi.SendNews(GetToken(), toUser, string.Join("|", list[i]), toTag, agentId, articles); } else { result = MassApi.SendNews(GetToken(), null, string.Join("|", list[i]), null, agentId, articles); } } return(result); } else { return(MassApi.SendNews(GetToken(), toUser, toParty, toTag, agentId, articles)); } } catch (Exception e) { MassResult result = new MassResult(); return(result); } }
public void SendTH(List <Article> MODEL, string ModelCode, string type, string strUserS = "@all") { try { var app = new JH_Auth_ModelB().GetEntity(p => p.ModelCode == ModelCode); if (strUserS == "") { return; } thModel th = new thModel(); th.MODEL = MODEL; th.authAppID = app.AppID; th.UserS = string.IsNullOrEmpty(strUserS) ? "@all" : strUserS; if (Qyinfo.IsUseWX == "Y") { th.MODEL.ForEach(d => d.Url = Qyinfo.WXUrl.TrimEnd('/') + "/View_Mobile/UI/UI_COMMON.html?funcode=" + ModelCode + "_" + type + (d.Url == "" ? "" : "_" + d.Url) + "&corpid=" + Qyinfo.corpId.Trim()); th.MODEL.ForEach(d => d.PicUrl = (string.IsNullOrEmpty(d.PicUrl) ? "" : Qyinfo.FileServerUrl.Trim() + Qyinfo.QYCode + "/document/image/" + new FT_FileB().ExsSclarSql("select zyid from FT_File where ID='" + d.PicUrl + "'").ToString())); //if (app.AppType == "1") //{ MassApi.SendNews(GetToken(app.AppID.ToString()), app.AppID, th.MODEL, th.UserS.Replace(',', '|'), "", ""); //} //else //{ // MassApi.SendText(GetToken(app.AppID.ToString()), th.UserS.Replace(',', '|'), "", "", app.AppID, th.MODEL[0].Title); //} } } catch (Exception ex) { CommonHelp.WriteLOG(ex.ToString()); } }
/// <summary> /// 发送图文消息到给定的用户; /// 注意普通图文消息的长度不超过512字节; /// </summary> /// <param name="userOpenId"></param> /// <param name="articleList"></param> public void SendNews(string userOpenId, List <Article> articleList) { // userOpenId = "qy01c0f1cc0310c7002848a37e2e"; MassApi.SendNews(AccessTokenOrAppKey, Config.AppId, articleList, CorrectUserOpenId(userOpenId)); //MassApi.SendNews(AccessToken, null, articleList, userOpenId); // MassApi.SendNews(AccessToken, userOpenId, null, null, Config.AgentId, articleList); }
/// <summary> /// 发送图文消息 /// </summary> /// <param name="accessTokenOrAppKey">调用接口凭证(AccessToken)或AppKey(根据AccessTokenManager.BuildingKey(corpId, corpSecret)方法获得)</param> /// <param name="toUser">UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送</param> /// <param name="toParty">PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="toTag">TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="agentId">企业应用的id,可在应用的设置页面查看</param> /// <param name="articles">图文信息内容,包括title(标题)、description(描述)、url(点击后跳转的链接。企业可根据url里面带的code参数校验员工的真实身份)和picurl(图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80。如不填,在客户端不显示图片)</param> /// <param name="safe">表示是否是保密消息,0表示否,1表示是,默认0</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static MessageResult SendNews(string accessTokenOrAppKey, int agentId, NewsBody body, string toUser = null, string toParty = null, string toTag = null, int safe = 0, int timeOut = Config.TIME_OUT) { var result = MassApi.SendNews(accessTokenOrAppKey, agentId.ToString(), body.ToNews(), toUser, toParty, toTag, safe, timeOut); return(result.ToMsgResult()); }
public WorkJsonResult MassApiSendNews(SendNewsDataDto dto) { //var token = GetAccessToken(QyConfig.CorpID, QyConfig.CorpSecret); dto.agentid = 91; //dto.news.articles.ForEach(p=> { // p.description= striphtml(p.description.Replace("'", "”")).Replace("'", "”"); //}); var result = MassApi.SendNews(dto.AccessToken, dto); return(result); }
/// <summary> /// 微信图文推送。该函数不建议使用,建议使用PublishMessage函数代替。 /// </summary> /// <param name="iAppID"></param> /// <param name="strMsgType"></param> /// <param name="strUser"></param> /// <param name="strDept"></param> /// <param name="strTags"></param> /// <param name="strContent"></param> /// <param name="lstContent"></param> /// <returns></returns> public static MassResult SendMsg(int iAppID, string strMsgType, string strUser, string strDept, string strTags, string strContent, List <Article> lstContent) { MassResult objResult = null; int IsSec = 0; var objConfig = WeChatCommonService.GetWeChatConfig(iAppID); LogManager.GetLogger(typeof(WechatCommon)).Debug("strUser:{0} strDept:{1} strTags:{2} Msg:{3}", strUser, strDept, strTags, strContent); string strToken = AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); switch (strMsgType) { case "text": objResult = MassApi.SendText(strToken, strUser, strDept, strTags, iAppID.ToString(), strContent, IsSec); break; case "image": Dictionary <string, Stream> dic = new Dictionary <string, Stream>(); // for (int i = 0; i < Request.Files.Count; i++) { var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("~/") + "/style/images/meteorshower.jpg"); dic.Add("meteorshower.jpg", stream); } var ret = MediaApi.Upload(strToken, UploadMediaFileType.file, dic, ""); objResult = MassApi.SendImage(strToken, strUser, strDept, strTags, iAppID.ToString(), ret.media_id, IsSec); break; case "news": objResult = MassApi.SendNews(strToken, strUser, strDept, strTags, iAppID.ToString(), lstContent, IsSec); break; case "file": Dictionary <string, Stream> dic1 = new Dictionary <string, Stream>(); for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("~/") + "/style/images/meteorshower.jpg"); dic1.Add("meteorshower.jpg", stream); } var ret1 = MediaApi.Upload(strToken, UploadMediaFileType.file, dic1, ""); objResult = MassApi.SendFile(strToken, strUser, strDept, strTags, iAppID.ToString(), ret1.media_id, IsSec); break; } return(objResult); }
public ActionResult SendNewMsgToBase64(string by, string value, string applicationId, string data, string type, string safe, string clientId) { if (!VerifyNewParam("by,value,applicationId,type,safe", clientId, _signCheckService)) { return(ErrMsg()); } if (string.IsNullOrEmpty(data) == false) { byte[] bytes = Convert.FromBase64String(data.Replace(" ", "+")); data = Encoding.UTF8.GetString(bytes); data = HttpUtility.HtmlDecode(data); } var weChatConfig = GetWechatConfig(); var toUsrids = string.Join("|", value); var token = Innocellence.Weixin.QY.CommonAPIs.AccessTokenContainer.GetToken(weChatConfig.WeixinCorpId, weChatConfig.WeixinCorpSecret); Weixin.QY.AdvancedAPIs.Mass.MassResult objResult; if (type == "news") { var lst = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Article> >(data); objResult = MassApi.SendNews(token, toUsrids, "", "", weChatConfig.WeixinAppId.ToString(), lst, safe == "true" ? 1 : 0); } else { objResult = MassApi.SendText(token, toUsrids, "", "", weChatConfig.WeixinAppId.ToString(), data, safe == "true" ? 1 : 0); } if (objResult.errcode == Weixin.ReturnCode_QY.请求成功) { return(Json(new { message = "", success = true }, JsonRequestBehavior.AllowGet)); } else { return(ErrMsg(objResult.errmsg)); } // return Json(new { success = true, message = "" }, JsonRequestBehavior.AllowGet); }
public void SendTPMSG(string ModelCode, List <Article> MODEL, string strUserS = "@all") { try { var app = new JH_Auth_ModelB().GetEntity(p => p.ModelCode == ModelCode); if (strUserS == "") { return; } if (Qyinfo.IsUseWX == "Y") { MassApi.SendNews(GetToken(app.AppID.ToString()), app.AppID, MODEL, strUserS, "", ""); } } catch { } }
/// <summary> /// 发送链接消息 /// </summary> /// <param name="QyID">企业ID</param> /// <param name="toUser">UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送</param> /// <param name="toParty">PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="toTag">TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数</param> /// <param name="agentId">企业应用的id,可在应用的设置页面查看</param> /// <param name="articles">图文信息内容,包括title(标题)、description(描述)、url(点击后跳转的链接。企业可根据url里面带的code参数校验员工的真实身份)和picurl(图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80。如不填,在客户端不显示图片)</param> /// <param name="safe">表示是否是保密消息,0表示否,1表示是,默认0</param> /// <returns></returns> public MassResult SendNewsMsg(string QyID, string toUser, string toParty, string toTag, string agentId, List <Article> articles, int safe = 0) { MassResult result = null; try { result = MassApi.SendNews(GetAccessToken(QyID), toUser, toParty, toTag, agentId, articles, safe); } catch (Exception ex) { LogWriter.Error(ex, string.Format("发送QyID为{0}的链接消息失败", QyID)); result = MassApi.SendNews(GetAccessToken(QyID, true), toUser, toParty, toTag, agentId, articles, safe); } if (result.errcode != ReturnCode_QY.请求成功) { LogWriter.Info(string.Format("发送QyID为{0}的链接消息失败,原因:{1}", QyID, result.errmsg)); } return(result); }
public void SendNew(string title, string description, string picUrl, string url, string toUser) { List <Article> articles = new List <Article>(); if (!string.IsNullOrWhiteSpace(picUrl) && picUrl.IndexOf("http") < 0) { picUrl = workSetting.Value.SendMsgHost + picUrl; } if (!string.IsNullOrWhiteSpace(url) && url.IndexOf("http") < 0) { url = workSetting.Value.SendMsgHost + url; } articles.Add(new Article() { Title = title, Description = description, PicUrl = picUrl, Url = url }); MassApi.SendNews(workSetting.Value.MsgAccessTokenKey, workSetting.Value.MsgAppId, articles, toUser); }
/// <summary> /// 发送消息 /// </summary> /// <param name="by">类型</param> /// <param name="value">用户,by是userid,支持10000每次,其他支持1000每次</param> /// <param name="applicationId"></param> /// <param name="data">内容</param> /// <param name="type">类型text和news,默认text</param> /// <param name="safe">true 保密 false 不保密</param> /// <returns></returns> public ActionResult SendMsg(string by, string value, string applicationId, string data, string type, string safe) { if (!VerifyParam("by,value,applicationId,data,type,safe")) { return(ErrMsg()); } var weChatConfig = GetWechatConfig(); var toUsrids = value; var token = Innocellence.Weixin.QY.CommonAPIs.AccessTokenContainer.GetToken(weChatConfig.WeixinCorpId, weChatConfig.WeixinCorpSecret); Expression <Func <SysAddressBookMember, bool> > predicate = (a) => a.DeleteFlag == 0 && a.Status == 1; PageCondition ConPage = new PageCondition() { PageIndex = 1, PageSize = 1000 }; if (value.IndexOf("|") >= 0) { var toUsers = value.Split('|'); switch (by) { case "id": var toInt = toUsers.Select(a => int.Parse(a)).ToArray(); predicate = predicate.AndAlso(a => toInt.Contains(a.Id)); break; case "hrcode": predicate = predicate.AndAlso(a => toUsers.Contains(a.EmployeeNo)); break; case "userid": predicate = null; break; case "wxid": predicate = predicate.AndAlso(a => toUsers.Contains(a.WeiXinId)); break; case "email": predicate = predicate.AndAlso(a => toUsers.Contains(a.Email)); break; default: predicate = null; break; } } else { switch (by) { case "id": predicate = predicate.AndAlso(a => a.Id == int.Parse(value)); break; case "hrcode": predicate = predicate.AndAlso(a => a.EmployeeNo == value); break; case "userid": predicate = null; break; case "wxid": predicate = predicate.AndAlso(a => a.WeiXinId == value); break; case "email": predicate = predicate.AndAlso(a => a.Email == value); break; default: predicate = null; break; } } if (predicate != null) { var lst = ((IAddressBookService)_BaseService).GetList <AddressBookMemberView>(predicate, ConPage).Select(a => a.UserId).ToArray(); if (lst.Length > 0) { toUsrids = string.Join("|", lst); } else { return(ErrMsg("用户未找到!" + value)); } } Weixin.QY.AdvancedAPIs.Mass.MassResult objResult; if (type == "news") { var lst = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Article> >(data); objResult = MassApi.SendNews(token, toUsrids, "", "", weChatConfig.WeixinAppId.ToString(), lst, safe == "true" ? 1 : 0); } else { objResult = MassApi.SendText(token, toUsrids, "", "", weChatConfig.WeixinAppId.ToString(), data, safe == "true" ? 1 : 0); } if (objResult.errcode == Weixin.ReturnCode_QY.请求成功) { return(Json(new { message = "", success = true }, JsonRequestBehavior.AllowGet)); } else { return(ErrMsg(objResult.errmsg)); } // return Json(new { success = true, message = "" }, JsonRequestBehavior.AllowGet); }
public static void SendNews(string agentId, List <string> userCodes, List <Article> articles) { userCodes.ForEach(userCode => MassApi.SendNews(GetToken(agentId), agentId, articles, userCode)); }
public static MassResult SendMsgQY(int iAppID, string strMsgType, string strUser, string strDept, string strTags, string strContent, List <ArticleInfoView> lstContent, int IsSec, bool isPreview = false) { MassResult objResult = null; try { var objConfig = WeChatCommonService.GetWeChatConfigByID(iAppID); log.Warn("SendMsgQY strUser:{0} strDept:{1} strTags:{2} Msg:{3} APPID:{4} strMsgType:{5} IsSec:{6} MsgCount:{7}", strUser, strDept, strTags, strContent, iAppID, strMsgType, IsSec, lstContent.Count); string strToken = (objConfig.IsCorp != null && !objConfig.IsCorp.Value) ? Innocellence.Weixin.MP.CommonAPIs.AccessTokenContainer.GetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret) : AccessTokenContainer.TryGetToken(objConfig.WeixinCorpId, objConfig.WeixinCorpSecret); var news = lstContent[0].NewsInfo; //保密图文消息需要用mpnews发送 strMsgType = strMsgType == "news" && IsSec == 1 ? "mpnews" : strMsgType; switch (strMsgType) { case "text": objResult = MassApi.SendText(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstContent[0].ArticleContent, IsSec); break; case "image": news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.IMAGE, news.ImageContent); objResult = MassApi.SendImage(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); break; case "news": #region news var wechatBaseUrl = CommonService.GetSysConfig("WeChatUrl", ""); var lstArticle = new List <Article>(); int i = 0; foreach (var objModel in lstContent) { if (i == 0) //位置不同,缩略图的比例不一样 { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_B"); } else { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_T"); } i++; lstArticle.Add(new Article() { Title = objModel.ArticleTitle, Description = objModel.ArticleComment, // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), PicUrl = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl).Replace("/\\", "/").Replace("\\", "/"), Url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl, objModel.Id, objModel.AppId, isPreview) }); } //var lstArticle = lstContent.Select(objModel => new Article() //{ // Title = objModel.ArticleTitle, // Description = objModel.ArticleComment, // // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), // //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), // PicUrl = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl).Replace("/\\", "/").Replace("\\", "/"), // Url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl, objModel.Id, objModel.AppId, isPreview) //}).ToList(); objResult = MassApi.SendNews(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstArticle, IsSec); #endregion break; case "mpnews": #region mpnews var wechatBaseUrl1 = CommonService.GetSysConfig("WeChatUrl", ""); var lstMpArticle = new List <MpNewsArticle>(); int ii = 0; foreach (var objModel in lstContent) { if (ii == 0) //位置不同,缩略图的比例不一样 { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_B"); } else { objModel.ImageCoverUrl = doGetFileCover(objModel.ImageCoverUrl, "_T"); } ii++; lstMpArticle.Add(new MpNewsArticle() { title = objModel.ArticleTitle, digest = objModel.ArticleComment, content = objModel.ArticleContent, author = objModel.UpdatedUserID, show_cover_pic = "1", thumb_media_id = GetMediaId(objModel.ImageCoverUrl, strToken), // PicUrl = aiv.ThumbImageId == null ? wechatBaseUrl+"/Content/img/LogoRed.png" : string.Format("{0}/Common/PushFile?id={1}&FileName={2}", wechatBaseUrl, aiv.ThumbImageId, aiv.ThumbImageUrl), //PicUrl = objModel.ImageCoverUrl == null ? wechatBaseUrl + "Content/img/LogoRed.png" : string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), // = string.Format("{0}{1}", wechatBaseUrl, objModel.ImageCoverUrl), content_source_url = string.Format("{0}/News/ArticleInfo/WxDetail/{1}?wechatid={2}&isPreview={3}", wechatBaseUrl1, objModel.Id, objModel.AppId, isPreview) }); } objResult = MassApi.SendMpNews(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), lstMpArticle, IsSec); #endregion break; case "video": //WechatCommon.GetMediaInfo(AutoReplyContentEnum.VIDEO, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.VIDEO, news.VideoContent); objResult = MassApi.SendVideo(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, news.NewsTitle, news.NewsComment, IsSec); //NewsToAttachments(news, "video", news.VideoContent); break; case "file": // WechatCommon.GetMediaInfo(AutoReplyContentEnum.FILE, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.FILE, news.FileSrc); objResult = MassApi.SendFile(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); // NewsToAttachments(news, "file", news.FileSrc); break; case "voice": // WechatCommon.GetMediaInfo(AutoReplyContentEnum.VOICE, news, news.AppId); news.MediaId = DoNewsInfo(news, objConfig, AutoReplyContentEnum.VOICE, news.SoundSrc); objResult = MassApi.SendVoice(strToken, strUser, strDept, strTags, objConfig.WeixinAppId.ToString(), news.MediaId, IsSec); //NewsToAttachments(news, "voice", news.SoundSrc); break; } } catch (Exception e) { log.Error(e); throw; } //在更新MsgLog时保持原来的错误处理, 即不进行任何处理 finally { UpdateMsgLog(lstContent, objResult); } return(objResult); }