View Source LibWechat (lib_wechat v0.2.1)

LibWechat

This SDK provides an Elixir interface to interact with Wechat's APIs.

Head to the API reference for usage details.

Installation

Add the dependency to your mix.exs file:

def deps do
  [
    {:lib_wechat, "~> 0.2"}
  ]
end

Usage

  1. Create a new instance of LibWechat with your appid and secret.
wechat =
  LibWechat.new(
    appid: "Your APP ID",
    secret: "Your APP SECRET"
  )
  1. Add LibWechat to your supervision tree.
children = [
  {LibWechat, wechat: wechat}
]

Supervisor.init(children, strategy: :one_for_one)
  1. Call the API you want to use.
# Get access token
LibWechat.get_access_token(wechat)

# Get miniapp session with code
LibWechat.jscode_to_session(wechat, "jscode")

# Get unlimited miniapp wxacode
LibWechat.get_unlimited_wxacode(wechat, token,
    %{"scene" => "foo=bar",
      "page" => "pages/index/index",
      "width" => 430,
      "auto_color" => false,
      "line_color" => %{"r" => 0, "g" => 0, "b" => 0},
      "is_hyaline" => false
    })

#.... see more api in lib_wechat.ex

License

This project is licensed under the MIT License - see the LICENSE file for details

Summary

Types

@type err_t() :: {:error, LibWechat.Error.t()}
@type ok_t(m) :: {:ok, m}
@type options_t() ::
  keyword(
    {:name, atom()}
    | {:client, term()}
    | {:appid, binary()}
    | {:secret, binary()}
  )
@type t() :: %LibWechat{
  appid: binary(),
  client: Client.t(),
  name: GenServer.name(),
  secret: binary()
}
@type token_t() :: String.t()

Functions

Link to this function

generate_scheme(lib_wechat, token, payload)

View Source

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html

Examples

{:ok, %{
  "errcode" => 0,
  "errmsg" => "ok",
  "openlink" => "weixin://dl/business/?t=Akeatr890b"
}} = LibWechat.generate_scheme(wechat, token,
  %{
    "jump_wxa" => %{
      "path" => "pages/index/index",
      "query" => "foo=bar"
    },
    "is_expire" => false,
    "expire_type" => 0,
    "expire_time" => 0
  }
)
Link to this function

get_access_token(lib_wechat)

View Source
@spec get_access_token(t()) :: ok_t(LibWechat.Typespecs.string_dict()) | err_t()

获取access_token https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

Examples

{:ok, %{"access_token"=>"xxx"}} = LibWechat.get_access_token(wechat)
Link to this function

get_phone_number(wechat, token, code)

View Source
@spec get_phone_number(t(), token_t(), String.t()) ::
  ok_t(LibWechat.Typespecs.string_dict()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html

Examples

{:ok,
  %{
      "errcode":0,
      "errmsg":"ok",
      "phone_info": {
        "phoneNumber":"xxxxxx",
        "purePhoneNumber": "xxxxxx",
        "countryCode": 86,
        "watermark": {
            "timestamp": 1637744274,
            "appid": "xxxx"
        }
      }
    }
  } = get_phone_number(wechat, token, code)
Link to this function

get_unlimited_wxacode(lib_wechat, token, payload)

View Source
@spec get_unlimited_wxacode(t(), token_t(), LibWechat.Typespecs.string_dict()) ::
  ok_t(binary()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

Examples

{:ok, <<255, 216, ...>>} = LibWechat.get_unlimited_wxacode(wechat, token,
  %{"scene" => "foo=bar",
    "page" => "pages/index/index",
    "width" => 430,
    "auto_color" => false,
    "line_color" => %{"r" => 0, "g" => 0, "b" => 0},
    "is_hyaline" => false
  }
)
Link to this function

get_urllink(lib_wechat, token, payload)

View Source

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html

Examples

{:ok, %{
  "errcode" => 0,
  "errmsg" => "ok",
  "url_link" => "https://wxaurl.cn/bz2LB4RMDVqq"
}} = LibWechat.get_urllink(wechat, token,
  %{
    "path" => "pages/index/index",
    "query" => "foo=bar",
    "is_expire" => false,
    "expire_type" => 0,
    "expire_time" => 0
  }
)
Link to this function

jscode_to_session(wechat, code)

View Source
@spec jscode_to_session(t(), String.t()) ::
  ok_t(LibWechat.Typespecs.string_dict()) | err_t()

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

Link to this function

msg_sec_check(lib_wechat, token, payload)

View Source

https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/sec-center/sec-check/msgSecCheck.html

Examples

iex> payload = %{
  "openid"=> "OPENID",
  "scene"=> 1,
  "version"=> 2,
  "content"=> "hello world!"
}
iex> LibWechat.msg_sec_check(wechat, token, payload)
{
   "errcode"=> 0,
   "errmsg"=> "ok",
   "result"=> %{
       "suggest"=> "risky",
       "label"=> 20001
   },
   "detail"=> [
       %{
           "strategy"=> "content_model",
           "errcode"=> 0,
           "suggest"=> "risky",
           "label"=> 20006,
           "prob"=> 90
       },
       %{
           "strategy": "keyword",
           "errcode": 0,
           "suggest": "pass",
           "label": 20006,
           "level": 20,
           "keyword": "命中的关键词1"
       },
       {
           "strategy": "keyword",
           "errcode": 0,
           "suggest": "risky",
           "label": 20006,
           "level": 90,
           "keyword": "命中的关键词2"
       }
   ],
   "trace_id": "60ae120f-371d5872-7941a05b"
}
@spec new(options_t()) :: t()

create an instance of LibWechat

options

  • :name (atom/0) - name of this process The default value is :wechat.

  • :client (term/0) - http client instance which implements LibWechat.Http behavior The default value is %LibWechat.Http.Default{name: LibWechat.Http.Default}.

  • :appid (String.t/0) - Required. 第三方用户唯一凭证

  • :secret (String.t/0) - Required. 第三方用户唯一凭证密钥,即appsecret

@spec start_link(keyword()) :: GenServer.on_start()
Link to this function

subscribe_send(lib_wechat, token, payload)

View Source

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html

Examples

{:ok, %{
  "errcode" => 0,
  "errmsg" => "ok",
  "msgid" => 294402298110051942
  }} = LibWechat.subscribe_send(wechat, token,
  %{
    "touser" => "OPENID",
    "template_id" => "TEMPLATE_ID",
    "page" => "index",
    "miniprogram_state" => "developer",
    "lang" => "zh_CN",
    "data" => %{
      "number01" => %{"value" => "339208499"},
      "date01" => %{"value" => "2015年01月05日"},
      "site01" => %{"value" => "TIT创意园"},
      "site02" => %{"value" => "广州市新港中路397号"}
    }
  }
)
Link to this function

uniform_send(wechat, token, body)

View Source

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html

Examples

{:ok, %{"errcode" => 0, "errmsg" => "ok"}} = LibWechat.uniform_send(wechat, token,
  %{
    "touser" => "OPENID",
    "weapp_template_msg" => %{
      "template_id" => "TEMPLATE_ID",
      "page" => "index",
      "form_id" => "FORMID",
      "data" => %{
        "keyword1" => %{"value" => "339208499"},
        "keyword2" => %{"value" => "2015年01月05日"},
        "keyword3" => %{"value" => "粤海喜来登酒店"},
        "keyword4" => %{"value" => "广州市天河区天河路208号"}
      },
      "emphasis_keyword" => "keyword1.DATA"
    },
    "mp_template_msg" => %{
      "appid" => "APPID ",
      "template_id" => "TEMPLATE_ID",
      "url" => "http://weixin.qq.com/download",
      "miniprogram" => %{
        "appid" => "xiaochengxuappid12345",
        "pagepath" => "index?foo=bar"
      },
      "data" => %{
        "first" => %{"value" => "恭喜你购买成功!", "color" => "#173177"},
        "keyword1" => %{"value" => "巧克力", "color" => "#173177"},
        "keyword2" => %{"value" => "39.8元", "color" => "#173177"},
        "keyword3" => %{"value" => "2014年9月22日", "color" => "#173177"},
        "remark" => %{"value" => "欢迎再次购买!", "color" => "#173177"}
      }
    }
  })