接收事件推送

关于接收事件推送的接口说明,请参考:

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

本节只介绍用户关注和取消关注事件,其他类型的事件将在相关章节介绍。

本节的任务是:

1、创建一个用户表,用于保存用户信息。
2、有用户关注测试号,则在用户表增加一行,用于登记此用户信息。
3、有用户取消关注测试号,则在用户表删除对应的行。

显然要完成上述任务,我们还需要了解获取用户信息的接口,参考:

https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId

首先设计结构如下图所示的表,表名为WXUsers,用于记登记已关注用户的信息:

然后将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"
                   
'保存和回复文本和图片消息的代码
               
Case "event" '如果是事件
                    Select Case xo("Event")
                        Case "subscribe"
'
关注事件
                            Dim url As String = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN"
                            url = CExp(url, Functions.Execute("GetAccessToken"), xo("FromUserName"))
                            Dim hc As New HttpClient(url)
                            Dim jo As JObject = Jobject.Parse(hc.GetDaTa)
                            If jo("errcode") IsNot Nothing Then
                               
'这里编写代码记录错误信息,方便查阅
                           
Else
                                Dim dr As DataRow = DataTables("WXUsers").AddNew()
                                Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl","groupid","remark","language"}
                                For Each nm As String In nms
                                    dr(nm) = jo(nm)
                                Next
                                dr("subscribe_time") = Date.Now()
                                dr.Save()
                            End If
                        Case "unsubscribe"
'
取消关注事件
                             DataTables("WXUsers").DeleteFor("openid = '" & xo("FromUserName").ToString() & "'" )
                    End Select
           
End Select
        End
If
End
Select

实际开发的时候,建议大家将相关代码做成自定义函数,避免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"
                    '
保存和收到的消息
                Case "event" '
如果是事件
                    Select Case xo("Event")
                        Case "subscribe" '
关注事件
                            Dim url As String = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN"
                            url = CExp(url, Functions.Execute("GetAccessToken"), xo("FromUserName"))
                            Dim hc As New HttpClient(url)
                            Dim jo As JObject = Jobject.Parse(hc.GetDaTa)
                            If jo("errcode") IsNot Nothing Then
                               
'这里编写代码记录错误信息,方便查阅
                            Else
                               
'保存消息
                                Dim dr As DataRow = DataTables("WXUsers").AddNew()
                                Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl","groupid","remark","language"}
                                For Each nm As String In nms
                                    dr(nm) = jo(nm)
                                Next
                                dr("subscribe_time") = Date.Now()
                                dr.Save()
                                '回复欢迎语
                                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") = "您好,感谢您关注FoxTable,您的关注就是我们的动力."
                                e.WriteString(so.ToXML)
                            End If
                        Case "unsubscribe" '取消关注事件
                            DataTables("WXUsers").DeleteFor("openid = '" & xo("FromUserName").ToString() & "'" )
                    End Select
            End Select
        End If

End
Select


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