/// <summary> /// 推送纯文字 /// </summary> public IResponseMessageBase GetResponseMessageTxt(RequestMessageText requestMessage, int ruleId, int accountId) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageText>(requestMessage); string openid = requestMessage.FromUserName; string token = ConvertDateTimeInt(DateTime.Now).ToString(); //responseMessage.Content = new BLL.weixin_request_content().GetContent(ruleId); WeixinRequestContent ctx = WeixinRequestContent.Find(WeixinRequestContent._.RuleId == ruleId); if (ctx != null) { responseMessage.Content = ctx.Content; WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = requestMessage.Content, ResponseType = "text", ReponseContent = responseMessage.Content, XmlContent = requestMessage.ToUserName }; entity.Insert(); } //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), requestMessage.Content, "text", responseMessage.Content, requestMessage.ToUserName); return(responseMessage); }
/// <summary> /// 处理语音请求 /// </summary> public IResponseMessageBase GetResponseMessageeMusic(RequestMessageEventBase requestMessage, int ruleId, int accountId) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageMusic>(requestMessage); string EventName = ""; if (requestMessage.Event.ToString().Trim() != "") { EventName = requestMessage.Event.ToString(); } //else if (requestMessage.EventKey != null) //{ // EventName += requestMessage.EventKey.ToString(); //} //Model.weixin_request_content model = new BLL.weixin_request_content().GetModel(ruleId); WeixinRequestContent model = WeixinRequestContent.Find(WeixinRequestContent._.RuleId == ruleId); if (model == null) { //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "music", "-1", requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "music", ReponseContent = "-1", XmlContent = requestMessage.ToUserName }; entity.Insert(); } else { if (model.MediaURL.Contains("http://")) { responseMessage.Music.MusicUrl = model.MediaURL; } else { //因为安装目录是以/开头,所以去掉,以免出现双斜杆 responseMessage.Music.MusicUrl = Utils.GetServerUrl() + model.MediaURL; } responseMessage.Music.Title = model.Title; responseMessage.Music.Description = model.Content; //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "music", "音乐链接:" + model.media_url + "|标题:" + model.title + "|描述:" + model.content, requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "music", ReponseContent = "音乐链接:" + model.MediaURL + "|标题:" + model.Title + "|描述:" + model.Content, XmlContent = requestMessage.ToUserName }; entity.Insert(); } return(responseMessage); }
/// <summary> /// 推送单张图片 /// </summary> public IResponseMessageBase GetResponseMessageSingleImagesContent(RequestMessageEventBase requestMessage, int ruleId, int accountId) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageImage>(requestMessage); string EventName = ""; var locationService = new LocationService(); NewLife.Log.XTrace.WriteLine("相应单图片回复"); WeixinRequestContent model = WeixinRequestContent.Find(WeixinRequestContent._.RuleId == ruleId); if (model == null) { NewLife.Log.XTrace.WriteLine("没找到单图片回复规则"); //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "music", "-1", requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "image", ReponseContent = "-1", XmlContent = requestMessage.ToUserName }; entity.Insert(); } else { if (string.IsNullOrEmpty(model.MediaURL)) { //如果没有mediaid 则先上传 string imgUrl = "~/wwwroot" + model.ImgURL; var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(siteConfig.AppId, UploadMediaFileType.image, ServerUtility.ContentRootMapPath(imgUrl)); model.MediaURL = uploadResult.media_id; model.Update(); NewLife.Log.XTrace.WriteLine("上传素材,返回mediaid:" + uploadResult.media_id); } Image img = new Image(); img.MediaId = model.MediaURL; responseMessage.CreateTime = DateTime.Now; responseMessage.Image = img; NewLife.Log.XTrace.WriteLine("回复单图片,mediaid" + img.MediaId + ";回复用户openid:" + responseMessage.FromUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "image", ReponseContent = "图片:" + model.MediaURL + "|标题:" + model.Title, XmlContent = requestMessage.ToUserName }; entity.Insert(); } return(responseMessage); }
public ActionResult NotFindKeywordAutoReply() { WeixinRequestRule ruleModel = WeixinRequestRule.Find(WeixinRequestRule._.RequestType == (int)WeixinRequestRule.XRequestType.NotFindKeyword);//获取规则 if (ruleModel == null) { ruleModel = new WeixinRequestRule(); ruleModel.RequestType = (int)WeixinRequestRule.XRequestType.NotFindKeyword; ruleModel.ResponseType = (int)WeixinRequestRule.XResponseType.Text; ruleModel.RuleName = "未找到关键词自动回复"; ruleModel.IsDefault = 1; ruleModel.Insert(); //增加Response WeixinRequestContent content = new WeixinRequestContent() { RuleId = ruleModel.Id, Content = "未找到关键词自动回复", }; content.Insert(); ruleModel = WeixinRequestRule.Find(WeixinRequestRule._.RequestType == (int)WeixinRequestRule.XRequestType.NotFindKeyword);//重新获取规则 } switch (ruleModel.ResponseType) { case 0: //文本 ViewBag.txtContent = ruleModel.ListContent[0].Content; break; case 1: //多图 break; case 2: //语音 ViewBag.txtSoundTitle = ruleModel.ListContent[0].Title; ViewBag.txtSoundUrl = ruleModel.ListContent[0].MediaURL; ViewBag.txtSoundContent = ruleModel.ListContent[0].Content; break; case 8: //单图 ViewBag.txtSingleImage = ruleModel.ListContent[0].ImgURL; break; } Core.Admin.WriteLogActions("查看公众号未找到关键字自动回复;"); return(View(ruleModel)); }
public IActionResult SubscribeReply() { WeixinRequestRule ruleModel = WeixinRequestRule.Find(WeixinRequestRule._.RequestType == (int)WeixinRequestRule.XRequestType.Flow);//获取规则 if (ruleModel == null) { ruleModel = new WeixinRequestRule(); ruleModel.RequestType = (int)WeixinRequestRule.XRequestType.Flow; ruleModel.ResponseType = (int)WeixinRequestRule.XResponseType.Text; ruleModel.RuleName = "关注自动回复"; ruleModel.IsDefault = 1; ruleModel.Insert(); //增加Response WeixinRequestContent content = new WeixinRequestContent() { RuleId = ruleModel.Id, Content = "关注公众号自动回复文本" }; content.Insert(); } switch (ruleModel.ResponseType) { case 0: //文本 ViewBag.txtContent = ruleModel.ListContent[0].Content; break; case 1: //多图 break; case 2: //语音 ViewBag.txtSoundTitle = ruleModel.ListContent[0].Title; ViewBag.txtSoundUrl = ruleModel.ListContent[0].MediaURL; ViewBag.txtSoundContent = ruleModel.ListContent[0].Content; break; case 8: //单图 ViewBag.txtSingleImage = ruleModel.ListContent[0].ImgURL; break; } Admin.WriteLogActions("查看公众号关注自动回复;"); return(View(ruleModel)); }
/// <summary> /// 推送纯文字 /// </summary> public IResponseMessageBase GetResponseMessageTxt(RequestMessageEventBase requestMessage, int ruleId, int accountId) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageText>(requestMessage); string openid = requestMessage.FromUserName; string token = ConvertDateTimeInt(DateTime.Now).ToString(); WeixinRequestContent rct = WeixinRequestContent.Find(WeixinRequestContent._.RuleId == ruleId); responseMessage.Content = rct != null ? rct.Content : "";// new BLL.weixin_request_content().GetContent(ruleId); NewLife.Log.XTrace.WriteLine("文本自动关注回复:" + ruleId + "||" + responseMessage.Content); string EventName = ""; if (requestMessage.Event.ToString().Trim() != "") { EventName = requestMessage.Event.ToString(); } //else if (requestMessage.EventKey != null) //{ // EventName += requestMessage.EventKey.ToString(); //} //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "text", responseMessage.Content, requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "text", ReponseContent = responseMessage.Content, XmlContent = requestMessage.ToUserName }; entity.Insert(); return(responseMessage); }
public IActionResult EditClickReplyRule(WeixinRequestRule entity) { WeixinRequestRule model = WeixinRequestRule.Find(WeixinRequestRule._.RequestType == (int)WeixinRequestRule.XRequestType.CustomMenu & WeixinRequestRule._.Id == entity.Id);//获取规则 if (model == null) { tip.Message = "系统找不到本记录"; return(Json(tip)); } model.ResponseType = entity.ResponseType; model.Keywords = entity.Keywords; model.IsDefault = 1; int defaultDetailId = int.Parse(Request.Form["hdfTID"]);//默认一条的ID。肯定至少有一条 int responseType = model.ResponseType; IList <WeixinRequestContent> ls = new List <WeixinRequestContent>(); if (responseType == 0) //纯文本 { string txtContent = Request.Form["txtContent"]; if (string.IsNullOrEmpty(txtContent)) { tip.Message = "文本回复,文本不能为空!"; return(Json(tip)); } model.ResponseType = (int)WeixinRequestRule.XResponseType.Text;//回复的类型:文本1,图文2,语音3,视频4,第三方接口5 ls.Add(new WeixinRequestContent { RuleId = model.Id, Content = txtContent.Trim(), Id = defaultDetailId }); } else if (responseType == (int)WeixinRequestRule.XResponseType.Image) //图文 { #region 赋值规则图片 string[] itemIdArr = Request.Form["item_id"]; string[] itemTitleArr = Request.Form["item_title"]; string[] itemContentArr = Request.Form["item_imginfo"]; string[] itemImgUrlArr = Request.Form["item_imgurl"]; string[] itemLinkUrlArr = Request.Form["item_linkurl"]; if (itemIdArr != null && itemTitleArr != null && itemImgUrlArr != null && itemLinkUrlArr != null && itemContentArr != null) { if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemImgUrlArr.Length) && (itemImgUrlArr.Length == itemLinkUrlArr.Length)) { for (int i = 0; i < itemIdArr.Length; i++) { if (!string.IsNullOrEmpty(itemTitleArr[i])) { WeixinRequestContent modelt = new WeixinRequestContent(); modelt.Id = Utils.StrToInt(itemIdArr[i].Trim(), 0); modelt.Title = itemTitleArr[i].Trim(); modelt.Content = itemContentArr[i].Trim(); modelt.ImgURL = itemImgUrlArr[i].Trim(); modelt.LinkUrl = itemLinkUrlArr[i].Trim(); modelt.RuleId = model.Id; ls.Add(modelt); } } //model.contents = ls; } } #endregion if (ls == null || ls.Count < 1) { tip.Message = "图文列表,图文项目不能为空!"; return(Json(tip)); } } else if (responseType == (int)WeixinRequestRule.XResponseType.Video) //语音 { string txtSoundTitle = Request.Form["txtSoundTitle"]; if (string.IsNullOrEmpty(txtSoundTitle)) { tip.Message = "语音回复,语音标题不能为空!"; return(Json(tip)); } string txtSoundUrl = Request.Form["txtSoundUrl"]; if (string.IsNullOrEmpty(txtSoundUrl)) { tip.Message = "语音回复,语音地址不能为空!"; return(Json(tip)); } string txtSoundContent = Request.Form["txtSoundContent"]; ls.Add(new WeixinRequestContent { RuleId = model.Id, MediaURL = txtSoundUrl.Trim(), Title = txtSoundTitle, Id = defaultDetailId, Content = txtSoundContent }); } else if (responseType == (int)WeixinRequestRule.XResponseType.SingleImage) //单图片 { string txtSingleImage = Request.Form["txtSingleImage"]; if (string.IsNullOrEmpty(txtSingleImage)) { tip.Message = "单图片回复,图片地址不能为空!"; return(Json(tip)); } ls.Add(new WeixinRequestContent { RuleId = model.Id, ImgURL = txtSingleImage.Trim(), Id = defaultDetailId }); } model.Save(); IList <WeixinRequestContent> oldwc = model.ListContent; if (oldwc != null && oldwc.Count > 0) { foreach (WeixinRequestContent p in ls) { if (oldwc.Find(s => s.Id == p.Id) != null) { oldwc.Remove(oldwc.Find(s => s.Id == p.Id)); } } if (oldwc != null && oldwc.Count > 0) { oldwc.Delete(); } } if (ls != null && ls.Count > 0) { foreach (var item in ls) { item.RuleId = model.Id; } } ls.Save(); tip.Message = "修改点击自定义菜单事件自动回复成功!"; tip.Status = JsonTip.SUCCESS; tip.ReturnUrl = "close"; Admin.WriteLogActions("修改点击自定义菜单事件自动回复;"); return(Json(tip)); }
public IActionResult AddTextReplyRule(WeixinRequestRule model) { model.IsDefault = 1; int responseType = model.ResponseType; model.RequestType = (int)WeixinRequestRule.XRequestType.Text;//文本请求 IList <WeixinRequestContent> ls = new List <WeixinRequestContent>(); if (responseType == 0) //纯文本 { string txtContent = Request.Form["txtContent"]; if (string.IsNullOrEmpty(txtContent)) { tip.Message = "文本回复,文本不能为空!"; return(Json(tip)); } model.ResponseType = (int)WeixinRequestRule.XResponseType.Text;//回复的类型:文本1,图文2,语音3,视频4,第三方接口5 ls.Add(new WeixinRequestContent { RuleId = model.Id, Content = txtContent.Trim() }); } else if (responseType == (int)WeixinRequestRule.XResponseType.Image) //图文 { #region 赋值规则图片 string[] itemIdArr = Request.Form["item_id"]; string[] itemTitleArr = Request.Form["item_title"]; string[] itemContentArr = Request.Form["item_imginfo"]; string[] itemImgUrlArr = Request.Form["item_imgurl"]; string[] itemLinkUrlArr = Request.Form["item_linkurl"]; if (itemIdArr != null && itemTitleArr != null && itemImgUrlArr != null && itemLinkUrlArr != null && itemContentArr != null) { if ((itemIdArr.Length == itemTitleArr.Length) && (itemTitleArr.Length == itemImgUrlArr.Length) && (itemImgUrlArr.Length == itemLinkUrlArr.Length)) { for (int i = 0; i < itemIdArr.Length; i++) { if (!string.IsNullOrEmpty(itemTitleArr[i])) { WeixinRequestContent modelt = new WeixinRequestContent(); modelt.Id = Utils.StrToInt(itemIdArr[i].Trim(), 0); modelt.Title = itemTitleArr[i].Trim(); modelt.Content = itemContentArr[i].Trim(); modelt.ImgURL = itemImgUrlArr[i].Trim(); modelt.LinkUrl = itemLinkUrlArr[i].Trim(); modelt.RuleId = model.Id; ls.Add(modelt); } } //model.contents = ls; } } #endregion if (ls == null || ls.Count < 1) { tip.Message = "图文列表,图文项目不能为空!"; return(Json(tip)); } } else if (responseType == (int)WeixinRequestRule.XResponseType.Video) //语音 { string txtSoundTitle = Request.Form["txtSoundTitle"]; if (string.IsNullOrEmpty(txtSoundTitle)) { tip.Message = "语音回复,语音标题不能为空!"; return(Json(tip)); } string txtSoundUrl = Request.Form["txtSoundUrl"]; if (string.IsNullOrEmpty(txtSoundUrl)) { tip.Message = "语音回复,语音地址不能为空!"; return(Json(tip)); } string txtSoundContent = Request.Form["txtSoundContent"]; ls.Add(new WeixinRequestContent { RuleId = model.Id, MediaURL = txtSoundUrl.Trim(), Title = txtSoundTitle, Content = txtSoundContent }); } else if (responseType == (int)WeixinRequestRule.XResponseType.SingleImage) //单图片 { string txtSingleImage = Request.Form["txtSingleImage"]; if (string.IsNullOrEmpty(txtSingleImage)) { tip.Message = "单图片回复,图片地址不能为空!"; return(Json(tip)); } ls.Add(new WeixinRequestContent { RuleId = model.Id, ImgURL = txtSingleImage.Trim() }); } model.Insert(); IList <WeixinRequestContent> oldwc = model.ListContent; if (oldwc != null && oldwc.Count > 0) { foreach (WeixinRequestContent p in ls) { if (oldwc.Find(s => s.Id == p.Id) != null) { oldwc.Remove(oldwc.Find(s => s.Id == p.Id)); } } if (oldwc != null && oldwc.Count > 0) { oldwc.Delete(); } } if (ls != null && ls.Count > 0) { foreach (var item in ls) { item.RuleId = model.Id; } } ls.Save(); tip.Message = "添加文本关键字回复规则成功!"; tip.Status = JsonTip.SUCCESS; tip.ReturnUrl = "close"; Core.Admin.WriteLogActions("添加文本关键字回复规则;"); return(Json(tip)); }
/// <summary> /// 推送多图文 /// </summary> public IResponseMessageBase GetResponseMessageNews(RequestMessageEventBase requestMessage, int ruleId, int accountId) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageNews>(requestMessage); string openid = requestMessage.FromUserName; string token = ConvertDateTimeInt(DateTime.Now).ToString(); Senparc.NeuChar.Entities.Article article; List <Senparc.NeuChar.Entities.Article> artList = new List <Senparc.NeuChar.Entities.Article>(); //IList<Model.weixin_request_content> twList = new BLL.weixin_request_content().GetModelList(10, ruleId, string.Empty); IList <WeixinRequestContent> twList = WeixinRequestContent.FindAll(WeixinRequestContent._.RuleId == ruleId, null, null, 0, 10); foreach (WeixinRequestContent modelt in twList) { article = new Senparc.NeuChar.Entities.Article(); article.Title = modelt.Title; article.Description = modelt.Content; article.Url = GetWXApiUrl(modelt.LinkUrl, token, openid); if (string.IsNullOrEmpty(modelt.ImgURL)) { article.PicUrl = string.Empty; } else { if (modelt.ImgURL.Contains("http://")) { article.PicUrl = modelt.ImgURL; } else { //因为安装目录是以/开头,所以去掉,以免出现双斜杆 article.PicUrl = Utils.GetServerUrl() + modelt.ImgURL; } } artList.Add(article); } string EventName = ""; if (requestMessage.Event.ToString().Trim() != "") { EventName = requestMessage.Event.ToString(); } //else if (requestMessage.EventKey != null) //{ // EventName += requestMessage.EventKey.ToString(); //} if (artList.Count == 0) { //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "txtpic", "-1", requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "txtpic", ReponseContent = "-1", XmlContent = requestMessage.ToUserName }; entity.Insert(); } else { //new BLL.weixin_response_content().Add(accountId, requestMessage.FromUserName, requestMessage.MsgType.ToString(), EventName, "txtpic", "这次发了" + artList.Count + "条图文", requestMessage.ToUserName); WeixinResponseContent entity = new WeixinResponseContent() { OpenId = requestMessage.FromUserName, RequestType = requestMessage.MsgType.ToString(), RequestContent = EventName, ResponseType = "txtpic", ReponseContent = "这次发了" + artList.Count + "条图文", XmlContent = requestMessage.ToUserName }; entity.Insert(); } responseMessage.Articles.AddRange(artList); return(responseMessage); }