Example: Use C#.NET to teach you how to develop WeChat official account (16) -- Click menu 2 for event message processing

When a WeChat user clicks on the menu, two events will be forwarded to your server through the WeChat server, one is the event push when the message is pulled; the other is the event push when the link is jumped.

The first one was discussed in the previous article, and the second one will be discussed in this article.

1. Message format

Push XML data package example:

<xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[FromUser]]></FromUserName>
  <CreateTime>123456789</CreateTime>
  <MsgType><![CDATA[event]]></MsgType>
  <Event><![CDATA[VIEW]]></Event>
  <EventKey><![CDATA[www.qq.com]]></EventKey>
</xml>

2. Operation demonstration

 

3. Code implementation

After receiving the event sent by the WeChat server, the designated interface webpage AccessWx.aspx will first judge the type of the event, and then hand it over to the designated event processing class for processing and response. Introduce the namespace using QinMing.Weixin.EventHandlerMenuView at the beginning of AccessWx.aspx.cs introduced in the first part of this series of articles;

And improve the following section of processing View, and add the click menu jump url event processing link given in this article.  

				else if(Event == "VIEW")
				{
					//对用户点击菜单触发的VIEW事件处理,使用QinMing.Weixin.EventHandlerMenuView命名空间下的MenuViewEventDeal类
					MenuViewEventDeal med = new MenuViewEventDeal();
					Response.Write(med.DealResult(weixinXML));	
				}

Create a new class MenuViewEventDeal under the namespace QinMing.Weixin.EventHandlerMenuView to handle menu click events sent from the WeChat server. Remember that the class source code files should be placed in the App_Code directory! The following is the source code of the click menu event processing:
QinMingWeixinEventHandlerMenuView.cs file content is as follows:
 

using System;
using System.Web;
using System.Xml;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using QinMing.Config;
using QinMing.Tools;
using QinMing.Weixin.ReturnContent;

namespace QinMing.Weixin.EventHandlerMenuView
{
	//事件消息处理:点击菜单view
    public class MenuViewEventDeal :System.Web.UI.Page
    {

		public string DealResult(string weixinXML)
        {
            string content = DealMenuView(weixinXML);  
            return content;
        }

        public string DealMenuView(string weixinXML)
        {
            string strresponse = "";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(weixinXML);
            XmlNodeList list = doc.GetElementsByTagName("xml");
            XmlNode xn = list[0];
            string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;   //关注用户的加密后openid
            string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;       //公众微信号原始ID
            string MsgType=xn.SelectSingleNode("//MsgType").InnerText;
            string Event=xn.SelectSingleNode("//Event").InnerText;
			string EventKey=xn.SelectSingleNode("//EventKey").InnerText;
			
			//保存点击菜单view事件
			SaveEvent(FromUserName, ToUserName, EventKey);
			
            //给管理员发送粉丝点击菜单通知
			QinMing.WeixinTemplateMessage.SendTemplateMessage.SendRemindMsg("管理员openid", "粉丝点击菜单信息提醒" + FromUserName, "http://www.yourweb.com/DisplayOneUser.aspx?open_id=" + FromUserName);  
			
			return strresponse;
        }
		
		//保存事件信息
        public void SaveEvent(string FromUserName, string ToUserName,string EventKey)
        {
            SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "insert into weixin_recv_event (msg_type,event_type,open_id,gh_id,recv_time,event_key) "
			    + "values ('event','VIEW','" + FromUserName + "','" + ToUserName + "',getdate(),'" + EventKey + "') ";
            //QinMingTools.WriteLog("sql语句:", cmd.CommandText);
			cmd.ExecuteScalar();
      
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }

    }
}

 

Guess you like

Origin blog.csdn.net/daobaqin/article/details/124831417