被动回复消息
我们可以在HttpRequest事件中被动回复消息,被动回复的消息也是XML格式的。
被动回复的消息必须先进行加密,Foxtable内置了一个WXBizCrypt类,专门用于微信消息的加解密。
除了要加解密,企业微信被动回复消息和公众号几乎一模一样。
被动回复文本消息
如果要被动回复文本消息,请参考以下的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
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,"yyyy年MM月dd日")
msg =
wbiz .EncryptMsg(e,so.ToXML
) '加密要发送的消息
If
IsNumeric(msg)
Then
'如果加密失败
Dim
err As
String = wbiz.GetErorDesc(CInt(msg))
'获取错误描述
Else
e.WriteString(msg)
End
If
End
Select
End
If
End
Select
被动回复图文消息
如果要被动回复图文消息,请参考以下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
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
msg =
wbiz .EncryptMsg(e,so.ToXML
) '加密要发送的消息
If
IsNumeric(msg)
Then
'如果加密失败
Dim
err As
String = wbiz.GetErorDesc(CInt(msg))
'获取错误描述
Else
e.WriteString(msg)
End
If
End
Select
End
If
End
Select
现在用户向我们的测试号发送消息,会收到类似以下内容的图文回复:
准备素材
不管是被动回复还是主动发送,除了文本和图文消息,其它要发送给用户的消息,包括图片、视频和语音,都必须事先将相关文件上传到微信服务器。
因为微信服务器并不会直接给用户发送文件,而是发送一个素材ID(MediaId),每一个素材ID都对应者一个素材文件。
关于素材的管理,请参考后面的《素材接口》这一章。
被动回复图片消息
必须事先将要回复的图片上传到微信服务器,因为回复的只能是图片素材的ID。
被动回复图片消息参考代码:
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
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")
=
"2UgIup9iQcz60YIjrpxPFSUTYoPdTAqCz52PXUruWM6isWNdPOQwRgPY3lzJmsKLxPeSWqLMUMREyg9K-4MO8bg"
'图片素材ID
msg =
wbiz .EncryptMsg(e,so.ToXML
) '加密要发送的消息
If
IsNumeric(msg)
Then
'如果加密失败
Dim
err As
String = wbiz.GetErorDesc(CInt(msg))
'获取错误描述
Else
e.WriteString(msg)
End
If
End
Select
End
If
End
Select
被动回复语音消息
必须事先将要回复的语音文件上传到微信服务器,因为回复的只能是语音素材的ID。
被动回复语音消息参考代码:
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
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")
=
"20U2cc1RA4G8ZOU573NDgQlF0BsBHtiyl5FfxkxUc5jbZxFZqqsD3Ab2AGywhU-s-SP6Jgjs76RciJfWZQ68OVg"
'语音素材ID
msg =
wbiz .EncryptMsg(e,so.ToXML
) '加密要发送的消息
If
IsNumeric(msg)
Then
'如果加密失败
Dim
err As
String = wbiz.GetErorDesc(CInt(msg))
'获取错误描述
Else
e.WriteString(msg)
End
If
End
Select
End
If
End
Select
被动回复视频消息
必须事先将要回复的视频文件上传到微信服务器,因为回复的只能是是因素材的ID。
被动回复视频消息参考代码:
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
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")
=
"24rRqRLyJMJXT-FAtspbOc1pkPwZleQ9t6PRBMK-hQY-0pPjSRmWQUUGjTda_pVn0SwbFGzjX4DtONQNqCQe7rg"
'语音素材ID
so("Video")("Title")
= "视频标题"
so("Video")("Description")
= "视频内容简介"
msg =
wbiz .EncryptMsg(e,so.ToXML
) '加密要发送的消息
If
IsNumeric(msg)
Then
'如果加密失败
Dim
err As
String = wbiz.GetErorDesc(CInt(msg))
'获取错误描述
Else
e.WriteString(msg)
End
If
End
Select
End
If
End
Select