/// <summary> /// 将微信接收到的消息处理成类 /// </summary> /// <param name="xml"></param> public static Models.xml GetMessage(string xml) { Models.xml message = new Models.xml(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNode xnMsgType = doc.SelectSingleNode($"/xml/MsgType"); if (xnMsgType.InnerText != "text") { return(null); } Type type = typeof(Models.xml); var propertries = type.GetProperties(); foreach (var pi in propertries)//遍历当前类的所有公共属性 { XmlNode xnChiefComplaint = doc.SelectSingleNode($"/xml/{pi.Name}"); if (xnChiefComplaint != null) { pi.SetValue(message, xnChiefComplaint.InnerText); } } return(message); }
/// <summary> /// 回复文本信息 /// </summary> /// <param name="message"></param> /// <returns></returns> public static string GetTextMesage(Models.xml message) { return($@"<xml> <ToUserName><![CDATA[{message.FromUserName}]]></ToUserName> <FromUserName><![CDATA[{message.ToUserName}]]></FromUserName> <CreateTime>{message.CreateTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{message.Content}]]></Content> </xml>"); }
/// <summary> /// 回复图文信息 /// </summary> /// <param name="message"></param> /// <returns></returns> public static string GetNewsMesage(Models.xml message, Models.AutoReply reply) { return($@"<xml> <ToUserName><![CDATA[{message.FromUserName}]]></ToUserName> <FromUserName><![CDATA[{message.ToUserName}]]></FromUserName> <CreateTime>{message.CreateTime}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[{reply.Title}]]></Title> <Description><![CDATA[{reply.Content}]]></Description> <PicUrl><![CDATA[{reply.ImgUrl}]]></PicUrl> <Url><![CDATA[{reply.Link}]]></Url> </item> </Articles> </xml>"); }
public async Task <string> Post() { var reader = new StreamReader(Request.Body); string xml = await reader.ReadToEndAsync(); log.Error($"用户发送的信息{xml}"); Models.xml message = Common.CommonFunction.GetMessage(xml); if (message == null) { log.Error($"不是文本信息,不解析"); return(""); } var builder = new ConfigurationBuilder().AddJsonFile("JsonFile/AutoMessage.json", optional: false, reloadOnChange: true); var config = builder.Build(); var replys = config.GetSection("AutoReply").Get <List <Models.AutoReply> >(); if (replys != null && replys.Count > 0) { foreach (var reply in replys) { if (reply.Type == Common.WeiXinEnum.MessageType.cp) { if (message.Content.StartsWith(reply.Key)) { cpLog.Error($"查询CP信息:{message.Content}"); string telphone = message.Content.Substring(reply.Key.Length).Trim(); var cp = _context.Map.Where(t => t.ManTelphone == telphone).FirstOrDefault(); string cp_telphone = ""; if (cp == null) { cp = _context.Map.Where(t => t.WomenTelphone == telphone).FirstOrDefault(); if (cp == null) { cpLog.Error($"查询CP信息,电话:{telphone};没有配对结果"); message.Content = "找不到您的匹配信息哦,小编正在努力帮您寻找心仪的对象..."; return(Common.CommonFunction.GetTextMesage(message)); } cp_telphone = cp.ManTelphone; } else { cp_telphone = cp.WomenTelphone; } var cp_user = _context.UserInfo.Where(t => t.Telphone == cp_telphone).FirstOrDefault(); if (cp_user == null) { message.Content = "CP丢失了,小编正在努力匹配中..."; cpLog.Error($"CP信息丢失;CP1电话:{telphone};匹配的结果:{cp_telphone}"); return(Common.CommonFunction.GetTextMesage(message)); } else { cpLog.Error($"CP匹配成功;CP1电话:{telphone};匹配的结果:{cp_telphone}"); reply.Content = Common.CommonFunction.GetCp(cp_user, cp.ID); return(Common.CommonFunction.GetNewsMesage(message, reply)); } } } if (message.Content == reply.Key) { switch (reply.Type) { case Common.WeiXinEnum.MessageType.text: message.Content = reply.Content; return(Common.CommonFunction.GetTextMesage(message)); case Common.WeiXinEnum.MessageType.news: reply.Content = reply.Content; return(Common.CommonFunction.GetNewsMesage(message, reply)); default: message.Content = reply.Content; return(Common.CommonFunction.GetTextMesage(message)); } } } } message.Content = _configuration["DefalutReply"]; return(Common.CommonFunction.GetTextMesage(message)); }