https://github.com/goofansu/wechat-elixir
Wechat API wrapper in Elixir
https://github.com/goofansu/wechat-elixir
elixir wechat wechat-sdk weixin weixin-sdk
Last synced: 2 months ago
JSON representation
Wechat API wrapper in Elixir
- Host: GitHub
- URL: https://github.com/goofansu/wechat-elixir
- Owner: elixir-wechat
- License: mit
- Created: 2016-07-22T10:03:08.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2021-08-01T01:15:33.000Z (over 3 years ago)
- Last Synced: 2025-01-31T21:52:19.201Z (2 months ago)
- Topics: elixir, wechat, wechat-sdk, weixin, weixin-sdk
- Language: Elixir
- Homepage: https://hex.pm/packages/wechat
- Size: 299 KB
- Stars: 74
- Watchers: 9
- Forks: 18
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-wechat - goofansu/wechat-elixir
README
Wechat API wrapper in Elixir.[](https://github.com/elixir-wechat/wechat/actions?query=workflow%3ACI)
[](https://hex.pm/packages/wechat)
## Installation
```elixir
def deps do
[{:wechat, "~> 0.4.0"}]
end
```## Configuration (optional)
```elixir
config :wechat,
adapter_opts: {Wechat.Adapters.Redis, ["redis://localhost:6379/0"]},
httpoison_opts: [recv_timeout: 300_000]
```## Create a client to call APIs
```elixir
iex(1)> client = Wechat.Client.new(appid: "WECHAT_APPID", secret: "WECHAT_SECRET")
%Wechat.Client{
appid: "WECHAT_APPID",
secret: "WECHAT_SECRET",
endpoint: "https://api.weixin.qq.com/"
}iex(2)> Wechat.User.get(client)
{:ok,
%{
"count" => 1,
"data" => %{"openid" => ["oi00OuKAhA8bm5okpaIDs7WmUZr4"]},
"next_openid" => "oi00OuKAhA8bm5okpaIDs7WmUZr4",
"total" => 1
}}
```## Create a Wechat implementation
You can implement the `Wechat` module to simplify the usage.
First, create an implementation by `use Wechat` :
```elixir
defmodule MyApp.Wechat do
use Wechat, otp_app: :my_appdef users do
client() |> Wechat.User.get()
end
end
```Config the implementation with Wechat credentials:
```elixir
config :my_app, MyApp.Wechat,
appid: "APP_ID",
secret: "APP_SECRET",
token: "TOKEN",
encoding_aes_key: "ENCODING_AES_KEY" # Required if you enabled the encrypt mode
```## Wechat implementation examples
### JS-SDK
[https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html)
```html
<%= raw MyApp.Wechat.wechat_config_js(@conn, debug: false, api: ~w(previewImage closeWindow)) %>
$(function() {
var urls = [];
$('img').map(function(){
url = window.location.origin + $(this).attr('src'),
urls.push(url);
});$('img').click(function(e) {
wx.previewImage({
current: window.location.origin + $(this).attr('src'),
urls: urls
});
})
});```
### Process message in Phoenix
[https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html](https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html)
- router.ex
```elixir
defmodule MyApp.Router do
scope "/wechat", MyApp do
resources "/", WechatController, [:index, :create]
end
end
```- wechat_controller.ex
```elixir
defmodule MyApp.WechatController do
use MyApp.Web, :controller# Validate signature param
plug Wechat.Plugs.RequestValidator, module: MyApp.Wechat# Parse message
plug Wechat.Plugs.MessageParser, [module: MyApp.Wechat] when action in [:create]def index(conn, %{"echostr" => echostr}) do
text conn, echostr
enddef create(conn, _params) do
%{"ToUserName" => to, "FromUserName" => from, "Content" => content} = conn.body_params
reply = %{from: to, to: from, content: content}msg = Phoenix.View.render_to_string(EvercamWechatWeb.WechatView, "text.xml", reply: reply)
# Return encrypted message if possible
case Wechat.encrypt_message(msg) do
{:ok, reply} ->
render(conn, "encrypt.xml", reply: reply){:error, _} ->
text(conn, msg)
end
end
end
```- text.xml.eex
```xml
]]>
]]>
]]>
<%= DateTime.to_unix(DateTime.utc_now) %>```
- encrypt.xml.eex
```xml
]]>
]]>
<%= @reply.timestamp %>
]]>```
## Users
* [evercam_wechat](https://github.com/evercam/evercam_wechat)