被动回复消息

关于被动回复消息的接口说明,请参考:

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

可以看到被动回复的消息,也是XML格式的。

被动回复文本消息

如果要被动回复文本消息,请参考以下的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 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,今天是" & Format(Date.Today,"yyyyMMdd")
                    e.WriteString(so.ToXML)
            End Select
       
End If
End
Select

现在用户向我们的测试号发送消息,会收到类似以下内容的回复:

您好,我是Foxtable,今天是2017年02月01日


当然实际开发的时候,我们会根据用户消息的不同,经过处理之后,回复不同的内容。

另外同样建议将回复消息的代码做成自定义函数,以简化HttpRequest事件代码。

被动回复图文消息

如果要被动回复图文消息,请参考以下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 so As New Foxtable.XObject()
                    Dim ja As New JArray
                    so("ToUserName") = xo("FromUserName")
                    so("FromUserName") = xo("ToUserName")
                    so("CreateTime") = (Date.Now - st).TotalSeconds()
                    so("MsgType") = "news"
                    so("ArticleCount") = 1
'
图文数量
                    For cnt As Integer = 1 To 1
'
可以回最多8条图文,这里只回1
                       Dim ao As New JObject
                        ao("Title") =
"
特朗普提名保守派大法官" '图文标题
                        ao("Description") =
"
特朗普在白宫提名尼尔·戈萨奇为美国最高法院大法官" '图文简介
                        ao("PicUrl") = "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1632653074,1222564348&fm=80&w=179&h=119&img.JPEG"
'
图片
                        ao("Url") = "http://news.ifeng.com/a/20170201/50640529_0.shtml"
'
单击阅读原文后跳转的连接
                        ja.Add(ao)
                    Next
                    so("Articles") = New JObject()
                    so("Articles")("item") = ja
                    e.WriteString(so.ToXML)
           
End Select
        End
If

End
Select

现在用户向我们的测试号发送消息,会收到类似以下内容的图文回复:




准备素材

不管是被动回复还是主动发送,除了文本和图文消息,其它要发送给用户的消息,包括图片、视频和语音,都必须事先将相关文件上传到微信服务器。

因为微信服务器并不会直接给用户发送文件,而是发送一个素材ID(MediaId),每一个素材ID都对应者一个素材文件。

关于素材的管理,请参考后面的《素材接口》这一章。

被动回复图片消息

必须事先将要回复的图片上传到微信服务器,因为回复的只能是图片素材的ID。

被动回复图片消息参考代码:

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 so As New Foxtable.XObject()
                    so("ToUserName") = xo("FromUserName")
                    so("FromUserName") = xo("ToUserName")
                    so("CreateTime") = (Date.Now - st).TotalSeconds()
                    so("MsgType") = "image"
                    so("Image") = New JObject
                    so("Image")("MediaId") = "eHDdfBQmRtOJ-PtkD6WENicqBE7-tFFczZ6p5ZUnz0k"
'
图片素材ID
                    e.WriteString(so.ToXML)
            End Select
        End
If

End
Select

被动回复语音消息

必须事先将要回复的语音文件上传到微信服务器,因为回复的只能是语音素材的ID。

被动回复语音消息参考代码:

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 so As New Foxtable.XObject()
                    so("ToUserName") = xo("FromUserName")
                    so("FromUserName") = xo("ToUserName")
                    so("CreateTime") = (Date.Now - st).TotalSeconds()
                    so("MsgType") = "voice"
                    so("Voice") = New JObject
                    so("Voice")("MediaId") = "eHDdfBQmRtOJ-PtkD6WENrp8gcW8NSKXkp9sOzn3NoQ"
'
语音素材ID    
                    e.WriteString(so.ToXML)
            End Select
        End
If

End
Select

被动回复视频消息

必须事先将要回复的视频文件上传到微信服务器,因为回复的只能是是因素材的ID。

被动回复视频消息参考代码:

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 so As New Foxtable.XObject()
                    so("ToUserName") = xo("FromUserName")
                    so("FromUserName") = xo("ToUserName")
                    so("CreateTime") = (Date.Now - st).TotalSeconds()
                    so("MsgType") = "video"
                    so("Video") = New JObject
                    so("Video")("MediaId") = "eHDdfBQmRtOJ-PtkD6WENoqiLnKOzd9llepgH1VioLM"
'
语音素材ID
                    so("Video")("Title") =
"
视频标题"
                    so("Video")("Description") =
"
视频内容简介"
                    e.WriteString(so.ToXML)
            End Select
       
End If
End
Select


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