接收事件推送

企业微信接收事件推送和公众号基本相似,只是有个解密和加密的过程。

假定你需要自动生成一个日志文件,记录用户关注、取消关注、位置以及进入应用的信息:

laoliu于2017-02-14 07:06:30关注0
laoliu于2017-02-14 07:06:31关注9
laoliu于2017-02-14 07:06:34访问9
laoliu于2017-02-14 07:06:54访问9
zhangsan于2017-02-14 07:07:09访问9
zhangsan于2017-02-14 07:07:14访问9
zhangsan在2017-02-14 07:07:16的位置为:21.257669|110.377129


完成这样的任务很简单,可以参考下面的HttpRequest事件代码:

If e.Path = "wefox"
    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"
'
处理消息
               
'
保存消息的代码
            Case "event"
'
处理事件
                Dim UserID As String  = xo("FromUserName")
'
用户的UserID
                Dim CreateTime As Date = st.AddSeconds(xo("CreateTime"))
'
事件触发时间
                Dim AgentID As String = xo("AgentID")
'
应用ID,如果为0表示针对整个企业微信.
                Dim logFile As String  = "c:\data\wxlog" & Format(Date.Today,"yyyyMMdd") & ".txt"
'
你可以选择每日或每月一个日志文件
                Select Case xo("Event")
                    Case "subscribe"
'
关注事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "关注" & AgentID & vbcrlf, True)
                    Case "unsubscribe"
'
取消关注事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "取消关注" & AgentID & vbcrlf ,True)
                    Case "enter_agent"
'
进入应用事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "访问" & AgentID & vbcrlf , True)
                    Case "LOCATION"
'
上报地理位置事件
                        Dim y As String = xo("Longitude")
'
经度
                        Dim x As String = xo("Latitude")
'
纬度
                       
Filesys.WriteAllText(logFile , UserID & "在" & CreateTime & "的位置为:" & x & "|" & y & vbcrlf , True)
                End Select
        End Select
    End
If

End
If

在事件中被动回复消息

在事件中也可以被动回复消息,记得消息一定要先加密才能回复,录入下面的代码在用户关注后,自动回复一条欢迎语:

If e.Path = "wefox"
    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"
'
处理消息
               
'
保存消息的代码
            Case "event"
'
处理事件
                Dim UserID As String  = xo("FromUserName")
'
用户的UserID
                Dim CreateTime As Date = st.AddSeconds(xo("CreateTime"))
'
事件触发时间
                Dim AgentID As String = xo("AgentID")
'
应用ID,如果为0表示针对整个企业微信.
                Dim logFile As String  = "c:\data\wxlog" & Format(Date.Today,"yyyyMMdd") & ".txt"
'
你可以选择每日或每月一个日志文件
                Select Case xo("Event")
                    Case "subscribe"
'
关注事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "关注" & AgentID & vbcrlf, True)
                       
'
被动回复消息
                        Dim so As New Foxtable.XObject()
                        so("ToUserName") = xo("FromUserName")
                        so("FromUserName") = xo("ToUserName")
                        so("CreateTime") = (Date.Now - st).TotalSeconds()
                        so("MsgType")= "text"
                        so("Content") =
"
今天是个好日子,欢迎" & UserID & "的加入!"
                        msg = wbiz .EncryptMsg(e,so.ToXML )
'
加密要发送的消息
                        If  IsNumeric(msg) Then
'
如果加密失败
                            Dim err As String = wbiz.GetErorDesc(CInt(msg))
'
获取错误描述
                        Else
                            e.WriteString(msg)
                        End If
                    Case "unsubscribe"
'
取消关注事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "取消关注" & AgentID & vbcrlf ,True)
                    Case "enter_agent"
'
进入应用事件
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "访问" & AgentID & vbcrlf , True)
                    Case "LOCATION"
'
上报地理位置事件
                        Dim y As String = xo("Longitude")
'
经度
                        Dim x As String = xo("Latitude")
'
纬度
                        Filesys.WriteAllText(logFile , UserID &
"
" & CreateTime & "的位置为:" & x & "|" & y & vbcrlf , True)
                End Select
        End Select
    End
If

End
If


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