接收菜单事件

单击任何自定义菜单按钮,微信服务器都会向Foxtable的HTTP服务推送一个事件,即使是直接跳转到网页的按钮,也一样会推送事件。

需要注意的是,所有向服务器传送图片的菜单命令,会发送两次消息,第一次是菜单命令事件,第二次是普通的image消息,第二次才会真正将图片的MediaID和URL传递给Foxtable。

接收并处理菜单事件,可以参考以下HttpRequest事件代码,这段代码将接收到的菜单事件记录在一个日志文件中:


Select Case e.path
    Case "wefox"
        If e.Request.HttpMethod.ToUpper = "GET"
            If Functions.Execute("VerifySignature",e) Then
                e.WriteString(e.GetValues("echostr"))
            End If
        ElseIf e.Request.HttpMethod.ToUpper = "Post"
            Dim xo As Foxtable.XObject = Foxtable.XObject.Parse(e.PlainText)
            Dim st As New Date(1970,1,1,8,0,0)
            Select Case xo("MsgType")
                Case "text","image","voice","video","shortvideo"
                    '
保存收到的消息
                    Dim dr As DataRow = DataTables("Message").AddNew()
                    dr("FromUserName") = xo("FromUserName")
                    dr("CreateTime") = st.AddSeconds(xo("CreateTime"))
                    dr("MsgType") = xo("MsgType")
                    dr("MsgId") = xo("MsgId")
                    dr("MediaId") = xo("MediaId")
                    dr("PicUrl") = xo("PicUrl")
                    dr("Content") = xo("Content")
                    dr.Save()
                Case "event"
                    Dim OpenId As String = xo("FromUserName")
                    Dim key As String = xo("EventKey")
                   
Dim CreateTime As Date = st.AddSeconds(xo("CreateTime")) '触发事件时间
                    Dim logFile As String  = "c:\data\location" & Format(Date.Today,"yyyyMMdd") & ".txt" '你可以选择每日或每月一个日志文件
                    Select Case xo("Event")
                        Case "subscribe" '关注事件
                            '处理用户关注事件的代码
                        Case "unsubscribe" '取消关注事件
                            '处理用户取消关注事件的代码'
                        Case "LOCATION" '自动上报地理位置事件
                            '处理自动上报地理位置的代码
                        Case "TEMPLATESENDJOBFINISH"
                            '处理模板消息发送事件的代码
                        Case "SCAN"
                            '处理扫描情景二维码事件的代码
                        Case "CLICK" '普通单击事件
                            Filesys.WriteAllText(logFile , OpenId & "于" & CreateTime & "单击按钮:" & key & vbcrlf, True)
                        Case "VIEW" '单击跳转到网页事件
                            Filesys.WriteAllText(logFile , OpenId & "于" & CreateTime & "访问网页:" & key & vbcrlf, True)
                        Case "scancode_push","scancode_waitmsg" '两种扫描二维码事件
                            Dim ScanResult As String = xo("ScanCodeInfo")("ScanResult") '获取二维码扫描结果
                            Filesys.WriteAllText(logFile , OpenId & "于" & CreateTime & "扫描二维码:" & ScanResult & vbcrlf, True)
                        Case "pic_sysphoto","pic_photo_or_album","pic_weixin" '三种发送图片事件
                            Dim Count As Integer = xo("SendPicsInfo")("Count") '获取图片数量
                            Filesys.WriteAllText(logFile , OpenId & "于" & CreateTime & "提交图片数:" & Count & vbcrlf, True)
                        Case "location_select" '主动上报地理位置事件
                            Dim lx As String = xo("SendLocationInfo")("Location_X")
                            Dim ly As String = xo("SendLocationInfo")("Location_Y")
                           
Dim lb As String = xo("SendLocationInfo")("Label")
                            Dim pn As String = xo("SendLocationInfo")("Poiname")
                            Filesys.WriteAllText(logFile , OpenId & "
" & CreateTime & "上报位置:" & lx &"|" & ly & "|" &  lb & "|" & pn & vbcrlf, True)
                    End Select

            End Select
        End If
End
Select

 


本页地址:http://www.foxtable.com/mobilehelp/topics/0201.htm