用户标签管理
标签用于给用户分类,一个公众号可以创建100个标签,一个用户可以有多个标签。
用户标签管理接口说明,请参考:
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
首先创建一个表,用于保存用户标签设置,表名为tag,结构如下:
创建标签
一个公众号可以创建100个标签,创建标签的代码参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/create?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
jo("tag")
= New JObject()
jo("tag")("name")
= "vip"
hc.Content
= jo.ToString
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
Is Nothing
Then
Dim dr
As DataRow
= DataTables("tag").AddNew()
dr("id")
= jo("tag")("id")
dr("name")
= jo("tag")("name")
dr.Save()
Else
MessageBox.Show(jo.ToString)
End
If
以上代码创建了一个名为vip的标签。
修改标签
我们可以修改标签名称,例如将ID为104的标签名改为"VIP用户",参考代码为:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/update?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
jo("tag")
= New JObject()
jo("tag")("id")
= "104"
jo("tag")("name")
= "VIP用户"
hc.Content
= jo.ToString
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("标签修改成功")
Else
MessageBox.Show(jo.ToString)
End
If
删除标签
删除标签的代码请参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
jo("tag")
= New JObject()
jo("tag")("id")
= "101"
hc.Content
= jo.ToString
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
DataTables("tag").DeleteFor("id
= '101'")
Else
MessageBox.show(jo.ToString)
End
If
列出现有标签
列出现有标签的代码请参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/get?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
JObject = JObject.Parse(hc.GetData)
If
jo("errcode")
Is Nothing
Then
DataTables("tag").DataRows.Clear()
For Each
tg As
JToken In
jo("tags")
Dim dr
As DataRow
= DataTables("tag").AddNew()
dr("id")
= tg("id")
'标签id
dr("name")
= tg("name")
'标签名
dr("count")
= tg("count")
'此标签下的用户数
Next
Else
MessageBox.show(jo.ToString)
End
If
批量为用户打标签
可以批量为用户打标签,每个用户最多可以打三个标签,代码参考:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
Dim
ja As
New JArray()
ja.Add("ofjtFwBSZ5cNqTKLSKx2TNEAxfBI")
ja.Add("ofjtFwGvPBfy9aGz9OJ-qWiG5oA0")
jo("openid_list")
= ja
jo("tagid")
= "104"
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("批量打标签成功!")
Else
MessageBox.show(jo.ToString)
End
If
批量为用户取消标签
如果要为若干用户取消指定标签,可以参考代码:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
Dim
ja As
New JArray()
ja.Add("ofjtFwBSZ5cNqTKLSKx2TNEAxfBI")
ja.Add("ofjtFwGvPBfy9aGz9OJ-qWiG5oA0")
jo("openid_list")
= ja
jo("tagid")
= "104"
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
= "0" Then
MessageBox.Show("批量取消标签成功!")
Else
MessageBox.show(jo.ToString)
End
If
获取某用户的标签
例如在命令窗口执行下面的代码,可以列出指定用户的全部标签:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
jo("openid")
= "ofjtFwBSZ5cNqTKLSKx2TNEAxfBI"
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("tagid_list")
IsNot Nothing
Then
For Each
jk As
JToken In
jo("tagid_list")
Output.show(jk)
Next
Else
MessageBox.show(jo.ToString)
End
If
获取某标签下的所有用户
例如在命令窗口执行下面的代码,可以列出ID为"104"标签下的所有用户:
Dim
url As
String =
"https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={0}"
Dim
hc As
New HttpClient(CExp(url,
Functions.Execute("GetAccessToken")))
Dim
jo As
New JObject()
jo("tagid")
= "104"
hc.Content
= jo.ToString()
jo =
JObject.Parse(hc.GetData)
If
jo("errcode")
Is Nothing
Then
If CInt(jo("count"))
> 0 Then
For Each
jk As
JToken In
jo("data")("openid")
Output.show(jk)
Next
End If
Else
MessageBox.show(jo.ToString)
End
If