/// <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)); }
/// <summary> /// 联系客服(菜单) /// </summary> /// <param name="recMsg"></param> /// <returns></returns> public async Task <string> ContactKf(PubReceiveMsg recMsg) { try { var timeStamp = ComHelper.ConvertDateTimeInt(DateTime.Now); if (!IsWorkTime()) { var msg = "您好!现在非客服上班时间,请您在每天9-22点联系我们的客服美眉!"; return(await wxAutoComResponse.SendWxText(recMsg.FromUserName, recMsg.ToUserName, timeStamp, msg)); } else { PubKfApi.SendTextMsg(recMsg.FromUserName, "正在为你转接在线客服,请稍后....."); var msg = PubMsgApi.BuildKfTransferMsg(recMsg.FromUserName, recMsg.ToUserName, timeStamp); logHelper.Debug("ContactKf:msg:" + msg.JsonSerialize()); // 发送客服消息提醒 return(await wxAutoComResponse.AutoMsgResponse(msg)); } } catch (Exception ex) { logHelper.Error("ContactKf:联系客服失败:" + ex.Message + " " + ex.StackTrace); } return(wxAutoComResponse.ResponseOK());; }
/// <summary> /// 关注 /// </summary> /// <param name="recMsg"></param> /// <returns></returns> private async Task <string> DealWxSubscribe(PubReceiveMsgCData recMsg) { // 关注记录 await wxDbLogHelper.SaveSubLog(recMsg, 0); // 发送关注消息 var timeStamp = ComHelper.ConvertDateTimeInt(DateTime.Now); var wxUser = await repo.FirstOrDefaultAsync <WxUserInfo>(x => x.OpenId == recMsg.FromUserName); var subConf = await repo.FirstOrDefaultAsync <DbConfig>(x => x.ConfigName == "subscribeText"); if (wxUser != null && subConf != null) { var msg = subConf.ConfigValue.Replace("{nickname}", wxUser.NickName); return(await wxAutoComResponse.SendWxText(recMsg.FromUserName, recMsg.ToUserName, timeStamp, msg)); } 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, "微信服务器暂忙,请稍候重试。")); } }