接收用户消息

微信服务器收到用户发来的消息后,会以POST方式,将消息转发给Foxtable端的HTTP服务。

消息的格式是纯文本的XML,
保存在HttpRequest事件的e参数PlainText中。

企业微信的消息都是经过加密的,
收到的消息要先解密,发送的消息也要先加密,Foxtable内置了一个WXBizCrypt类,专门用于微信消息的加解密。

1、首先新建一个数据表,用于保存收到的用户消息,表名为Message,表结构如下:



2、将FoxTable端的HttpRequest时事件代码设置为:


Select
Case e.path
    Case "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"
                    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")
            End Select
       
End If
End
Select

过滤重复消息

微信服务器将收到的用户消息转发给Foxtable端的HTTP服务,如果Foxtbale不能在5秒内做出回应,微信服务器会再次转发此消息,总共会重复三次,这意味着如果用户量比较大,服务端处理不及时,可能会收到三条同样的消息,显然我们需要编码过滤掉重复的消息。

为了过滤掉重复消息,可以将HttpRequest事件代码改为:

Select Case e.path
    Case "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)
            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"
                    If DataTables("Message").Find("MsgID = '" & xo("MsgId").ToString() & "'") IsNot  Nothing Then
'
如果有相同MsgID的信息直接返回
                        Return
                    End If
                    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")
            End Select
       
End If
End
Select


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