.NET微信公众号开发之公众号消息处理(2)

class EventHandler : IHandler { /// <summary> /// 请求的xml /// </summary> private string RequestXml { get; set; } /// <summary> /// 构造函数 /// </summary> /// <param></param> public EventHandler(string requestXml) { this.RequestXml = requestXml; } /// <summary> /// 处理请求 /// </summary> /// <returns></returns> public string HandleRequest() { string response = string.Empty; EventMessage em = EventMessage.LoadFromXml(RequestXml); if (em.Event.Equals("subscribe", StringComparison.OrdinalIgnoreCase))//用来判断是不是首次关注 { PicTextMessage tm = new PicTextMessage();//我自己创建的一个图文消息处理类 tm.ToUserName = em.FromUserName; tm.FromUserName = em.ToUserName; tm.CreateTime = CommonWeiXin.GetNowTime(); response = tm.GenerateContent(); } return response; } }

TextHandler

/// <summary> /// 文本信息处理类 /// </summary> public class TextHandler : IHandler { string openid { get; set; } string access_token { get; set; } /// <summary> /// 请求的XML /// </summary> private string RequestXml { get; set; } /// <summary> /// 构造函数 /// </summary> /// <param>请求的xml</param> public TextHandler(string requestXml) { this.RequestXml = requestXml; } /// <summary> /// 处理请求 /// </summary> /// <returns></returns> public string HandleRequest() { string response = string.Empty; TextMessage tm = TextMessage.LoadFromXml(RequestXml); string content = tm.Content.Trim(); if (string.IsNullOrEmpty(content)) { response = "您什么都没输入,没法帮您啊。"; } else { string username = System.Configuration.ConfigurationManager.AppSettings["weixinid"].ToString(); AccessToken token = AccessToken.Get(username); access_token = token.access_token; openid = tm.FromUserName; response = HandleOther(content); } tm.Content = response; //进行发送者、接收者转换 string temp = tm.ToUserName; tm.ToUserName = tm.FromUserName; tm.FromUserName = temp; response = tm.GenerateContent(); return response; } /// <summary> /// 处理其他消息 /// </summary> /// <param></param> /// <returns></returns> private string HandleOther(string requestContent) { string response = string.Empty; if (requestContent.Contains("你好") || requestContent.Contains("您好")) { response = "您也好~"; }else if (requestContent.Contains("openid") || requestContent.Contains("id") || requestContent.Contains("ID"))//用来匹配用户输入的关键字 { response = "你的Openid: "+openid; } else if (requestContent.Contains("token") || requestContent.Contains("access_token")) { response = "你的access_token: " + access_token; }else { response = "试试其他关键字吧。"; } return response; } }

HandlerFactory

/// <summary> /// 处理器工厂类 /// </summary> public class HandlerFactory { /// <summary> /// 创建处理器 /// </summary> /// <param>请求的xml</param> /// <returns>IHandler对象</returns> public static IHandler CreateHandler(string requestXml) { IHandler handler = null; if (!string.IsNullOrEmpty(requestXml)) { //解析数据 XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(requestXml); XmlNode node = doc.SelectSingleNode("/xml/MsgType"); if (node != null) { XmlCDataSection section = node.FirstChild as XmlCDataSection; if (section != null) { string msgType = section.Value; switch (msgType) { case "text": handler = new TextHandler(requestXml); break; case "event": handler = new EventHandler(requestXml); break; } } } } return handler; } }

在这里基本的一些类已经完成了,现在我们来完成,关注我们的微信公众号,我们就发送一条图文消息,同时输入我们的一些关键字,返回一些消息,比如输入id返回用户的openid等等。

二.PicTextMessage

public class PicTextMessage : Message { /// <summary> /// 模板静态字段 /// </summary> private static string m_Template; /// <summary> /// 默认构造函数 /// </summary> public PicTextMessage() { this.MsgType = "news"; } /// <summary> /// 从xml数据加载文本消息 /// </summary> /// <param></param> public static PicTextMessage LoadFromXml(string xml) { PicTextMessage tm = null; if (!string.IsNullOrEmpty(xml)) { XElement element = XElement.Parse(xml); if (element != null) { tm = new PicTextMessage(); tm.FromUserName = element.Element(CommonWeiXin.FROM_USERNAME).Value; tm.ToUserName = element.Element(CommonWeiXin.TO_USERNAME).Value; tm.CreateTime = element.Element(CommonWeiXin.CREATE_TIME).Value; } } return tm; } /// <summary> /// 模板 /// </summary> public override string Template { get { if (string.IsNullOrEmpty(m_Template)) { LoadTemplate(); } return m_Template; } } /// <summary> /// 生成内容 /// </summary> /// <returns></returns> public override string GenerateContent() { this.CreateTime = CommonWeiXin.GetNowTime(); string str= string.Format(this.Template, this.ToUserName, this.FromUserName, this.CreateTime); return str; } /// <summary> /// 加载模板 /// </summary> private static void LoadTemplate() { m_Template= @"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[有位停车欢迎你!]]></Title> <Description><![CDATA[如有问题请致电400-6238-136或直接在微信留言,我们将第一时间为您服务!]]></Description> <PicUrl><![CDATA[]]></PicUrl> <Url><![CDATA[]]></Url> </item> </Articles> </xml> "; } }

最后我们的效果如下所示;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzywx.html