用户自定义菜单制作时,需要用到access_token,我们直接使用前面讲解的IsExistAccess_Token()函数。我理解的微信公共平台里面菜单分为button和sub_button,即菜单和子菜单,这些菜单都有一个name的属性,类别分为click和view,click类有key属性;而view类有url属性,含有子菜单的菜单没有key属性也没有url属性。这些情况可以从下面的例子看出来。
public void MyMenu()在你页面的 Page_Load 函数中调用这个MyMenu(),就可以显示出来了。
{
string weixin1 = "";
weixin1 = @" {
""button"":[
{
""type"":""click"",
""name"":""你好!"",
""key"":""Hello""
},
{
""type"":""view"",
""name"":""公司简介"",
""url"":""http://www.4ugood.net""
},
{
""name"":""产品介绍"",
""sub_button"":[
{
""type"":""click"",
""name"":""产品1"",
""key"":""P1""
},
{
""type"":""click"",
""name"":""产品2"",
""key"":""P2""
}]
}]
}
";
string access_token = IsExistAccess_Token();
string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token, weixin1);
Response.Write(i);
}
既然显示出来了,菜单的时间如何出发呢?我们已经了解到了如果类型为view的话,他有url属性,这个不需要处理,点击后会直接跳转到你设定的url的页面,下面我来看看如何触发click吧,按照微信的文档可以用(!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")来判断,我把之前的代码改造一下,同时把在GetWxMessage()方法中把EventKey的值附上,wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
protected void Page_Load(object sender, EventArgs e)这样就可以相应你的菜单事件了,我上面的代码写的有很多可以优化的地方,这里主要以简介为主,以后我们会逐渐搭建起一个微信公共平台的.net框架,什么菜单类,消息类等等。
{
MyMenu();
wxmessage wx = GetWxMessage();
string res = "";
if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
{
string content = "";
content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";
res = sendTextMessage(wx, content);
}
else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")
{
if(wx.EventKey=="Hello")
res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
if(wx.EventKey=="P1")
res = sendTextMessage(wx, "你好,点击了产品1");
if(wx.EventKey=="P2")
res = sendTextMessage(wx, "你好,点击了产品2");
}
else
{
if (wx.MsgType == "text" && wx.Content == "你好")
{
res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
}
else if (wx.MsgType == "voice")
{
res = sendTextMessage(wx, wx.Recognition);
}
else
{
res = sendTextMessage(wx, "你好,未能识别消息!");
}
}
Response.Write(res);
}
private wxmessage GetWxMessage()
{
wxmessage wx = new wxmessage();
StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
if (wx.MsgType.Trim() == "text")
{
wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
}
if (wx.MsgType.Trim() == "event")
{
wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
}
if (wx.MsgType.Trim() == "voice")
{
wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;
}
return wx;
}
/// <summary>
/// 发送文字消息
/// </summary>
/// <param name="wx">获取的收发者信息</param>
/// <param name="content">内容</param>
/// <returns></returns>
private string sendTextMessage(wxmessage wx, string content)
{
string res = string.Format(@"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{3}]]></Content>
</xml> ",
wx.FromUserName, wx.ToUserName, DateTime.Now, content);
return res;
}