Current section

Files

Jump to
exwechat lib wechat tag.ex
Raw

lib/wechat/tag.ex

defmodule ExWechat.Tag do
@moduledoc """
微信用户标签管理接口
- 实现对公众号的标签进行创建、查询、修改、删除等操作
- 对用户进行打标签、取消标签等操作
例子
- 查询标签: `query()`
- 创建标签: `create("myTag")`
"""
alias ExWechat.Util
@doc """
获取公众号已创建的标签
"""
def query do
"https://api.weixin.qq.com/cgi-bin/tags/get?access_token=#{ExWechat.token()}"
|> HTTPoison.get
|> Util.parse_resp
|> Util.check_wechat_resp
end
@doc """
创建标签号
- 一个公众号,最多可以创建100个标签
"""
@spec create(String.t()) :: {:ok, map} | {:error, map}
def create(tag) do
body = %{tag: %{name: tag}} |> Poison.encode!
"https://api.weixin.qq.com/cgi-bin/tags/create?access_token=#{ExWechat.token()}"
|> HTTPoison.post!(body, [Util.content_type(:json)])
|> Util.parse_resp
|> Util.check_wechat_resp
end
@doc """
编辑标签号
"""
def update(id, tag) do
body = %{tag: %{id: id, name: tag}} |> Poison.encode!
"https://api.weixin.qq.com/cgi-bin/tags/update?access_token=#{ExWechat.token()}"
|> HTTPoison.post!(body, [Util.content_type(:json)])
|> Util.parse_resp
|> Util.check_wechat_resp
end
@doc """
删除标签号
"""
def destroy(id) do
body = %{tag: %{id: id}} |> Poison.encode!
"https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=#{ExWechat.token()}"
|> HTTPoison.post!(body, [Util.content_type(:json)])
|> Util.parse_resp
|> Util.check_wechat_resp
end
@doc """
批量为用户打标签
- 可以传入用户的 OpenID 数组,也可以传入单一的 OpenID
"""
def add_members(id, openids) when is_list(openids) do
body = %{tagid: id, openid_list: openids} |> Poison.encode!
"https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=#{ExWechat.token()}"
|> HTTPoison.post!(body, [Util.content_type(:json)])
|> Util.parse_resp
|> Util.check_wechat_resp
end
def add_members(id, openid) do
id |> add_members([openid])
end
@doc """
查询用户的标签列表
"""
def query_members(openid) do
body = %{openid: openid} |> Poison.encode!
"https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=#{ExWechat.token()}"
|> HTTPoison.post!(body, [Util.content_type(:json)])
|> Util.parse_resp
|> Util.check_wechat_resp
end
end