public AbsCharLogic Create() { string msg_context = string.Empty; Regex regex = new Regex(@"(@\w+):<br/>(菜单)"); if (regex.IsMatch(msg.Msg)) { return(new MenuLogic(contact, msg)); //菜单 } var nomal_pat = "(@\\w+):<br/>(\\w+)"; Regex n_r = new Regex(nomal_pat); int flag = 1; //1.普通消息 2,at机器人 var userName = string.Empty; if (n_r.IsMatch(msg.Msg)) { flag = 1; msg_context = n_r.Match(msg.Msg).Groups[2].ToString(); userName = n_r.Match(msg.Msg).Groups[1].ToString(); } var at_pat = "(@\\w+):<br/>@" + contact.GetNickName(msg.To) + "\\s(.*)"; Regex r = new Regex(at_pat); if (r.IsMatch(msg.Msg)) { flag = 2; msg_context = r.Match(msg.Msg).Groups[2].ToString(); //内容 userName = r.Match(msg.Msg).Groups[1].ToString(); } //如果内容必须使用自定义逻辑 if (UseCustomLogic(msg_context)) { return(new CustomLogic(contact, msg, msg_context, userName)); //自定义逻辑 } else { if (flag == 1) { return(new CustomLogic(contact, msg, msg_context, userName)); //自定义逻辑 } if (flag == 2) { return(new AutoLogic(contact, msg)); } } return(null); }
/// <summary> /// 接收消息 /// </summary> /// <param name="msg"></param> void wt_OnRevice(WXMsg msg) { try { WxContact c = new WxContact(msg.Uin); string log = "type:" + msg.Type.ToString() + "来源:" + msg.From + "[ " + c.GetNickName(msg.From) + "],发至:" + msg.To + " [" + c.GetNickName(msg.To) + "]" + msg.Msg; Tools.WriteLog(log); var m = ReplyFactory.Create(msg).MakeContent(msg); var sendContext = m.context; if (!string.IsNullOrEmpty(sendContext)) { WXService s = new WXService(); s.Sid = msg.Sid; s.Uin = msg.Uin; s.SendMsg(sendContext, msg.To, msg.From, m.type, msg.Uin, msg.Sid); } Wx_AlertForm.ShowWay showWay = Wx_AlertForm.ShowWay.UpDown; string afTitle = "[ " + c.GetNickName(msg.From) + "]回复[" + c.GetNickName(msg.To) + "]" + "一条消息"; string afContent = msg.Msg; int afShowInTime, afShowTime, afShowOutTime; int afWidth, afHeigth; int.TryParse("100", out afShowInTime); int.TryParse("8000", out afShowTime); int.TryParse("800", out afShowOutTime); int.TryParse("250", out afWidth); int.TryParse("120", out afHeigth); af = new Wx_AlertForm(); af.Show(afContent, afTitle, showWay, afWidth, afHeigth, afShowInTime, afShowTime, afShowOutTime); Tools.WriteLog("【demo】" + log); } catch (Exception ex) { Tools.WriteLog("【错误】" + ex.ToString()); Tools.WriteLog(ex.ToString()); } }