public WxJsonResult SendMedia(Wx_App app, long mediaId, bool isToAll = true) { var media = Get(mediaId); var result = GroupMessageApi.SendGroupMessageByGroupId(app.AppId, null, media.MediaId, GetMessageType(media.Type), isToAll); WriteSendLog(mediaId, result); return(result); }
public void SendImageByGroupIdTest() { string file = ""; //文件路径,以下以图片为例 string groupId = ""; //分组Id var accessToken = AccessTokenContainer.GetAccessToken(_appId); var mediaId = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, file).media_id; var result = GroupMessageApi.SendGroupMessageByGroupId(accessToken, groupId, mediaId, GroupMessageType.image, false); Assert.IsTrue(result.msg_id.Length > 0); }
/// <summary> /// 根据分组群发卡券消息(目前该接口仅支持填入非自定义code的卡券,自定义code的卡券需先进行code导入后调用。 ) /// </summary> /// <param name="groupId">群发到的分组的group_id,参加用户管理中用户分组接口,若is_to_all值为true,可不填写group_id</param> /// <param name="value">群发卡券时传入cardId</param> /// <param name="isToAll">用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户</param> /// <returns></returns> public ActionResult SendGroupMessageByGroupId(string groupId, string value, bool isToAll) { string is_toall = isToAll.ToString(); if (isToAll == true) { if (!VerifyParam("value,is_toall")) { return(ErrMsg()); } } else { if (!VerifyParam("groupId,value,is_toall")) { return(ErrMsg()); } if (string.IsNullOrEmpty(groupId)) { return(ErrMsg("groupId不能为空!")); } } if (string.IsNullOrEmpty(value)) { return(ErrMsg("cardId不能为空!")); } var weChatConfig = GetWechatConfig(); var ret = GroupMessageApi.SendGroupMessageByGroupId(weChatConfig.WeixinAppId, weChatConfig.WeixinCorpSecret, groupId, value, GroupMessageType.wxcard, isToAll); if (ret.errcode == Weixin.ReturnCode.请求成功) { return(Json(new { message = "", success = true, msg_id = ret.msg_id })); } else { return(Json(new { message = ret.errmsg, success = false }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 群发消息 /// </summary> /// <param name="type"> /// 群发类型Text = 0, /// News = 1, /// Music = 2, /// Image = 3, /// Voice = 4, /// Video = 5 /// <param name="groupId">要发送的用户分组ID 发给所有的时候可以不输入</param> /// <returns>返回消息ID</returns> public string SendGroupMessageAll(int type, int groupId = -1) { var accessToken = AccessTokenContainer.TryGetAccessToken(AppId, AppSecret); SendResult result = null; bool isToAll = (groupId == -1); string imgFilePath = Server.MapPath("~/logo.png"); switch ((ResponseMsgType)type) { case ResponseMsgType.Text: result = GroupMessageApi.SendGroupMessageByGroupId(accessToken, groupId.ToString(), "测试的群发文本内容---来自北盟网校", GroupMessageType.text, isToAll); break; case ResponseMsgType.Image: UploadTemporaryMediaResult mediaImage = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, imgFilePath); result = GroupMessageApi.SendGroupMessageByGroupId(accessToken, groupId.ToString(), mediaImage.media_id, GroupMessageType.image, isToAll); break; case ResponseMsgType.News: UploadTemporaryMediaResult mediaResultImage = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, imgFilePath); NewsModel[] newsModels = new NewsModel[5]; for (int i = 0; i < 5; i++) { newsModels[i] = new NewsModel(); newsModels[i].title = "标题" + i; newsModels[i].author = "作者"; newsModels[i].thumb_media_id = mediaResultImage.media_id; newsModels[i].show_cover_pic = "http://weixin.bamn.cn/logo.png"; newsModels[i].content = "内容"; newsModels[i].content_source_url = "http://www.bamn.cn"; newsModels[i].digest = "描述"; } var mediaResultNews = MediaApi.UploadTemporaryNews(accessToken, 10000, newsModels); result = GroupMessageApi.SendGroupMessageByGroupId(accessToken, groupId.ToString(), mediaResultNews.media_id, GroupMessageType.mpnews, isToAll); break; } return(result.errcode + "-" + result.errmsg); }
private void SendGroupMessage(string tempStr, string accessToken, MediaList_OthersResult imgResult, string strAction) { string sendCountTxt = Server.MapPath("sendcount.txt"); var sendCount = int.Parse(File.ReadAllText(sendCountTxt)); NewsModel[] newsList = new NewsModel[sendCount]; var dataSevice = new DataAccessSerive(); var jobList = dataSevice.GetTopJobInfoList(); if (jobList.Count > 0) { List <Article> articles = new List <Article>(); int i = 0; string imgName; foreach (var job in jobList) { if (i == sendCount) { break; } imgName = "send" + i.ToString() + ".jpg"; string imgUrl = string.Format("{0}/image/{1}", WebConfigurationManager.AppSettings["domain"], imgName); var jobDetail = dataSevice.GetJobDetail(job.JobId); var news = new NewsModel(); news.author = ""; news.content = tempStr.Replace("[ComName]", job.ComName) .Replace("[ComBrief]", jobDetail.ComBrief) .Replace("[DetailPalce]", jobDetail.DetailPalce) .Replace("[LinkMan]", jobDetail.LinkMan) .Replace("[Phone]", jobDetail.Phone) .Replace("[JobName]", jobDetail.JobName) .Replace("[JobType]", jobDetail.JobType) .Replace("[LowMoney]", jobDetail.LowMoney) .Replace("[HrNum]", jobDetail.HrNum) .Replace("[Edu]", jobDetail.Edu) .Replace("[RegisterDate]", jobDetail.RegisterDate) .Replace("[EffectDate]", jobDetail.EffectDate) .Replace("[Other]", jobDetail.Other) .Replace("[Sex]", jobDetail.Sex) ; //news.content_source_url = WebConfigurationManager.AppSettings["domain"] + "/html/detail.html?id=" + job.JobId; news.content_source_url = WebConfigurationManager.AppSettings["website"]; news.digest = job.ComName + "诚聘" + job.JobName; news.show_cover_pic = "0"; news.thumb_media_id = imgResult.item.Find(p => p.name == imgName).media_id; news.title = job.ComName + "诚聘" + job.JobName; newsList[i] = news; i++; } UploadForeverMediaResult mediaResult = MediaApi.UploadNews(accessToken, 100000, newsList); try { GroupMessageApi.SendGroupMessageByGroupId (accessToken, "-1", mediaResult.media_id, GroupMessageType.mpnews, true); txtResult.Text += "提交成功一次" + strAction + " 推送成功"; } catch (Exception ex) { txtResult.Text += "提交成功一次订" + strAction + " 推送失败 " + ex.Message; } } }
public void PushNews(string accessToken, string webPath, string tempStr) { OpenIdResultJson json = UserApi.Get(accessToken, ""); var imgResult = MediaApi.GetOthersMediaList(accessToken, UploadMediaFileType.image, 0, 10000); var DircetPath = Environment.CurrentDirectory + "/image/"; if (!Directory.Exists(DircetPath)) { Directory.CreateDirectory(DircetPath); } for (int i = 0; i < 6; i++) { string imgName = string.Format("send{0}.jpg", i); if (imgResult.item.Find(p => p.name == imgName) == null) { var imgPath = webPath + "/image/" + imgName; var filePath = DircetPath + "/image/" + imgName; CommonUtility.HttpUtility.DownloadFile(imgPath, filePath); var mediaId = MediaApi.UploadForeverMedia(accessToken, filePath).media_id; } } imgResult = MediaApi.GetOthersMediaList(accessToken, UploadMediaFileType.image, 0, 10000); string sendCountTxtUrl = webPath + "/" + "sendcount.txt"; var sendCountTxt = CommonUtility.HttpUtility.Get(sendCountTxtUrl); var sendCount = int.Parse(sendCountTxt); NewsModel[] newsList = new NewsModel[sendCount]; var dataSevice = new DataAccessSerive(); var jobList = dataSevice.GetTopJobInfoList(); if (jobList.Count > 0) { List <Article> articles = new List <Article>(); int i = 0; string imgName; foreach (var job in jobList) { if (i == sendCount) { break; } string domain = ConfigurationManager.AppSettings.Get("domain"); string website = ConfigurationManager.AppSettings.Get("website"); imgName = "send" + i.ToString() + ".jpg"; string imgUrl = string.Format("{0}/image/{1}", domain, imgName); var jobDetail = dataSevice.GetJobDetail(job.JobId); var news = new NewsModel() { author = "", content = tempStr.Replace("[ComName]", job.ComName) .Replace("[ComBrief]", jobDetail.ComBrief) .Replace("[DetailPalce]", jobDetail.DetailPalce) .Replace("[LinkMan]", jobDetail.LinkMan) .Replace("[Phone]", jobDetail.Phone) .Replace("[JobName]", jobDetail.JobName) .Replace("[JobType]", jobDetail.JobType) .Replace("[LowMoney]", jobDetail.LowMoney) .Replace("[HrNum]", jobDetail.HrNum) .Replace("[Edu]", jobDetail.Edu) .Replace("[RegisterDate]", jobDetail.RegisterDate) .Replace("[EffectDate]", jobDetail.EffectDate) .Replace("[Other]", jobDetail.Other) .Replace("[Sex]", jobDetail.Sex) , //content_source_url = domain + "/html/detail.html?id=" + job.JobId, content_source_url = website, digest = job.ComName + "诚聘" + job.JobName, show_cover_pic = "0", thumb_media_id = imgResult.item.Find(p => p.name == imgName).media_id, title = job.ComName + "诚聘" + job.JobName }; newsList[i] = news; i++; } UploadForeverMediaResult mediaResult = MediaApi.UploadNews(accessToken, 100000, newsList); try { if (isDebug != "1") { GroupMessageApi.SendGroupMessageByGroupId(accessToken, "-1", mediaResult.media_id, GroupMessageType.mpnews, true); } Console.WriteLine("素材提交推送 推送成功"); logger.Info(DateTime.Now.ToString() + " 素材提交推送 推送成功"); } catch (Exception ex) { Console.WriteLine("素材提交推送 推送失败 " + ex.Message); var MailService = new MailSendFunc(); MailService.SendMail("*****@*****.**", ex.Message, "素材提交推送异常", mailCfg); logger.ErrorException(DateTime.Now.ToString() + "素材提交推送失败 " + ex.Message, ex); } } }
public ActionResult Send(WeiChat_GroupMessageLog model) { var res = new AjaxResponse { Success = true, Message = "发送成功!" }; if (string.IsNullOrWhiteSpace(model.MediaId)) { res.Success = false; res.Message = "请选择素材!"; } else { try { var messageType = GroupMessageType.image; switch (model.MessageType) { case SendMessageTypes.Text: messageType = GroupMessageType.text; break; case SendMessageTypes.Image: messageType = GroupMessageType.image; break; case SendMessageTypes.Voice: messageType = GroupMessageType.voice; break; case SendMessageTypes.Video: messageType = GroupMessageType.video; break; case SendMessageTypes.News: messageType = GroupMessageType.mpnews; break; default: break; } var q = db.WeiChat_Users.AsQueryable(); if (model.SexType != SexTypes.All) { var sexType = (WeChatSexTypes)((int)model.SexType - 1); q = q.Where(p => p.Sex == sexType); if (!string.IsNullOrEmpty(model.UserGroupIds)) { //var groupId = Convert.ToInt32(model.UserGroupIds); //q = q.Where(p => p.GroupIds.Contains(groupId)); q = q.Where(p => p.GroupIds.Contains(model.UserGroupIds)); } } switch (model.MessageType) { case SendMessageTypes.Text: case SendMessageTypes.Image: case SendMessageTypes.Voice: case SendMessageTypes.Video: case SendMessageTypes.News: { SetModel(model, default(long)); if (model.SexType != SexTypes.All) { var openIds = q.Select(p => p.OpenId).ToArray(); if ((openIds == null) || (openIds.Length == 0)) { res.Success = false; res.Message = "没有找到符合该条件的粉丝,请手动同步粉丝后或者修改条件后再试!"; } var result = GroupMessageApi.SendGroupMessageByOpenId(AccessToken, messageType, model.MediaId, 10000, openIds); if (result.errcode != ReturnCode.请求成功) { res.Success = false; res.Message = "发送失败!" + result.errmsg; } } else { var result = GroupMessageApi.SendGroupMessageByGroupId(AccessToken, model.UserGroupIds, model.MediaId, messageType, true); if (result.errcode != ReturnCode.请求成功) { res.Success = false; res.Message = "发送失败!" + result.errmsg; } } model.IsSuccess = res.Success; model.Message = res.Message; db.WeiChat_GroupMessageLogs.Add(model); db.SaveChanges(); break; } default: break; } } catch (Exception ex) { res.Success = false; res.Message = "发送失败!" + ex.Message; model.IsSuccess = res.Success; model.Message = res.Message; db.WeiChat_GroupMessageLogs.Add(model); db.SaveChanges(); } } return(Json(res)); }
/// <summary> /// 发送全部用户 /// </summary> /// <param name="account"></param> /// <param name="args"></param> /// <param name="yilidb"></param> /// <returns></returns> private async Task <SendResult> SendAllByGroup(MpAccountDto account, MpMessageDto args, IDBHelper yilidb) { SendResult result = null; if (args.MessageType == MpMessageType.text.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.Content, GroupMessageType.text, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.Content, GroupMessageType.text, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.video.ToString()) { var videoId = int.Parse(args.VideoID.ToString()); var video = yilidb.FindOne <MpMediaVideo>("SELECT TOP 1 * FROM MpMediaVideos WHERE ID=" + videoId, null, false); if (video == null) { throw new Exception(string.Format("视频{0}不存在", args.VideoName)); } try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.VideoMediaID, GroupMessageType.video, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.VideoMediaID, GroupMessageType.video, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.voice.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.VoiceMediaID, GroupMessageType.voice, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.VoiceMediaID, GroupMessageType.voice, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.image.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.ImageMediaID, GroupMessageType.image, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.ImageMediaID, GroupMessageType.image, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.mpnews.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.ArticleMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.ArticleMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.mpmultinews.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken)).access_token, "", args.ArticleGroupMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId((await StaticObjects.GetAccessToken(account.TaskAccessToken, 1)).access_token, "", args.ArticleGroupMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } return(result); }
/// <summary> /// 发送全部用户 /// </summary> /// <param name="account"></param> /// <param name="args"></param> /// <returns></returns> private async Task <SendResult> SendAllByGroup(MpAccountDto account, MpMessageDto args) { SendResult result = null; if (args.MessageType == MpMessageType.text.ToString()) { try { LogWriter.Info("群发消息开始 全部开始"); result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.Content, GroupMessageType.text, true, clientmsgid: $"MpMessage{args.Id}"); LogWriter.Info("群发消息开始 全部结束"); } catch (Exception ex) { LogWriter.Error("群发消息开始 第一次报错", ex); try { LogWriter.Info("群发消息开始 全部开始2"); result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.Content, GroupMessageType.text, true, clientmsgid: $"MpMessage{args.Id}"); LogWriter.Info("群发消息开始 全部结束2"); } catch (Exception ex2) { LogWriter.Error("群发消息开始 第二次报错", ex2); } } } else if (args.MessageType == MpMessageType.video.ToString()) { var videoId = int.Parse(args.VideoID.ToString()); var video = await _mpMediaVideoAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> { Id = videoId }); if (video == null) { throw new Exception(string.Format("视频{0}不存在", args.VideoName)); } try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.VideoMediaID, GroupMessageType.video, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.VideoMediaID, GroupMessageType.video, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.voice.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.VoiceMediaID, GroupMessageType.voice, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.VoiceMediaID, GroupMessageType.voice, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.image.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.ImageMediaID, GroupMessageType.image, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.ImageMediaID, GroupMessageType.image, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.mpnews.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.ArticleMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.ArticleMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } else if (args.MessageType == MpMessageType.mpmultinews.ToString()) { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), "", args.ArticleGroupMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { try { result = GroupMessageApi.SendGroupMessageByGroupId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), "", args.ArticleGroupMediaID, GroupMessageType.mpnews, true, clientmsgid: $"MpMessage{args.Id}"); } catch { } } } return(result); }