@@ -4,47 +4,198 @@ Wechat Rails
4
4
[ ![ Build Status] ( https://travis-ci.org/skinnyworm/omniauth-wechat-oauth2.svg )] ( https://travis-ci.org/skinnyworm/wechat-rails ) [ ![ Code Climate] ( https://codeclimate.com/github/skinnyworm/wechat-rails.png )] ( https://codeclimate.com/github/skinnyworm/wechat-rails ) [ ![ Code Coverage] ( https://codeclimate.com/github/skinnyworm/wechat-rails/coverage.png )] ( https://codeclimate.com/github/skinnyworm/wechat-rails ) [ ![ Gem Version] ( https://badge.fury.io/rb/wechat-rails.png )] ( http://badge.fury.io/rb/wechat-rails )
5
5
6
6
7
- Wechat MP platform provides following services for developers to use
7
+ Wechat-rails 可以帮助开发者方便地在Rails环境中集成微信公众平台提供的所有服务,目前微信公众平台提供了以下几种类型的服务。
8
8
9
- - API service (user query, media retrieval and sending custom messages)
10
- - Messaging service (repspond to messages sent by user)
11
- - OAuth 2.0
9
+ - ##### 微信公众平台基本API, 无需Web环境。
10
+ - ##### 消息处理机制, 需运行在Web环境中。
11
+ - ##### OAuth 2.0认证机制
12
12
13
- Wechat-rails gem helps developer to easily use the API service and Messaging service in rails app. For oauth2 integeration, you can consider [ omniauth-wechat-oauth2] ( https://github.com/skinnyworm/omniauth-wechat-oauth2 ) .
13
+ Wechat-rails gem 包含了一个命令行程序可以调用各种无需web环境的API。同时它也提供了Rails Controller的responder DSL, 可以帮助开发者方便地在Rails应用中集成微信的消息处理机制。如果你的App还需要集成微信OAuth2.0, 你可以考虑 [ omniauth-wechat-oauth2] ( https://github.com/skinnyworm/omniauth-wechat-oauth2 ) , 这个gem可以方便地和devise集成提供完整的用户认证 .
14
14
15
- You need to get a wechat API key at: http ://mp.weixin.qq.com
15
+ 在使用这个Gem前,你需要获得微信API的appid, secret, token。具体情况可以参见 http ://mp.weixin.qq.com
16
16
17
- ## Installation
17
+ ## 安装
18
18
19
- Add to your ` Gemfile ` :
19
+ Using ` gem install ` or add to your app's ` Gemfile ` :
20
+
21
+ ```
22
+ gem install "wechat-rails"
23
+ ```
24
+
25
+ ```
26
+ gem "wechat-rails", git:"https://github.com/skinnyworm/wechat-rails"
27
+ ```
28
+
29
+
30
+ ## 配置
31
+
32
+ #### 命令行程序的配置
33
+
34
+ 要使用命令行程序,你需要在你的home目录中创建一个` ~/.wechat.yml ` ,包含以下内容。其中` access_token ` 是存放access_token的文件位置。
35
+
36
+ ```
37
+ appid: "my_appid"
38
+ secret: "my_secret"
39
+ access_token: "/var/tmp/wechat_access_token"
40
+ ```
41
+
42
+ #### Rails 全局配置
43
+ Rails环境中, 你可以在config中创建wechat.yml, 为每个rails environment创建不同的配置。
44
+
45
+ ```
46
+ default: &default
47
+ appid: "app_id"
48
+ secret: "app_secret"
49
+ token: "app_token"
50
+ access_token: "/var/tmp/wechat_access_token"
51
+
52
+ production:
53
+ appid: <%= ENV['WECHAT_APPID'] %>
54
+ secret: <%= ENV['WECHAT_APP_SECRET'] %>
55
+ token: <%= ENV['WECHAT_TOKEN'] %>
56
+ access_token: <%= ENV['WECHAT_ACCESS_TOKEN'] %>
57
+
58
+ staging:
59
+ <<: *default
60
+
61
+ development:
62
+ <<: *default
63
+
64
+ test:
65
+ <<: *default
66
+ ```
67
+
68
+ #### Rails 为每个Responder配置不同的appid和secret
69
+ 在个别情况下,你的app可能需要处理来自多个公众账号的消息,这时你可以配置多个responder controller。
20
70
21
71
``` ruby
22
- gem " wechat-rails"
72
+ class WechatFirstController < ApplicationController
73
+ wechat_responder appid: " app1" , secret: " secret1" , token: " token1" , access_token: Rails .root.join(" tmp/access_token1" )
74
+
75
+ on :text , with: " help" , respond: " help content"
76
+ end
77
+ ```
78
+
79
+ ## 使用命令行
80
+
81
+ ```
82
+ $ wechat
83
+ Wechat commands:
84
+ wechat custom_image [OPENID, IMAGE_PATH] # 发送图片客服消息
85
+ wechat custom_music [OPENID, THUMBNAIL_PATH, MUSIC_URL] # 发送音乐客服消息
86
+ wechat custom_news [OPENID, NEWS_YAML_FILE] # 发送图文客服消息
87
+ wechat custom_text [OPENID, TEXT_MESSAGE] # 发送文字客服消息
88
+ wechat custom_video [OPENID, VIDEO_PATH] # 发送视频客服消息
89
+ wechat custom_voice [OPENID, VOICE_PATH] # 发送语音客服消息
90
+ wechat help [COMMAND] # Describe available commands or one specific command
91
+ wechat media [MEDIA_ID, PATH] # 媒体下载
92
+ wechat media_create [MEDIA_ID, PATH] # 媒体上传
93
+ wechat menu # 当前菜单
94
+ wechat menu_create [MENU_YAML] # 创建菜单
95
+ wechat menu_delete # 删除菜单
96
+ wechat user [OPEN_ID] # 查找关注者
97
+ wechat users # 关注者列表
98
+
23
99
```
24
100
25
- Then ` bundle install ` .
101
+ ### 使用场景
102
+ 以下是几种典型场景的使用方法
26
103
27
- ### Setup Environment
28
- For development environment, you need to export some environment variables before you can use the commandline utilities.
104
+ #####获取所有用户的OPENID
29
105
30
106
```
31
- export WECHAT_APPID=<app id>
32
- export WECHAT_SECRET=<app secret>
33
- export WECHAT_ACCESS_TOKEN=<储存access_token文件的位置, eg. /var/tmp/access_token>
34
- export TOKEN=<Token可由开发者任意填写,用作生成签名,在配置接口时使用>
107
+ $ wechat users
108
+
109
+ {"total"=>4, "count"=>4, "data"=>{"openid"=>["oCfEht9***********", "oCfEhtwqa***********", "oCfEht9oMCqGo***********", "oCfEht_81H5o2***********"]}, "next_openid"=>"oCfEht_81H5o2***********"}
110
+
35
111
```
36
- For production environment, consult your service configuration for how to expose your environment variables.
37
112
38
- ## Command line utilities
39
- Once the gem was installed, you should have a command line utility for invoking all the api methods.
113
+ #####获取用户的信息
40
114
41
- ## Controller DSL
42
- To set up a rails controlelr for reponding user message is easy. Following is an example
115
+ ```
116
+ $ wechat user "oCfEht9***********"
117
+
118
+ {"subscribe"=>1, "openid"=>"oCfEht9***********", "nickname"=>"Nickname", "sex"=>1, "language"=>"zh_CN", "city"=>"徐汇", "province"=>"上海", "country"=>"中国", "headimgurl"=>"http://wx.qlogo.cn/mmopen/ajNVdqHZLLBd0SG8NjV3UpXZuiaGGPDcaKHebTKiaTyof*********/0", "subscribe_time"=>1395715239}
119
+
120
+ ```
121
+
122
+ #####获取用户的信息
123
+
124
+ ```
125
+ $ wechat user "oCfEht9***********"
126
+
127
+ {"subscribe"=>1, "openid"=>"oCfEht9***********", "nickname"=>"Nickname", "sex"=>1, "language"=>"zh_CN", "city"=>"徐汇", "province"=>"上海", "country"=>"中国", "headimgurl"=>"http://wx.qlogo.cn/mmopen/ajNVdqHZLLBd0SG8NjV3UpXZuiaGGPDcaKHebTKiaTyof*********/0", "subscribe_time"=>1395715239}
128
+ ```
129
+
130
+ ##### 获取当前菜单
131
+ ```
132
+ $ wechat menu
133
+
134
+ {"menu"=>{"button"=>[{"type"=>"view", "name"=>"保护的", "url"=>"http://***/protected", "sub_button"=>[]}, {"type"=>"view", "name"=>"公开的", "url"=>"http://***", "sub_button"=>[]}]}}
135
+
136
+ ```
137
+
138
+ ##### 创建菜单
139
+ 创建菜单需要一个定义菜单内容的yaml文件,比如
140
+ menu.yaml
141
+
142
+ ```
143
+ button:
144
+ -
145
+ type: "view"
146
+ name: "保护的"
147
+ url: "http://***/protected"
148
+ -
149
+ type: "view"
150
+ name: "公开的"
151
+ url: "http://***"
152
+
153
+ ```
154
+
155
+ 然后执行命令行
156
+
157
+ ```
158
+ $ wechat menu_create menu.yaml
159
+
160
+ ```
161
+
162
+ ##### 发送客服图文消息
163
+ 需定义一个图文消息内容的yaml文件,比如
164
+ articles.yaml
165
+
166
+ ```
167
+ articles:
168
+ -
169
+ title: "习近平在布鲁日欧洲学院演讲"
170
+ description: "新华网比利时布鲁日4月1日电 国家主席习近平1日在比利时布鲁日欧洲学院发表重要演讲"
171
+ url: "http://news.sina.com.cn/c/2014-04-01/232629843387.shtml"
172
+ pic_url: "http://i3.sinaimg.cn/dy/c/2014-04-01/1396366518_bYays1.jpg"
173
+ ```
174
+
175
+ 然后执行命令行
176
+
177
+ ```
178
+ $ wechat custom_news oCfEht9oM*********** articles.yml
179
+
180
+ ```
181
+
182
+
183
+
184
+ ## Rails Responder Controller DSL
185
+
186
+ 为了在Rails app中响应用户的消息,开发者需要创建一个wechat responder controller. 首先在router中定义
187
+
188
+ ``` ruby
189
+ resource :wechat , only: [:show , :create ]
190
+
191
+ ```
192
+
193
+ 然后创建Controller class, 例如
43
194
44
195
``` ruby
45
196
46
197
class WechatsController < ApplicationController
47
- wechat_rails
198
+ wechat_responder
48
199
49
200
# 默认的文字信息responder
50
201
on :text do |request , content |
@@ -76,7 +227,7 @@ class WechatsController < ApplicationController
76
227
77
228
# 处理视频信息
78
229
on :video do |request |
79
- nickname = Wechat .api. user(request[:FromUserName ])[" nickname" ] # 呼叫 api 获得发送者的nickname
230
+ nickname = wechat. user(request[:FromUserName ])[" nickname" ] # 调用 api 获得发送者的nickname
80
231
request.reply.video(request[:MediaId ], title: " 回声" , description: " #{ nickname } 发来的视频请求" ) # 直接视频返回给用户
81
232
end
82
233
@@ -86,13 +237,37 @@ class WechatsController < ApplicationController
86
237
end
87
238
88
239
# 当无任何responder处理用户信息时,使用这个responder处理
89
- on :fallback do |request |
90
- request.reply.text " fallback"
91
- end
92
-
240
+ on :fallback , respond: " fallback message"
241
+ end
242
+
243
+ ```
244
+
245
+ 在controller中使用` wechat_responder ` 引入Responder DSL, 之后可以用
246
+
247
+ ```
248
+ on <message_type> do |message|
249
+ message.reply.text "some text"
93
250
end
94
251
95
252
```
253
+ 来响应用户信息。
96
254
255
+ 目前支持的message_type有如下几种
256
+
257
+ - : text 响应文字消息,可以用` :with ` 参数来匹配文本内容 ` on(:text, with:'help'){|message, content| ...} `
258
+ - : image 响应图片消息
259
+ - : voice 响应语音消息
260
+ - : video 响应视频消息
261
+ - : location 响应地理位置消息
262
+ - : link 响应链接消息
263
+ - : event 响应事件消息, 可以用` :with ` 参数来匹配事件类型
264
+ - : fallback 默认响应,当收到的消息无法被其他responder响应时,会使用这个responder.
265
+
266
+ ## Message DSL
267
+
268
+ Wechat-rails 的核心是一个Message DSL,帮助开发者构建各种类型的消息,包括主动推送的和被动响应的。
269
+ ....
270
+
271
+
97
272
98
273
0 commit comments