Skip to content

Commit

Permalink
更新 WeChatTag (#702)
Browse files Browse the repository at this point in the history
Co-authored-by: yaoqiankun <yaoqiankun@datarc.cn>
  • Loading branch information
yqkcn and yaoqiankun committed Mar 15, 2022
1 parent 1688490 commit ad43416
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions wechatpy/client/api/tag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

from operator import itemgetter

from wechatpy.utils import to_text
from wechatpy.client.api.base import BaseWeChatAPI
Expand All @@ -10,6 +10,9 @@ def create(self, name):
"""
创建标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param name: 标签名(30个字符以内)
:return: 返回的 JSON 数据包
Expand All @@ -18,25 +21,28 @@ def create(self, name):
return self._post(
"tags/create",
data={"tag": {"name": name}},
result_processor=lambda x: x["tag"],
result_processor=itemgetter("tag"),
)

def get(self):
"""
获取公众号已创建的标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:return: 所有标签列表
"""

res = self._get("tags/get", result_processor=lambda x: x["tags"])

return res
return self._get("tags/get", result_processor=itemgetter("tags"))

def update(self, tag_id, name):
"""
编辑标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param tag_id: 标签id,由微信分配
:param name: 标签名字(30个字符以内)
:return: 返回的 JSON 数据包
Expand All @@ -49,6 +55,9 @@ def delete(self, tag_id):
"""
删除标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param tag_id: 标签 ID
:return: 返回的 JSON 数据包
Expand All @@ -59,6 +68,9 @@ def tag_user(self, tag_id, user_id):
"""
批量为用户打标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param tag_id: 标签 ID
:param user_id: 用户 ID, 可以是单个或者列表
Expand All @@ -78,6 +90,9 @@ def untag_user(self, tag_id, user_id):
"""
批量为用户取消标签
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param tag_id: 标签 ID
:param user_id: 用户 ID, 可以是单个或者列表
Expand All @@ -97,19 +112,25 @@ def get_user_tag(self, user_id):
"""
获取用户身上的标签列表
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param user_id: 用户 ID, 可以是单个或者列表
:return: 返回的 JSON 数据包
"""
return self._post(
"tags/getidlist",
data={"openid": user_id},
result_processor=lambda x: x["tagid_list"],
result_processor=itemgetter("tagid_list"),
)

def get_tag_users(self, tag_id, first_user_id=None):
"""
获取标签下粉丝列表
详情请参考
https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
:param tag_id: 标签 ID
:param first_user_id: 可选。第一个拉取的 OPENID,不填默认从头开始拉取
:return: 返回的 JSON 数据包
Expand All @@ -123,17 +144,17 @@ def get_tag_users(self, tag_id, first_user_id=None):

def iter_tag_users(self, tag_id, first_user_id=None):
"""
获取标签下粉丝openid列表
获取标签下粉丝 openid 的生成器
:return: 返回一个迭代器,可以用for进行循环,得到openid
:return: 返回一个迭代器,可以用 for 进行循环,得到 openid
使用示例::
from wechatpy import WeChatClient
client = WeChatClient('appid', 'secret')
for openid in client.tag.iter_tag_users(0):
print(openid)
>>> from wechatpy import WeChatClient
>>>
>>> client = WeChatClient('appid', 'secret')
>>> for openid in client.tag.iter_tag_users(0):
>>> print(openid)
"""
while True:
Expand All @@ -149,8 +170,9 @@ def iter_tag_users(self, tag_id, first_user_id=None):
def get_black_list(self, begin_openid=None):
"""
获取公众号的黑名单列表
详情请参考
https://mp.weixin.qq.com/wiki?id=mp1471422259_pJMWA
https://developers.weixin.qq.com/doc/offiaccount/User_Management/Manage_blacklist.html
:param begin_openid: 起始的 OpenID,传空则默认从头开始拉取
:return: 返回的 JSON 数据包
Expand All @@ -167,8 +189,9 @@ def get_black_list(self, begin_openid=None):
def batch_black_list(self, openid_list):
"""
批量拉黑用户
详情请参考
https://mp.weixin.qq.com/wiki?id=mp1471422259_pJMWA
https://developers.weixin.qq.com/doc/offiaccount/User_Management/Manage_blacklist.html
:param openid_list: 批量拉黑用户的 OpenID list, 最多20个
:type openid_list: list
Expand All @@ -183,8 +206,9 @@ def batch_black_list(self, openid_list):
def batch_unblack_list(self, openid_list):
"""
批量取消拉黑
详情请参考
https://mp.weixin.qq.com/wiki?id=mp1471422259_pJMWA
https://developers.weixin.qq.com/doc/offiaccount/User_Management/Manage_blacklist.html
:param openid_list: 批量取消拉黑的 OpenID list, 最多20个
:type openid_list: list
Expand Down

0 comments on commit ad43416

Please sign in to comment.