ExWechat

Elixir/Phoenix wechat api wraper, (documentation).
Installation
-
Add
ex_wechatto your list of dependencies inmix.exs:def deps do[{:ex_wechat, "~> 0.1.7"}]end -
Ensure
ex_wechatis started before your application:
def application do
[extra_applications: [:ex_wechat]]
end
Usage
From v0.1.7, we have change the main module from ExWechat to Wechat.
Single Wechat api usage
-
Add config data(you can use
direnvto set yourENV):config :ex_wechat, Wechat,appid: System.get_env("WECHAT_APPID") || "your appid",secret: System.get_env("WECHAT_APPSECRET") || "your app secret",token: System.get_env("WECHAT_TOKEN") || "your token", -
Use the methods in
Wechatmodule, you can get all the method name from doc or from the definition files inlib/apis.Wechat.get_user_listWechat.get_menuWechat.Message.send_custom(openid, msgtype: "text", content: "Hello!")
Multi-account apis usage
-
Add your own module:
defmodule Wechat.User douse Wechat.Api, appid: "", secret: "", token: ""enddefmodule Wechat.Doctor douse Wechat.Api, appid: "", secret: "", token: ""end -
Use the methods in
Wechatwith the module you define:Wechat.User.get_user_listWechat.Doctor.get_user_listWechat.Message.send_custom(Wechat.User, openid, msgtype: "text", content: "Hello!")Wechat.Message.send_custom(Wechat.Doctor, openid, msgtype: "text", content: "Hello!")
Phoenix Plugs
-
Example usage with server verify and message responder:
defmodule Wechat.Router douse Wechat.Web, :routerpipeline :api doplug :accepts, ["json"]endpipeline :customer do# single wechat app don't need to add apiplug Wechat.Plugs.WechatSignatureResponder# you can add your own api module(for multi-accounts support)plug Wechat.Plugs.WechatSignatureResponder, api: Wechat.Customerplug Wechat.Plugs.WechatMessageParserendscope "/customer_wechat", Wechat dopipe_through [:api, :customer]get "/", CustomerWechatController, :indexpost "/", CustomerWechatController, :createendenddefmodule Wechat.CustomerWechatController douse Wechat.Web, :controlleruse Wechat.Responderend -
In your controller you can use the helper methods in
Wechat.Responderto respond with user:def on_text_responder(conn), do: conndef on_image_responder(conn), do: conndef on_voice_responder(conn), do: conndef on_video_responder(conn), do: conndef on_shortvideo_responder(conn), do: conndef on_location_responder(conn), do: conndef on_link_responder(conn), do: conndef on_event_responder(conn), do: conndefmodule Wechat.CustomerWechatController douse Wechat.Web, :controlleruse Wechat.Responderimport Wechat.Messagedefp on_text_responder(conn) domessage = conn.assigns[:message]case message do%{content: "我要图"} ->reply_with(conn, generate_passive(message, msgtype: "news",articles: [%{ title: "sjsjssjsj", description: "xxxxlaldsaldskl",picurl: "picurl", url: "http://baidu.com" },%{ title: "sjsjssjsj", description: "xxxxlaldsaldskl",picurl: "picurl", url: "http://baidu.com" }]))%{content: content} ->reply_with(conn, generate_passive(message, msgtype: "text",content: String.reverse(content)))_ ->connendendend -
Example usage with wechat site plug (get wechat user info):
defmodule Wechat.Router douse Wechat.Web, :routerpipeline :browser doplug :accepts, ["html"]plug :fetch_sessionplug :fetch_flashplug :protect_from_forgeryplug :put_secure_browser_headersendpipeline :wechat_website do# use the api you define, set the scope, the state module# and set the redirect url you want to.plug Wechat.Plugs.WechatWebsite, api: Wechat.Customer,host: "http://wechat.one-picture.com", scope: "snsapi_userinfo",state: Wechat.PageController# use the Wechat(default), scope is snsapi_base(default)plug Wechat.Plugs.WechatWebsite,host: "http://wechat.one-picture.com"endscope "/", Wechat dopipe_through [:browser, :wechat_website]get "/", PageController, :indexget "/about", PageController, :aboutendenddefmodule Wechat.PageController douse Wechat.Web, :controllerdef index(conn, _params) do# get the openid from sessionopenid = conn |> get_session(:openid)# get the wechat authorize data from assins# include the access_token and other info,# you can use this info to get the user's infos.wechat_result = conn.assigns[:wechat_result]render conn, "index.html"enddef about(conn, _params) dorender(conn, "about.html")enddef state do# you can set the state you want.'your_state'endend
License
MIT license