/// <summary> /// 自动回复规则添加 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool AddMsgAutoRule(MsgAutoRule model) { string sql = @"INSERT INTO [WX_MsgAutoRule] ([ID],[WXConfigID],[MatchType],[MatchPattern],[MsgType],[MsgValue],[Handle],[AddTime],[AddUser],[LastModTime],[LastModUser],[Order],[Enabled]) VALUES (@ID,@WXConfigID,@MatchType,@MatchPattern,@MsgType,@MsgValue,@Handle,@AddTime,@AddUser,@LastModTime,@LastModUser,@Order,@Enabled)"; System.Data.SqlClient.SqlParameter[] paras = new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@ID", model.ID), new System.Data.SqlClient.SqlParameter("@WXConfigID", model.WXConfigID), new System.Data.SqlClient.SqlParameter("@MatchType", model.MatchType), new System.Data.SqlClient.SqlParameter("@MatchPattern", model.MatchPattern), new System.Data.SqlClient.SqlParameter("@MsgType", model.MsgType), new System.Data.SqlClient.SqlParameter("@MsgValue", model.MsgValue), new System.Data.SqlClient.SqlParameter("@Handle", model.Handle), new System.Data.SqlClient.SqlParameter("@AddTime", DateTime.Now), new System.Data.SqlClient.SqlParameter("@AddUser", model.AddUser), new System.Data.SqlClient.SqlParameter("@LastModTime", DateTime.Now), new System.Data.SqlClient.SqlParameter("@LastModUser", model.LastModUser), new System.Data.SqlClient.SqlParameter("@Order", model.Order), new System.Data.SqlClient.SqlParameter("@Enabled", (model.Enabled == 1?1:0)) }; int rowsAffected = DbHelperSQL.ExecuteSql(sql.ToString(), paras); if (rowsAffected > 0) { return(true); } else { return(false); } }
/// <summary> /// 文本消息处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public string TextMsgReceive(TextRequestMsgModel msg) { string res = string.Empty; MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); MsgAutoRule rule = dal.GetKeywordsRule(wxConfig.ID, msg.Content); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } else { rule = dal.GetRegexRule(wxConfig.ID, msg.Content); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } else { rule = dal.GetDefaultRule(wxConfig.ID); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } } }//启用默认消息回复 return(res); }
protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } if (keyword.Text.Trim() != null && keyword.Text.Trim() != "") { MsgAutoRule model = new MsgAutoRule(); MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); model.MatchPattern = keyword.Text; model.Order = Convert.ToInt32(sort.Text); model.MsgValue = repmsgcontent.Value; model.ID = strID; model.Enabled = 1; model.LastModTime = DateTime.Now; //model.MatchType = "keywords"; //model.MsgType = "text"; //model.Handle = "Mozart.WeiXin.SubscribeCouponActHandle"; if (dal.UpdateMsgAutoRule(model)) { MessageBox.Show(this, "操作成功!"); } else { MessageBox.Show(this, "操作失败!"); } } else { MessageBox.Show(this, "请输入信息名称后再操作!"); } }
public MsgAutoRule GetRegexRule(string wxConfigID, string content) { MsgAutoRule res = null; if (!string.IsNullOrEmpty(wxConfigID) && !string.IsNullOrEmpty(content)) { string sql = @"SELECT * FROM [WX_MsgAutoRule] WHERE Enabled=1 AND MatchType='regex' AND WXConfigID=@WXConfigID ORDER BY [Order] ASC"; IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>() { new System.Data.SqlClient.SqlParameter("@WXConfigID", wxConfigID) }; DataSet ds = DbHelperSQL.Query(sql, paras.ToArray()); List <MsgAutoRule> list = ds.ConvertToList <MsgAutoRule>(); foreach (MsgAutoRule li in list) { if (Regex.IsMatch(content, li.MatchPattern)) { res = li; break; } } } return(res); }
/// <summary> /// 获取默认回复规则 /// </summary> /// <param name="wxConfigID"></param> /// <returns></returns> public MsgAutoRule GetDefaultRule(string wxConfigID) { MsgAutoRule res = null; if (!string.IsNullOrEmpty(wxConfigID)) { string sql = @"SELECT * FROM [WX_MsgAutoRule] WHERE Enabled=1 AND MatchType='default' AND WXConfigID=@WXConfigID ORDER BY [Order] ASC"; IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>() { new System.Data.SqlClient.SqlParameter("@WXConfigID", wxConfigID) }; DataSet ds = DbHelperSQL.Query(sql, paras.ToArray()); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; res = new MsgAutoRule() { ID = dr.GetColumnValue("ID", string.Empty), WXConfigID = dr.GetColumnValue("WXConfigID", string.Empty), MatchType = dr.GetColumnValue("MatchType", string.Empty), MatchPattern = dr.GetColumnValue("MatchPattern", string.Empty), MsgType = dr.GetColumnValue("MsgType", string.Empty), MsgValue = dr.GetColumnValue("MsgValue", string.Empty), Handle = dr.GetColumnValue("Handle", string.Empty), AddTime = dr.GetColumnValue("AddTime", DateTime.Now), AddUser = dr.GetColumnValue("AddUser", string.Empty), LastModTime = dr.GetColumnValue("LastModTime", DateTime.Now), LastModUser = dr.GetColumnValue("LastModUser", string.Empty) }; } } return(res); }
public void ShowActivityInfo(string strID) { MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); DataSet ds = dal.GetMsuAutoRuleDetail(strID); MsgAutoRule model = DataConvert.DataRowToModel <MsgAutoRule>(ds.Tables[0].Rows[0]); keyword.Text = model.MatchPattern; repmsgcontent.Value = model.MsgValue; sort.Text = model.Order.ToString(); if (strAction == "show") { this.btnReset.Visible = false; this.btnSave.Visible = false; keyword.ReadOnly = true; sort.ReadOnly = true; } }
public MsgAutoRule GetImageRule(string wxConfigID) { MsgAutoRule res = null; if (!string.IsNullOrEmpty(wxConfigID)) { string sql = @"SELECT * FROM [WX_MsgAutoRule] WHERE Enabled=1 AND MatchType='image' AND WXConfigID=@WXConfigID ORDER BY [Order] ASC"; IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>() { new System.Data.SqlClient.SqlParameter("@WXConfigID", wxConfigID) }; DataSet ds = DbHelperSQL.Query(sql, paras.ToArray()); res = ds.ConvertToFirstObj <MsgAutoRule>(); } return(res); }
/// <summary> /// 事件单击事件消息处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public string MenuClickProcess(ClickEventRequestMsgModel msg) { string res = string.Empty; MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); MsgAutoRule rule = dal.GetClickEventRule(wxConfig.ID, msg.EventKey); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } else { rule = dal.GetDefaultRule(wxConfig.ID); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } }//启用默认消息回复 return(res); }
protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } if (keyword.Text.Trim() != null && keyword.Text.Trim() != "") { WXConfigDAL configdal = new WXConfigDAL(); DataSet wxconfigds = configdal.GetWXConfigDataList(Session["strSiteCode"].ToString()); MsgAutoRule model = new MsgAutoRule(); MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); model.MatchPattern = keyword.Text; model.Order = Convert.ToInt32(sort.Text); model.MsgValue = repmsgcontent.Value; model.ID = Guid.NewGuid().ToString("N").ToUpper(); model.Enabled = 1; model.LastModTime = DateTime.Now; model.MatchType = "keywords"; model.MsgType = "text"; if (wxconfigds != null && wxconfigds.Tables.Count > 0 && wxconfigds.Tables[0].Rows.Count > 0) { model.WXConfigID = wxconfigds.Tables[0].Rows[0]["ID"].ToString(); } model.Handle = "Mozart.WeiXin.SubscribeCouponActHandle"; if (dal.AddMsgAutoRule(model)) { MessageBox.Show(this, "操作成功!"); } else { MessageBox.Show(this, "操作失败!"); } } else { MessageBox.Show(this, "请输入信息名称后再操作!"); } }
/// <summary> /// 获取关键字回复规则 /// </summary> /// <param name="wxConfigID"></param> /// <param name="keywords"></param> /// <returns></returns> public MsgAutoRule GetKeywordsRule(string wxConfigID, string keywords) { MsgAutoRule res = null; if (!string.IsNullOrEmpty(wxConfigID) && !string.IsNullOrEmpty(keywords)) { string sql = @"SELECT * FROM [WX_MsgAutoRule] WHERE Enabled=1 AND MatchType='keywords' AND WXConfigID=@WXConfigID AND MatchPattern=@MatchPattern ORDER BY [Order] ASC"; IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>() { new System.Data.SqlClient.SqlParameter("@WXConfigID", wxConfigID), new System.Data.SqlClient.SqlParameter("@MatchPattern", keywords) }; DataSet ds = DbHelperSQL.Query(sql, paras.ToArray()); res = ds.ConvertToFirstObj <MsgAutoRule>(); //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) //{ // DataRow dr = ds.Tables[0].Rows[0]; // res = new MsgAutoRule() // { // ID = dr.GetColumnValue("ID", string.Empty), // WXConfigID = dr.GetColumnValue("WXConfigID", string.Empty), // MatchType = dr.GetColumnValue("MatchType", string.Empty), // MatchPattern = dr.GetColumnValue("MatchPattern", string.Empty), // MsgType = dr.GetColumnValue("MsgType", string.Empty), // MsgValue = dr.GetColumnValue("MsgValue", string.Empty), // Handle = dr.GetColumnValue("Handle", string.Empty), // AddTime = dr.GetColumnValue("AddTime", DateTime.Now), // AddUser = dr.GetColumnValue("AddUser", string.Empty), // LastModTime = dr.GetColumnValue("LastModTime", DateTime.Now), // LastModUser = dr.GetColumnValue("LastModUser", string.Empty) // }; //} } return(res); }
/// <summary> /// 图片消息处理 /// </summary> /// <param name="msg"></param> /// <returns></returns> public string ImageMsgReceive(ImageRequestMsgModel msg) { string res = string.Empty; MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); MsgAutoRule rule = dal.GetImageRule(wxConfig.ID); if (rule != null) { switch (rule.MsgType.ToLower()) { case "hp_photo": try { string fileDir = HttpContext.Current.Server.MapPath("/HP_PHOTO/"); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } ExceptionLog log = new ExceptionLog(); log.Message = weixin.WeiXinConfig.AppId + "}{" + weixin.WeiXinConfig.AppSecret + "}{" + msg.MediaId; ExceptionLogDAL.InsertExceptionLog(log); //string fileName = weixin.SaveAsMedia(msg.MediaId, fileDir); string fileName = weixin.SaveAsFile(msg.PicUrl, fileDir); if (!string.IsNullOrEmpty(fileName)) { Photo p = new Photo() { SiteCode = siteCode, OpenId = msg.FromUserName, Img = fileName }; PhotoDAL photoDal = new PhotoDAL(); photoDal.SaveInfo(p); //回复文本消息 string url = string.Format("{0}/WebService/ImageEdit.aspx?id={1}", GetSiteUrl(), p.ID); string content = string.Format("<a href='{0}'>点击编辑</a>", url); TextResponseMsgModel textMsg = new TextResponseMsgModel() { ToUserName = msg.FromUserName, FromUserName = msg.ToUserName, CreateTime = WeiXinHelper.ConvertDateTimeInt(DateTime.Now).ToString(), Content = content }; res = textMsg.ToString(); } } catch (Exception ex) { ExceptionLogDAL.InsertExceptionLog(ex); } break; case "user_photo": try { string fileDir = HttpContext.Current.Server.MapPath("/USER_PHOTO/"); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } //string fileName = weixin.SaveAsMedia(msg.MediaId, fileDir); string fileName = weixin.SaveAsFile(msg.PicUrl, fileDir); if (!string.IsNullOrEmpty(fileName)) { UserPhoto photo = new UserPhoto() { Name = fileName, SiteCode = siteCode, OpenId = msg.FromUserName, FilePath = fileName }; UserPhotoDAL uPhotoDal = new UserPhotoDAL(); uPhotoDal.Insert(photo); //回复文本消息 TextResponseMsgModel textMsg = new TextResponseMsgModel() { ToUserName = msg.FromUserName, FromUserName = msg.ToUserName, CreateTime = WeiXinHelper.ConvertDateTimeInt(DateTime.Now).ToString(), Content = rule.MsgValue == null ? string.Empty : TransformText(rule.MsgValue, msg) }; res = textMsg.ToString(); } } catch (Exception ex) { ExceptionLogDAL.InsertExceptionLog(ex); } break; case "user_photo_url": try { string fileDir = HttpContext.Current.Server.MapPath("/USER_PHOTO/"); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } string fileName = weixin.SaveAsFile(msg.PicUrl, fileDir); if (!string.IsNullOrEmpty(fileName)) { UserPhoto photox = new UserPhoto() { Name = fileName, SiteCode = siteCode, OpenId = msg.FromUserName, FilePath = fileName }; UserPhotoDAL uPhotoDal = new UserPhotoDAL(); uPhotoDal.Insert(photox); //回复文本消息 string url = string.Format("{0}/MicroSite/PhotoPrint.aspx?id={1}", GetSiteUrl(), photox.ID); string content = string.Format("<a href='{0}'>开始打印</a>", url); TextResponseMsgModel textUrlMsg = new TextResponseMsgModel() { ToUserName = msg.FromUserName, FromUserName = msg.ToUserName, CreateTime = WeiXinHelper.ConvertDateTimeInt(DateTime.Now).ToString(), Content = content }; res = textUrlMsg.ToString(); } } catch (Exception ex) { ExceptionLogDAL.InsertExceptionLog(ex); } break; case "user_printphoto_stepone": try { string fileDir = HttpContext.Current.Server.MapPath("/USER_PHOTO/"); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } string fileName = weixin.SaveAsFile(msg.PicUrl, fileDir); if (!string.IsNullOrEmpty(fileName)) { UserPhoto photox = new UserPhoto() { Name = fileName, SiteCode = siteCode, OpenId = msg.FromUserName, FilePath = fileName }; UserPhotoDAL uPhotoDal = new UserPhotoDAL(); uPhotoDal.Insert(photox); //回复文本消息 string url = string.Format("{0}/MicroSite/PhotoPrintStepOne.aspx?id={1}", GetSiteUrl(), photox.ID); string content = string.Format("<a href='{0}'>开始打印</a>", url); TextResponseMsgModel textUrlMsg = new TextResponseMsgModel() { ToUserName = msg.FromUserName, FromUserName = msg.ToUserName, CreateTime = WeiXinHelper.ConvertDateTimeInt(DateTime.Now).ToString(), Content = content }; res = textUrlMsg.ToString(); } } catch (Exception ex) { ExceptionLogDAL.InsertExceptionLog(ex); } break; default: rule = dal.GetDefaultRule(wxConfig.ID); if (rule != null) { res = ProcessReply(msg, rule.MsgType, rule.MsgValue); } break; } } return(res); }
/// <summary> /// 修改自动回复规则 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool UpdateMsgAutoRule(MsgAutoRule model) { string safesql = ""; safesql = " update WX_MsgAutoRule set "; if (model.WXConfigID != null && model.WXConfigID != "") { safesql += "[WXConfigID]='" + model.WXConfigID + "',"; } if (model.MatchType != null && model.MatchType != "") { safesql += "[MatchType]='" + model.MatchType + "',"; } if (model.MatchPattern != null && model.MatchPattern != "") { safesql += "[MatchPattern]='" + model.MatchPattern + "',"; } if (model.MsgType != null && model.MsgType != "") { safesql += "[MsgType]='" + model.MsgType + "',"; } if (model.MsgValue != null && model.MsgValue != "") { safesql += "[MsgValue]='" + model.MsgValue + "',"; } if (model.Handle != null && model.Handle != "") { safesql += "[Handle]='" + model.Handle + "',"; } if (model.AddUser != null && model.AddUser.ToString() != "") { safesql += "[AddUser]='" + model.AddUser + "',"; } if (model.LastModTime != null && model.LastModTime.ToString() != "") { safesql += "[LastModTime]='" + model.LastModTime + "',"; } if (model.LastModUser != null && model.LastModUser.ToString() != "") { safesql += "[LastModUser]='" + model.LastModUser + "',"; } if (model.Order > 0 && model.Order.ToString() != "") { safesql += "[Order]=" + model.Order + ","; } if (model.Enabled > 0 && model.Enabled.ToString() != "") { safesql += "[Enabled]=" + (model.Enabled == 1?1:0) + ","; } safesql += "[AddTime]='" + DateTime.Now + "' where ID='" + model.ID + "'"; int rowsAffected = DbHelperSQL.ExecuteSql(safesql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }
protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } if (!string.IsNullOrEmpty(keyword.Text.Trim()) && fudVoice.HasFile && fudVoice.PostedFile.ContentLength <= 1024 * 1024 * 2 && (fudVoice.PostedFile.FileName.ToLower().Contains(".mp3") || fudVoice.PostedFile.FileName.ToLower().Contains(".amr"))) {//语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 try { //先上传并插入微信媒体表 string fileDir = string.Format("/Uploads/{0}/", DateTime.Now.ToString("yyyyMM")); if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(fileDir))) { System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(fileDir)); } string ext = System.IO.Path.GetExtension(fudVoice.FileName); string fileName = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), ext); string filePath = fileDir + fileName; fudVoice.SaveAs(HttpContext.Current.Server.MapPath(filePath)); Media media = new Media() { MediaName = fudVoice.FileName, MediaFile = filePath, MediaType = MediaUploadType.Voice.ToString().ToLower() }; //选中自动同步时,将语音文件自动同步至微信服务器 if (cboIsSyn.Checked) { WXConfigDAL cdal = new WXConfigDAL(); Model.WeiXin.WXConfig wxConfig = cdal.GetWXConfigBySiteCode(Session["strSiteCode"].ToString()); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); MediaUploadResult obj = weixin.PostMedia(HttpContext.Current.Server.MapPath(filePath), MediaUploadType.Voice); media.MediaID = obj.MediaID; media.LastSynTime = DateTime.Now; } } MediaDAL.CreateInstance().InsertInfo(media); //插入自动回复匹配表 WXConfigDAL configdal = new WXConfigDAL(); DataSet wxconfigds = configdal.GetWXConfigDataList(Session["strSiteCode"].ToString()); MsgAutoRule model = new MsgAutoRule(); MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); model.MatchPattern = keyword.Text; model.Order = Convert.ToInt32(sort.Text); model.MsgValue = media.ID;//对应微信媒体文件ID,因微信上传文件有效期为两天,所以需要调度程序定期自动上传 model.ID = Guid.NewGuid().ToString("N").ToUpper(); model.Enabled = 1; model.LastModTime = DateTime.Now; model.MatchType = "keywords"; model.MsgType = "voice"; if (wxconfigds != null && wxconfigds.Tables.Count > 0 && wxconfigds.Tables[0].Rows.Count > 0) { model.WXConfigID = wxconfigds.Tables[0].Rows[0]["ID"].ToString(); } if (dal.AddMsgAutoRule(model)) { MessageBox.Show(this, "操作成功!"); } else { MessageBox.Show(this, "操作失败!"); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } } else { MessageBox.Show(this, "请核对信息后再操作!"); } }