Wechat API wrapper in Elixir.
-
Add
wechat
to your list of dependencies inmix.exs
:def deps do [{:wechat, "~> 0.1.0"}] end
-
Dont start
wechat
in mix file -
Start a GenServer (Or start multiple with different account info) with the offical account info:
wechat_config_data = %Wechat.ConfigData{appid: my_official_appid, secret: my_official_secret, token_file: "/choose/a/temp/location/"} #Default is /tmp/access_token Wechat.start(:my_genserver, wechat_config_data)
-
Add config in
config.exs
config :wechat, Wechat, token: "wechat token", encoding_aes_key: "32bits key"
- send_text_message
Wechat.send_text_message(:my_genserver, open_id, text)
-
Wechat.Plugs.CheckUrlSignature
- Check url signature
- 接入指南
-
Wechat.Plugs.CheckMsgSignature
- Parse xml message (support decrypt msg)
- 消息加密解密技术方案
-
router.ex
defmodule MyApp.Router do pipeline :api do plug :accepts, ["json"] end scope "/wechat", MyApp do pipe_through :api # validate wechat server config get "/", WechatController, :index # receive wechat push message post "/", WechatController, :create end end
-
wechat_controller.ex
defmodule MyApp.WechatController do use MyApp.Web, :controller plug Wechat.Plugs.CheckUrlSignature plug Wechat.Plugs.CheckMsgSignature when action in [:create] def index(conn, %{"echostr" => echostr}) do text conn, echostr end def create(conn, _params) do msg = conn.assigns[:msg] reply = build_text_reply(msg, msg.content) render conn, "text.xml", reply: reply end defp build_text_reply(%{tousername: to, fromusername: from}, content) do %{from: to, to: from, content: content} end end
-
text.xml.eex
<xml> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[<%= @reply.content %>]]></Content> <ToUserName><![CDATA[<%= @reply.to %>]]></ToUserName> <FromUserName><![CDATA[<%= @reply.from %>]]></FromUserName> <CreateTime><%= DateTime.to_unix(DateTime.utc_now) %></CreateTime> </xml>