接收普通消息

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

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

在接收用户消息之前,首先我们要了解普通用户消息的格式,参考:

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html

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



提示:列名请和和接口消息中的键名保持一致,包括大小写,以方便编码。

2、然后将HttpRequest事件代码改为:

Select Case e.path
    Case "wefox"
        If e.Request.HttpMethod = "GET"
            If Functions.Execute("VerifySignature",e) Then
                e.WriteString(e.GetValues("echostr"))
            End If
        ElseIf e.Request.HttpMethod = "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("ThumbMediaId") = xo("ThumbMediaId")
                    dr("PicUrl") = xo("PicUrl")
                    dr("Content") = xo("Content")
                    dr("Format") = xo("Format")
                    dr("Recognition") = xo("Recognition")
                    dr.Save()
            End Select
        End
If

End
Select

现在即可将收到的用户消息保存在数据表中。

你也可以将代码改为:

Select Case e.path
    Case "wefox"
        If e.Request.HttpMethod = "GET"
            If Functions.Execute("VerifySignature",e) Then
                e.WriteString(e.GetValues("echostr"))
            End If
        ElseIf e.Request.HttpMethod = "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("CreateTime") = st.AddSeconds(xo("CreateTime"))
                    Dim nms() As String = {"FromUserName","MsgType","MsgId","Content","MediaId","ThumbMediaId","PicUrl","Format","Recognition"}
                    For Each nm As String In nms
                        dr(nm) = xo(nm)
                    Next
                    dr.Save()
           
End Select
        End
If

End
Select

可以看到,设计表的时候,列名和消息键名保持一致(包括大小写),可以简化编码。

同样实际开发的时候,建议将保存消息的代码做成自定义函数,以简化HttpRequest事件的代码。

过滤重复消息

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

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


Select
Case e.path
    Case "wefox"
        If e.Request.HttpMethod = "GET"
            If Functions.Execute("VerifySignature",e) Then
                e.WriteString(e.GetValues("echostr"))
            End If
        ElseIf e.Request.HttpMethod = "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"
                    If DataTables("Message").Find("MsgID = '" & xo("MsgId").ToString() & "'") IsNot  Nothing Then
'
如果有相同MsgID的信息直接返回
                        Return
                    End If
                    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("ThumbMediaId") = xo("ThumbMediaId")
                    dr("PicUrl") = xo("PicUrl")
                    dr("Content") = xo("Content")
                    dr("Format") = xo("Format")
                    dr("Recognition") = xo("Recognition")
                    dr.Save()
            End Select
        End
If

End
Select


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