接收菜单事件
企业微信接收菜单事件的代码和公众号相似,只是需要在处理之前先解密事件参数。
需要注意的是,所有向服务器传送图片的菜单命令,会发送两次消息,第一次是菜单命令事件,第二次是普通的image消息,第二次才会真正将图片的MediaID和URL传递给Foxtable,所以通常并不需要处理图片发送按钮的菜单事件,而是处理常规的image消息。
下面是企业微信接收处理菜单事件的参考代码,这个代码将接收到的菜单事件记录在一个日志文件中:
If
e.Path =
"wefox" Then
    Dim wbiz
As New
WXBizCrypt("wxa31aba4cd83af57e","foxtable","ilsmyivvRPNj0qxSiSzWCnqm7cy1w1RcS6w2LBhsh7J")
    If e.Request.HttpMethod 
= "GET"
        Dim 
ret As 
Integer = wbiz.CheckQYSignature(e)
        If ret 
<> 0 Then
'如果接入验证失败
            Dim err
As String =
wbiz.GetErorDesc(ret)
'获取错误描述
        End If
    ElseIf e.Request.HttpMethod 
= "Post"           
'
        Dim 
st As New
Date(1970,1,1,8,0,0)
        Dim msg 
As String =
wbiz.DecryptMsg(e)
'解密
        If  
IsNumeric(msg)
Then 
'如果解密
            Dim err
As String =
wbiz.GetErorDesc(CInt(msg))
'获取错误描述
            Return
        End If
        Dim 
xo As 
Foxtable.XObject 
= Foxtable.XObject.Parse(msg)
        Select Case
xo("MsgType")
            Case "text","image","voice","video","shortvideo"
'处理消息
                Dim 
dr As 
DataRow = DataTables("Message").AddNew()
                dr("AgentID") 
= xo("AgentID")
                dr("UserID") 
= xo("FromUserName")
                dr("CreateTime") 
= st.AddSeconds(xo("CreateTime"))
                dr("MsgType") 
= xo("MsgType")
                dr("MsgId") 
= xo("MsgId")
                dr("MediaId") 
= xo("MediaId")
                dr("ThumbMediaId") 
= xo("ThumbMediaId")
                dr("PicUrl") 
= xo("PicUrl")
                dr("Content") 
= xo("Content")
                
dr("Format") 
= xo("Format")
            Case 
"event" '处理事件
                Dim
CorpID As
String = xo("ToUserName")
'企业微信的CorpID
                Dim
UserID As
String  = xo("FromUserName")
'用户的UserID
                Dim
CreateTime As
Date = st.AddSeconds(xo("CreateTime"))
'触发事件时间
                Dim
AgentID As
String = xo("AgentID")
'应用ID
                Dim
key As
String = xo("EventKey")
'如果是菜单事件,用于获取按钮的Key或URL,其它事件返回空
                Dim
logFile As
String  = 
"c:\data\wxlog" &
Format(Date.Today,"yyyyMMdd")
& ".txt"
'你可以选择每日或每月一个日志文件
                Select
Case xo("Event")
                    Case
"subscribe" 
'关注事件
                    Case
"unsubscribe" 
'取消关注事件
                    Case
"LOCATION" 
'上报地理位置事件
                    Case
"enter_agent" 
'进入应用事件
                    Case
"click" '普通单击事件
                        Filesys.WriteAllText(logFile 
, UserID &
"于" &
CreateTime &
"单击按钮:" &
Key &
vbcrlf, True)
                    Case
"view" 
'单击跳转到网页事件
                        Filesys.WriteAllText(logFile 
, UserID &
"于" &
CreateTime &
"访问网页:" &
Key &
vbcrlf, True)
                    Case
"scancode_push","scancode_waitmsg"
'两种扫描二维码事件
                        Dim
ScanResult As
String = xo("ScanCodeInfo")("ScanResult")
'获取二维码扫描结果
                        Filesys.WriteAllText(logFile 
, UserID &
"于" &
CreateTime &
"扫描二维码:" &
ScanResult &
vbcrlf, True)
                    Case
"pic_sysphoto","pic_photo_or_album","pic_weixin"
'三种发送图片事件
                        Dim
Count As
Integer = xo("SendPicsInfo")("Count")
'获取图片数量
                        Filesys.WriteAllText(logFile 
, UserID &
"于" &
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 
, UserID &
"于"
& CreateTime
& 
"上报位置:"
& lx
& "|"
& ly
& "|"
& lb
& "|"
& pn 
& vbcrlf,
True)
                End 
Select
        End 
Select
    End 
If
End
If