public async Task <Result <string> > PrevKeyword([FromBody] WxAutoKeywordShow item) { if (!string.IsNullOrEmpty(item.OpenId)) { return(await wxAutoKeywordHelper.AutoRespond(item, item.OpenId)); } else { if (string.IsNullOrEmpty(item.PrevNick)) { return(new Result <string> { Message = "请输入要预览的微信用户昵称!" }); } // 用户信息 var wxUser = await repo.FirstOrDefaultAsync <WxUserInfo>(x => x.NickName == item.PrevNick && x.IsSubscribe == 1); if (wxUser == null) { return(new Result <string> { Message = "昵称对应的微信用户不存在!" }); } return(await wxAutoKeywordHelper.AutoRespond(item, wxUser.OpenId)); } }
/// <summary> /// 处理关键字 /// </summary> /// <param name="gzhClient"></param> /// <param name="gzhServer"></param> /// <param name="keyword"></param> /// <returns></returns> public async Task <string> DealWithKeyWord(string gzhClient, string gzhServer, string keyword) { // 当前时间 var dtNow = DateTime.Now; var timeStamp = ComHelper.ConvertDateTimeInt(dtNow); // 当前微信用户信息 var wxUser = await repo.FirstOrDefaultAsync <WxUserInfo>(x => x.OpenId == gzhClient); // 自动应答的内容 WxAutoKeywordShow autoKeyword = null; // 超过1条的相同关键字的自动回复 改用客服消息发送 var lstOthers = new List <WxAutoKeywordShow> { }; if (keyword == "openid") { return(await wxAutoComResponse.SendWxText(gzhClient, gzhServer, timeStamp, "您的OpenId为:" + gzhClient)); } else { var sql = string.Format(@" SELECT * FROM WxAutoKeyword WHERE IsDel = 0 AND CONCAT('、',KeyWords,'、') LIKE '%、{0}、%' ORDER BY CreateTime DESC LIMIT 5 ", keyword); var lstKeywords = await repo.SqlQueryAsync <WxAutoKeywordShow>(sql); if (lstKeywords.Count > 0) { autoKeyword = lstKeywords[0]; } // 发送客服消息 if (lstKeywords.Count > 1) { lstOthers = lstKeywords.Skip(1).Take(4).ToList(); } } // 公众号自动应答回复第一条关键字消息 if (autoKeyword == null) { var msg = "DealQyText:无法找到对应关键字活动:" + keyword; logHelper.Debug(msg); return(wxAutoComResponse.ResponseOK()); } // 其他发送客服消息(不等待) SendKfAutoKeyword(wxUser, lstOthers); // 根据不同类型推送消息 return(await SendMsgByPush(gzhClient, gzhServer, autoKeyword, wxUser)); }
public async Task <Result <string> > SaveAutoKeyword([FromBody] WxAutoKeywordShow item) { try { // 多图文 if (item.LstSelArtIds != null && item.LstSelArtIds.Count > 0) { item.ArtIds = string.Join(",", item.LstSelArtIds); } // 视频 if (item.ContentType == Enum_ApMsgType.Video.ToString()) { var vedio = await repo.FirstOrDefaultAsync <WxMediaShow>(x => x.MediaId == item.MediaId); if (vedio != null) { item.VideoTitle = vedio.Title; item.VideoDescription = vedio.Introduction; } } var dtNow = DateTime.Now; var user = currentUser.UserName; if (!item.CreateTime.HasValue) { item.Creater = user; item.CreateTime = dtNow; } item.Updater = user; item.UpdateTime = dtNow; await repo.SaveAsync(item); return(new Result <string> { IsSucc = true, Message = "保存自动回复关键字配置成功!" }); } catch (Exception ex) { logHelper.Error("SaveAutoKeywords:保存自动回复关键字配置失败:" + ex.Message + " " + ex.StackTrace); } return(new Result <string> { Message = "保存自动回复关键字配置失败!" }); }
/// <summary> /// 把永久图文转成自动应答图文(公众号自动回复) /// </summary> /// <param name="autoKeyword"></param> /// <returns></returns> public async Task <List <PubNewsArticle> > GetResponseWxNews(WxAutoKeywordShow autoKeyword) { var lstArt = new List <PubNewsArticle>(); var lstNewDets = await repo.QueryAsync <WxNewsDetailShow>(x => x.IsDel == 0 && x.MediaId == autoKeyword.MediaId); foreach (var det in lstNewDets) { var temp = new PubNewsArticle(); temp.Title = det.Title; temp.Description = det.Digest; temp.PicUrl = det.ThumbUrl; temp.Url = det.WxUrl; lstArt.Add(temp); } return(lstArt); }
/// <summary> /// GzhMarket后台文章 转化成 微信图文(公众号自动回复) /// </summary> /// <param name="autoKeyword"></param> /// <param name="lstIds"></param> /// <returns></returns> public async Task <List <PubNewsArticle> > GetResponseBackNews(WxAutoKeywordShow autoKeyword, List <long> lstIds) { // 微信图文列表 var lstWxArts = new List <PubNewsArticle>(); // 选定的GzhMarket后台图文列表 var lstArticles = await repo.QueryAsync <WxBackNewsShow>(x => x.IsDel == 0 && lstIds.Contains(x.Id)); if (autoKeyword.SendType == 0) { // 多图文随机推送一篇 var randArt = ComHelper.GetRandomVal(lstArticles); lstArticles = new List <WxBackNewsShow> { randArt }; } // 后台图文转微信图文 foreach (var art in lstArticles) { var news = new PubNewsArticle(); news.Title = art.Name; news.Description = art.SecondName; // 图片路径 if (!string.IsNullOrEmpty(art.ImgUrlVir)) { news.PicUrl = ComHelper.UpdImgAbsPath(WebRoot, art.ImgUrlVir); } // 网址 if (!string.IsNullOrEmpty(art.ArticleUrl)) { news.Url = art.ArticleUrl; if (art.ArticleUrl.StartsWith("~/")) { news.Url = ComHelper.UpdImgAbsPath(WebRoot, art.ArticleUrl); } } lstWxArts.Add(news); } return(lstWxArts); }
/// <summary> /// 发送红包消息(关键字回复) /// </summary> /// <param name="item"></param> /// <param name="openId"></param> /// <returns></returns> public async Task <Result <string> > SendAutoKeywordRedPack(WxAutoKeywordShow item, string openId) { // 判断红包个数 var sendCount = repo.SqlQuerySingle <int>($" SELECT COUNT(1) FROM WxAutoKeywordDetail WHERE AutoId = {item.Id} "); if (sendCount >= item.RedCount) { return(new Result <string> { Message = "很抱歉,你来晚了,红包已经发送完毕!" }); } // 判断用户是否领取过 var exist = await repo.FirstOrDefaultAsync <WxAutoKeywordDetail>(x => x.AutoId == item.Id && x.Opend == openId); if (exist != null) { return(new Result <string> { Message = "你已经领取过了,请勿重复领取,把机会留给没有参与的用户!" }); } // 发送红包 var redRes = RedpackApi.SendPack(openId, item.RedAmount, item.RedAct, item.RedWish, item.RedWish, ""); logHelper.Debug("SendPack:发送红包结果:" + ComHelper.JsonSerialize(redRes)); if (redRes.IsSucc) { //红包发送成功 var autoHis = await SaveAutoKeywordHis(item.Id, openId); logHelper.Debug("SendPack:发送红包成功:" + autoHis.JsonSerialize()); return(new Result <string> { IsSucc = true, Message = "发送红包成功!" }); } else { var msg = "红包发送失败:" + redRes.err_code + " " + redRes.err_code_des; logHelper.Error("SendPack:" + msg); return(new Result <string> { Message = msg }); } }
/// <summary> /// 后台图文转换成客服图文消息(关键字回复) /// </summary> /// <param name="item"></param> /// <returns></returns> public async Task <List <PubKfArticle> > GetAutoKeywordBackNews(WxAutoKeywordShow item) { var lstArts = new List <PubKfArticle>(); if (!string.IsNullOrEmpty(item.ArtIds)) { // 选定的图文ids item.LstSelArtIds = item.ArtIds.StrSplitList().Select(x => Convert.ToInt64(x)).ToList(); if (item.LstSelArtIds != null && item.LstSelArtIds.Count > 0) { // 最终发送的图文列表 var lstBackAftNews = new List <WxBackNewsShow>(); // 选定的图文列表 var lstBackNews = await repo.QueryAsync <WxBackNewsShow>(x => x.IsDel == 0 && item.LstSelArtIds.Contains(x.Id)); if (item.SendType == 0) { // 随机图文 var temp = ComHelper.GetRandomVal(lstBackNews); lstBackAftNews.Add(temp); } else { // 多图文 lstBackAftNews = lstBackNews; } // 转换图文 foreach (var news in lstBackAftNews) { var temp = new PubKfArticle(); temp.title = news.Name; temp.description = news.SecondName; temp.url = ComHelper.UpdImgAbsPath(WebRoot, news.ArticleUrl); temp.picurl = ComHelper.UpdImgAbsPath(WebRoot, news.ImgUrlVir); lstArts.Add(temp); } } } return(lstArts); }
public async Task <Result <string> > UpdAutoKeyword([FromBody] WxAutoKeywordShow item) { try { item.IsDel = 1 - item.IsDel; item.Updater = currentUser.UserName; item.UpdateTime = DateTime.Now; await repo.UpdateAsync(item, new List <string> { "IsDel", "Updater", "UpdateTime" }); return(new Result <string> { IsSucc = true, Message = "更新关键字状态成功!" }); } catch (Exception ex) { logHelper.Error("UpdAutoKe:更新关键字状态失败:" + ex.Message + " " + ex.StackTrace); } return(new Result <string> { Message = "更新关键字状态失败!" }); }
/// <summary> /// 根据不同类型推送消息 /// </summary> /// <param name="gzhClient"></param> /// <param name="gzhSever"></param> /// <param name="autoKeyword"></param> /// <param name="wxUser"></param> /// <returns></returns> public async Task <string> SendMsgByPush(string gzhClient, string gzhSever, WxAutoKeywordShow autoKeyword, WxUserInfo wxUser) { logHelper.Debug("SendMsgByPush:autoPushing:" + autoKeyword.JsonSerialize()); var dtNow = DateTime.Now; var timeStamp = ComHelper.ConvertDateTimeInt(dtNow); var apType = ComHelper.GetEnumValueByStr <Enum_ApMsgType>(autoKeyword.ContentType); switch (apType) { case Enum_ApMsgType.Text: { var msg = autoKeyword.TextContent.Replace("{nickname}", wxUser.NickName); logHelper.Debug("SendMsgByPush:Msg:" + msg); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, msg)); } case Enum_ApMsgType.Image: { var msg = PubMsgApi.BuildImageMsg(gzhClient, gzhSever, autoKeyword.MediaId, timeStamp); return(await wxAutoComResponse.AutoMsgResponse(msg)); } case Enum_ApMsgType.Voice: { var msg = PubMsgApi.BuildVoiceMsg(gzhClient, gzhSever, autoKeyword.MediaId, timeStamp); return(await wxAutoComResponse.AutoMsgResponse(msg)); } case Enum_ApMsgType.News: { var lstArt = await wxAutoConvertHelper.GetResponseWxNews(autoKeyword); var msg = PubMsgApi.BuildArticleMsg(gzhClient, gzhSever, timeStamp, lstArt); return(await wxAutoComResponse.AutoMsgResponse(msg)); } case Enum_ApMsgType.Video: { var msg = PubMsgApi.BuildVideoMsg(gzhClient, gzhSever, autoKeyword.MediaId, autoKeyword.VideoTitle, autoKeyword.VideoDescription, timeStamp); return(await wxAutoComResponse.AutoMsgResponse(msg)); } case Enum_ApMsgType.BackNews: { var lstIds = autoKeyword.ArtIds.StrSplitList().Select(x => Convert.ToInt64(x)).ToList(); if (lstIds.Count > 0) { var lstWxArts = await wxAutoConvertHelper.GetResponseBackNews(autoKeyword, lstIds); var msg = PubMsgApi.BuildArticleMsg(gzhClient, gzhSever, timeStamp, lstWxArts); return(await wxAutoComResponse.AutoMsgResponse(msg)); } } break; case Enum_ApMsgType.RedBag: { return(await wxAutoConvertHelper.SendResponseRedPack(gzhClient, gzhSever, autoKeyword, dtNow, timeStamp)); } default: break; } return(wxAutoComResponse.ResponseOK()); }
/// <summary> /// 自动推送红包(公众号自动回复) /// </summary> /// <param name="gzhClient"></param> /// <param name="gzhSever"></param> /// <param name="autoKeyword"></param> /// <param name="dtNow"></param> /// <param name="timeStamp"></param> /// <returns></returns> public async Task <string> SendResponseRedPack(string gzhClient, string gzhSever, WxAutoKeywordShow autoKeyword, DateTime dtNow, int timeStamp) { // 红包只能8点之后领取 if (dtNow.Hour < 8) { return(await wxAutoComResponse.SendWxText(gzhSever, gzhClient, timeStamp, "亲:微信红包将于8点到24点之间才能领取")); } // 红包参数配置 金额|个数|祝福语|红包名称|红包备注 if (autoKeyword.RedAmount <= 0 || autoKeyword.RedCount <= 0) { logHelper.Error("SendMsgByPush:红包配置失败!autoID:" + autoKeyword.Id); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "获取红包失败,请稍候重试!")); } // 红包已发放个数 var sendCount = repo.SqlQuerySingle <int>($" SELECT COUNT(1) FROM WxAutoKeywordDetail WHERE AutoId = {autoKeyword.Id} "); if (sendCount >= autoKeyword.RedCount) { logHelper.Error("SendMsgByPush:红包已经抢完,autoID:" + autoKeyword.Id); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "客官,您来晚了,红包已被先到的小伙伴抢完鸟,下次记得早点来哦。")); } // 判断用户是否已经发放过红包 var exist = repo.FirstOrDefault <WxAutoKeywordDetail>(x => x.Id == autoKeyword.Id && x.Opend == gzhClient); if (exist != null) { logHelper.Error("SendMsgByPush:重复领取红包,autoID:" + autoKeyword.Id + " OpenID:" + gzhClient); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "你已经领取过此红包,请把机会留给更多的人!")); } // 发放红包 var payRes = RedpackApi.SendPack(gzhClient, autoKeyword.RedAmount, autoKeyword.RedAct, autoKeyword.RedWish, autoKeyword.RedRemark, ""); if (payRes.IsSucc) { //红包发送成功 WxAutoKeywordDetail redHis = await SaveAutoKeywordHis(autoKeyword.Id, gzhClient); logHelper.Debug("SendMsgByPush:发送红包成功:" + redHis.JsonSerialize()); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "红包发放成功,请注意查收~~")); } else { logHelper.Error("SendMsgByPush:红包发送失败,错误码:" + payRes.err_code + " 错误描述" + payRes.err_code_des); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "微信服务器暂忙,请稍候重试。")); } }
/// <summary> /// 自动应答(客服消息) /// </summary> /// <param name="item"></param> /// <param name="openId"></param> /// <returns></returns> public async Task <Result <string> > AutoRespond(WxAutoKeywordShow item, string openId) { try { // 自动推送类型 Enum_ApMsgType type = ComHelper.GetEnumValueByStr <Enum_ApMsgType>(item.ContentType); // 发送客服消息预览 PubApiResult apiRes = new PubApiResult(); switch (type) { case Enum_ApMsgType.Text: item.TextContent = item.TextContent.Replace("{nickname}", string.Empty); apiRes = PubKfApi.SendTextMsg(openId, item.TextContent); break; case Enum_ApMsgType.Image: apiRes = PubKfApi.SendImageMsg(openId, item.MediaId); break; case Enum_ApMsgType.Voice: apiRes = PubKfApi.SendVoiceMsg(openId, item.MediaId); break; case Enum_ApMsgType.News: apiRes = PubKfApi.SendMpNewsMsg(openId, item.MediaId); break; case Enum_ApMsgType.Video: apiRes = PubKfApi.SendVideoMsg(openId, item.MediaId, item.VideoThumbMediaId, item.VideoTitle, item.VideoDescription); break; case Enum_ApMsgType.BackNews: var lstArts = await wxAutoConvertHelper.GetAutoKeywordBackNews(item); apiRes = PubKfApi.SendNewsMsg(openId, lstArts); break; case Enum_ApMsgType.RedBag: return(await wxAutoConvertHelper.SendAutoKeywordRedPack(item, openId)); default: break; } logHelper.Debug("PrevKeyword:预览返回结果:" + ComHelper.JsonSerialize(apiRes)); if (!apiRes.IsSuss) { var msg = "预览出错:" + apiRes.errcode + " " + apiRes.errmsg; logHelper.Error("PrevKeyword:" + msg); return(new Result <string> { Message = msg }); } return(new Result <string> { IsSucc = true, Message = "发送预览消息成功!" }); } catch (Exception ex) { logHelper.Error("PrevKeyword:发送预览消息失败:" + ex.Message + " " + ex.StackTrace); } return(new Result <string> { Message = "发送预览消息失败!" }); }