/// <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); }
/// <summary> /// 图片消息 /// </summary> /// <param name="filePath"></param> /// <param name="strAPPID"></param> /// <param name="strUserS"></param> public void SendImage(string filePath, string strAPPID, string strUserS = "@all") { try { if (strUserS == "") { return; } if (Qyinfo.IsUseWX == "Y") { Senparc.Weixin.Work.AdvancedAPIs.Media.UploadTemporaryResultJson md = MediaApi.Upload(GetToken(), Senparc.Weixin.Work.UploadMediaFileType.image, filePath); if (md.media_id != "") { MassApi.SendImage(GetToken(), strUserS, "", "", strAPPID, md.media_id); } } } catch { } }
public static void SendImage(string agentId, List <string> userCodes, string mediaId) { userCodes.ForEach(userCode => MassApi.SendImage(GetToken(agentId), agentId, mediaId, 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); }
public override void ExecuteResult(ControllerContext context) { if (base.Content == null) { //使用IMessageHandler输出 if (_messageHandlerDocument == null) { throw new Innocellence.Weixin.Exceptions.WeixinException("执行WeixinResult时提供的MessageHandler不能为Null!", null); } if (_messageHandlerDocument.ResponseMessage == null) { //throw new Innocellence.Weixin.MP.WeixinException("ResponseMessage不能为Null!", null); } else { context.HttpContext.Response.ClearContent(); context.HttpContext.Response.ContentType = "text/xml"; foreach (var a in _messageHandlerDocument.ResponseMessage) { var config = WeChatCommonService.lstSysWeChatConfig.Find(aa => aa.WeixinCorpId == a.FromUserName); var strToken = WeChatCommonService.GetWeiXinToken(config.Id); if ((a as ResponseMessageBaseWechat).isSafe) { if (a is ResponseMessageImage) { var b = (ResponseMessageImage)a; MassApi.SendImage(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Image.MediaId, 1); } else if (a is ResponseMessageVideo) { var b = (ResponseMessageVideo)a; MassApi.SendVideo(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Video.MediaId, b.Video.Title, b.Video.Description, 1); } else if (a is ResponseMessageVoice) { var b = (ResponseMessageVoice)a; MassApi.SendVoice(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Voice.MediaId, 1); } else if (a is ResponseMessageText) { var b = (ResponseMessageText)a; MassApi.SendText(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Content, 1); } else if (a is ResponseMessageMpNews) { var b = (ResponseMessageMpNews)a; MassApi.SendMpNews(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.MpNewsArticles, 1); } else if (a is ResponseMessageNews) { log.Error("ExecuteResult ResponseMessageNews but Type is safe message!"); _messageHandlerDocument.FinalResponseDocument(a as ResponseMessageBaseWechat).Save(context.HttpContext.Response.OutputStream); } } else { _messageHandlerDocument.FinalResponseDocument(a as ResponseMessageBaseWechat).Save(context.HttpContext.Response.OutputStream); } } } } base.ExecuteResult(context); }
public override void ExecuteResult(ControllerContext context) { if (base.Content == null) { //使用IMessageHandler输出 if (_messageHandlerDocument == null) { throw new Innocellence.Weixin.Exceptions.WeixinException("执行WeixinResult时提供的MessageHandler不能为Null!", null); } if (_messageHandlerDocument.ResponseMessage == null) { //throw new Innocellence.Weixin.MP.WeixinException("FinalResponseDocument不能为Null!", null); } else { context.HttpContext.Response.ClearContent(); context.HttpContext.Response.ContentType = "text/xml"; // var xml = _messageHandlerDocument.FinalResponseDocument.ToString().Replace("\r\n", "\n"); //腾 foreach (var a in _messageHandlerDocument.ResponseMessage) { if ((a as ResponseMessageBaseWechat).isSafe) { var appid = (a as ResponseMessageBaseWechat).APPID; log.Debug("Before Send Response to wechat:{0} type:{1}", appid, a.MsgType); var config = WeChatCommonService.GetWeChatConfigByID(appid); var strToken = WeChatCommonService.GetWeiXinToken(config.Id); if (a is ResponseMessageImage) { var b = (ResponseMessageImage)a; MassApi.SendImage(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Image.MediaId, 1); } else if (a is ResponseMessageVideo) { var b = (ResponseMessageVideo)a; MassApi.SendVideo(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Video.MediaId, b.Video.Title, b.Video.Description, 1); } else if (a is ResponseMessageVoice) { var b = (ResponseMessageVoice)a; MassApi.SendVoice(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Voice.MediaId, 1); } else if (a is ResponseMessageText) { var b = (ResponseMessageText)a; MassApi.SendText(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.Content, 1); } else if (a is ResponseMessageMpNews) { var b = (ResponseMessageMpNews)a; MassApi.SendMpNews(strToken, b.ToUserName, "", "", config.WeixinAppId.ToString(), b.MpNewsArticles, 1); } else if (a is ResponseMessageNews) { log.Error("ExecuteResult ResponseMessageNews but Type is safe message!"); _messageHandlerDocument.FinalResponseDocument(a as ResponseMessageBaseWechat).Save(context.HttpContext.Response.OutputStream); } } else { string xml = _messageHandlerDocument.FinalResponseDocument(a as ResponseMessageBaseWechat).ToString().Replace("\r\n", "\n"); log.Debug("Start Send Response Message: length:{0}", xml.Length); if (!string.IsNullOrEmpty(xml)) { var bytes = Encoding.UTF8.GetBytes(xml); //的 context.HttpContext.Response.OutputStream.Write(bytes, 0, bytes.Length); //很 context.HttpContext.Response.Flush(); } log.Debug("End Response Message: length:{0}", xml.Length); } } } } }