wechat 0.8.3 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -1
- data/CHANGELOG.md +5 -0
- data/README.md +83 -83
- data/lib/action_controller/wechat_responder.rb +1 -3
- data/lib/wechat/api.rb +1 -1
- data/lib/wechat/cipher.rb +2 -2
- data/lib/wechat/http_client.rb +4 -1
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e0f3e13da7012b66a0db4454a02588e18b197b0
|
4
|
+
data.tar.gz: 8f20da9e9e385f3a47f07fdd74857b6be7065ac8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c248645bc11c2b0973cb2a05a0db3ba0611de7173c2077632884be5097706b7906b7ae48c81e3d5094d628b7850278dc9f64d4851ee976fb8ac4e1ce0fbc70ff
|
7
|
+
data.tar.gz: fc0758f38f48240b64a74b475802259b98df2606952b2c6fbd7addb89b8dee177243789e5acd83adf76c8e8795864905610821ff6b71e6311a3badd157dd8586
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
[�w�C�Nn��8��&�&���'����,�X�T�e��ڂ)R�r�$��/8�
|
2
|
+
�`��
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -5,9 +5,9 @@ WeChat [![Gem Version](https://badge.fury.io/rb/wechat.svg)](https://rubygems.or
|
|
5
5
|
|
6
6
|
[中文文档 Chinese document](/README-CN.md)
|
7
7
|
|
8
|
-
[Wechat](http://www.wechat.com/) is a free messaging and calling app developed by [Tencent](http://tencent.com/en-us/index.shtml), after linking billion people, Wechat had become [
|
8
|
+
[Wechat](http://www.wechat.com/) is a free messaging and calling app developed by [Tencent](http://tencent.com/en-us/index.shtml), after linking billion people, Wechat had become [an application platform](https://uxdesign.cc/wechat-the-invincible-app-a-key-to-business-success-in-china-8e9a920deb26?source=wechat_gem).
|
9
9
|
|
10
|
-
WeChat gem tries to help Rails developer to integrate [enterprise account](https://qy.weixin.qq.com) / [public account](https://mp.weixin.qq.com/) easily. Features below are ready and there is no need
|
10
|
+
WeChat gem tries to help Rails developer to integrate [enterprise account](https://qy.weixin.qq.com) / [public account](https://mp.weixin.qq.com/) easily. Features below are ready and there is no need to write adapter code for talking to wechat server directly.
|
11
11
|
|
12
12
|
- [Sending message](http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF) API(Can access via console or in rails)
|
13
13
|
- [Receiving message](http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%B6%88%E6%81%AF%E4%B8%8E%E4%BA%8B%E4%BB%B6)(You must run on rails server to receiving message)
|
@@ -18,15 +18,15 @@ WeChat gem tries to help Rails developer to integrate [enterprise account](https
|
|
18
18
|
|
19
19
|
`wechat` command shares the same API in console, so you can interactive with wechat server quickly, without starting up web environment/code.
|
20
20
|
|
21
|
-
A responder DSL can be used in Rails controller,
|
21
|
+
A responder DSL can be used in Rails controller, giving an event based interface to handle messages sent by end user from wechat server.
|
22
22
|
|
23
|
-
Wechat provides OAuth2.0 authentication method `wechat_oauth2`, possibly the easiest way, for the users who
|
23
|
+
Wechat provides OAuth2.0 authentication method `wechat_oauth2`, possibly the easiest way, for the users who prefer using devise style authorization gems, [omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2) can be a good option.
|
24
24
|
|
25
|
-
There is official [weui](https://github.com/weui/weui),
|
25
|
+
There is official [weui](https://github.com/weui/weui), corresponding Rails gem called [weui-rails](https://github.com/Eric-Guo/weui-rails) is available, if you prefer following the same UI design as wechat.
|
26
26
|
|
27
27
|
For web page only wechat application, please use [`wechat_api`](#wechat_api---rails-controller-wechat-api), which only contains web feature compare with traditional message type [`wechat_responder`](#wechat_responder---rails-responder-controller-dsl).
|
28
28
|
|
29
|
-
There is a more complete [wechat-starter](https://github.com/goofansu/wechat-starter) demo available, even
|
29
|
+
There is a more complete [wechat-starter](https://github.com/goofansu/wechat-starter) demo available, which even includes the payment SDK feature.
|
30
30
|
|
31
31
|
## Installation
|
32
32
|
|
@@ -54,7 +54,7 @@ Run the generator:
|
|
54
54
|
rails generate wechat:install
|
55
55
|
```
|
56
56
|
|
57
|
-
`rails g wechat:install` will generated the initial `wechat.yml` configuration file, including an
|
57
|
+
`rails g wechat:install` will generated the initial `wechat.yml` configuration file, including an sample wechat controller and corresponding routes.
|
58
58
|
|
59
59
|
Enable session record:
|
60
60
|
|
@@ -63,7 +63,7 @@ rails g wechat:session
|
|
63
63
|
rake db:migrate
|
64
64
|
```
|
65
65
|
|
66
|
-
|
66
|
+
Enabling session will generate two files in Rails folder, you can add more columns to *wechat_session* table and add declaration to link to users table, it's also possible to store data directly in **hash_store**. if you are using PostgreSQL, using [hstore](http://guides.rubyonrails.org/active_record_postgresql.html#hstore)/json maybe better, but the best way is still to add a dedicate column to record the data, the Rails way.
|
67
67
|
|
68
68
|
Using Redis to store wechat token and ticket:
|
69
69
|
|
@@ -71,21 +71,21 @@ Using Redis to store wechat token and ticket:
|
|
71
71
|
rails g wechat:redis_store
|
72
72
|
```
|
73
73
|
|
74
|
-
Redis store
|
74
|
+
Redis store supports Rails application running in multi-server, no need to enable it if your Rails application is running on one server only, the wechat command won't read the token/ticket stored in Redis.
|
75
75
|
|
76
76
|
## Configuration
|
77
77
|
|
78
|
-
#### Configure
|
78
|
+
#### Configure wechat for the first time
|
79
79
|
|
80
|
-
Make sure finish all the setup on rails side first, then submit to wechat. Otherwise, wechat will raise error.
|
80
|
+
Make sure to finish all the setup on rails side first, then submit to wechat. Otherwise, wechat will raise error.
|
81
81
|
|
82
|
-
URL address for wechat by running `rails g wechat:install` is `http://your-server.com/wechat`
|
82
|
+
URL address for wechat created by running `rails g wechat:install` is `http://your-server.com/wechat`
|
83
83
|
|
84
84
|
How to setup appid/corpid and secret see below section.
|
85
85
|
|
86
86
|
#### Configure for command line
|
87
87
|
|
88
|
-
To
|
88
|
+
To use `wechat` command solely, you need to create configuration file `~/.wechat.yml` and include content below for public account. The access_token will be written to a file.
|
89
89
|
|
90
90
|
```
|
91
91
|
appid: "my_appid"
|
@@ -93,22 +93,22 @@ secret: "my_secret"
|
|
93
93
|
access_token: "/var/tmp/wechat_access_token"
|
94
94
|
```
|
95
95
|
|
96
|
-
For enterprise account, need
|
96
|
+
For enterprise account, you need to use `corpid` instead of `appid` as enterprise account supports multiply application (Tencent calls them agents) in one enterprise account. Obtaining the `corpsecret` is a little bit tricky, must be created at management mode->privilege setting and create any of management group to obtain. Due to Tencent currently only providing Chinese interface for their management console, it's highly recommended you find a colleague knowing Mandarin to help you to obtain the `corpsecret`.
|
97
97
|
|
98
|
-
Windows
|
98
|
+
Windows users need to store `.wechat.yml` at `C:/Users/[user_name]/` (replace with your user name), also pay attention to the direction of folder separator.
|
99
99
|
|
100
100
|
```
|
101
101
|
corpid: "my_appid"
|
102
102
|
corpsecret: "my_secret"
|
103
|
-
agentid: 1 # Integer, which can be
|
103
|
+
agentid: 1 # Integer, which can be obtained from application settings
|
104
104
|
access_token: "C:/Users/[user_name]/wechat_access_token"
|
105
105
|
```
|
106
106
|
|
107
107
|
#### Configure for Rails
|
108
108
|
|
109
|
-
Rails configuration
|
109
|
+
Rails configuration file supports different environment similar to database.yml, after running `rails generate wechat:install` you can find configuration file at `config/wechat.yml`
|
110
110
|
|
111
|
-
Public account
|
111
|
+
Public account configuration example:
|
112
112
|
|
113
113
|
```
|
114
114
|
default: &default
|
@@ -134,7 +134,7 @@ test:
|
|
134
134
|
<<: *default
|
135
135
|
```
|
136
136
|
|
137
|
-
Although it's optional for public account, but highly
|
137
|
+
Although it's optional for public account, but highly recommended to enable encrypt mode by adding these two items to `wechat.yml`
|
138
138
|
|
139
139
|
|
140
140
|
```
|
@@ -143,9 +143,9 @@ default: &default
|
|
143
143
|
encoding_aes_key: "my_encoding_aes_key"
|
144
144
|
```
|
145
145
|
|
146
|
-
Enterprise account must
|
146
|
+
Enterprise account must use encrypt mode (`encrypt_mode: true` is on by default, no need to configure).
|
147
147
|
|
148
|
-
The `token` and `encoding_aes_key` can be
|
148
|
+
The `token` and `encoding_aes_key` can be obtained from management console -> one of the agent application -> Mode selection, select callback mode and get/set.
|
149
149
|
|
150
150
|
```
|
151
151
|
default: &default
|
@@ -196,23 +196,23 @@ test:
|
|
196
196
|
# secret: "my_secret"
|
197
197
|
```
|
198
198
|
|
199
|
-
|
199
|
+
For multiple accounts details reference [PR 150](https://github.com/Eric-Guo/wechat/pull/150)
|
200
200
|
|
201
201
|
##### Configure priority
|
202
202
|
|
203
|
-
Running `wechat` command in the root folder of Rails application will using the Rails configuration first (`default` section), if can not find it, will relay on `~\.wechat.yml`, such behavior
|
203
|
+
Running `wechat` command in the root folder of Rails application will be using the Rails configuration first (`default` section), if can not find it, will relay on `~\.wechat.yml`, such behavior enables managing more wechat public account and enterprise account without changing your home `~\.wechat.yml` file.
|
204
204
|
|
205
205
|
##### Wechat server timeout setting
|
206
206
|
|
207
|
-
Stability
|
207
|
+
Stability varies for Tencent wechat server, so setting a long timeout may be needed, default is 20 seconds if not set.
|
208
208
|
|
209
209
|
##### Skip the SSL verification
|
210
210
|
|
211
|
-
SSL Certification can also be corrupted
|
211
|
+
SSL Certification can also be corrupted for some reason in China, [it's reported](http://qydev.weixin.qq.com/qa/index.php?qa=11037) and if it happens to you, you can set `skip_verify_ssl: true`. (not recommend)
|
212
212
|
|
213
213
|
#### Configure individual responder with different appid
|
214
214
|
|
215
|
-
|
215
|
+
Sometimes, you may want to host more than one enterprise/public wechat account in one Rails application, so you can provide this configuration info when calling `wechat_responder` or `wechat_api`
|
216
216
|
|
217
217
|
```ruby
|
218
218
|
class WechatFirstController < ActionController::Base
|
@@ -221,7 +221,7 @@ class WechatFirstController < ActionController::Base
|
|
221
221
|
on :text, with:"help", respond: "help content"
|
222
222
|
end
|
223
223
|
|
224
|
-
Or you can
|
224
|
+
Or you can provide full list of options.
|
225
225
|
|
226
226
|
```ruby
|
227
227
|
class WechatFirstController < ActionController::Base
|
@@ -233,9 +233,9 @@ end
|
|
233
233
|
|
234
234
|
#### JS-SDK helper
|
235
235
|
|
236
|
-
JS-SDK
|
236
|
+
JS-SDK gives you control over Wechat App behavior in html, by injecting a config signature, helper `wechat_config_js` does that in a simple way:
|
237
237
|
|
238
|
-
To make wechat_config_js
|
238
|
+
To make wechat_config_js work, you need to put [`wechat_api`](#wechat_api---rails-controller-wechat-api) or [`wechat_responder`](#wechat_responder---rails-responder-controller-dsl) at controller first.
|
239
239
|
|
240
240
|
```erb
|
241
241
|
<body>
|
@@ -249,11 +249,11 @@ To make wechat_config_js works, you need put [`wechat_api`](#wechat_api---rails-
|
|
249
249
|
</body>
|
250
250
|
```
|
251
251
|
|
252
|
-
Configure the `trusted_domain_fullname` if you are in development mode and app running behind a reverse proxy server, otherwise wechat gem
|
252
|
+
Configure the `trusted_domain_fullname` if you are in development mode and app is running behind a reverse proxy server, otherwise wechat gem won't be able to get the correct url to be signed later.
|
253
253
|
|
254
254
|
#### OAuth2.0 authentication
|
255
255
|
|
256
|
-
For public account, below
|
256
|
+
For public account, code below will get following user's info.
|
257
257
|
|
258
258
|
```ruby
|
259
259
|
class CartController < ActionController::Base
|
@@ -267,7 +267,7 @@ class CartController < ActionController::Base
|
|
267
267
|
end
|
268
268
|
```
|
269
269
|
|
270
|
-
For enterprise account, below
|
270
|
+
For enterprise account, code below will get enterprise member's userinfo.
|
271
271
|
|
272
272
|
```ruby
|
273
273
|
class WechatsController < ActionController::Base
|
@@ -283,22 +283,22 @@ class WechatsController < ActionController::Base
|
|
283
283
|
end
|
284
284
|
```
|
285
285
|
|
286
|
-
`wechat_oauth2` already
|
286
|
+
`wechat_oauth2` already implements the necessary OAuth2.0 and cookie logic. userid defined as the enterprise member UserID. openid defined as the user who following the public account, also notice openid will be different for the same user for different following public accounts.
|
287
287
|
|
288
288
|
Notice:
|
289
|
-
* If you use `wechat_responder` in your controller, you cannot use `create` and `show` action in your controller, otherwise will throw errors.
|
290
|
-
* If you get *redirect_uri parameter error* message, make sure you set the correct callback
|
289
|
+
* If you use `wechat_responder` in your controller, you cannot use `create` and `show` action in your controller, otherwise it will throw errors.
|
290
|
+
* If you get *redirect_uri parameter error* message, make sure you set the correct callback url value in wechat management console with path *Development center / Webpage service / Webpage authorization for retrieving user basic information*.
|
291
291
|
|
292
292
|
|
293
293
|
## The API privilege
|
294
294
|
|
295
|
-
wechat gems won't handle any privilege
|
295
|
+
wechat gems won't handle any privilege exceptions. (except token timeout, but it's not important to you as it's auto retry/recovery in gems internally), but Tencent will control a lot of privilege based on your public account type and certification, for more info please reference [official document](http://mp.weixin.qq.com/wiki/7/2d301d4b757dedc333b9a9854b457b47.html).
|
296
296
|
|
297
297
|
## Command line mode
|
298
298
|
|
299
299
|
The available API is different between public account and enterprise account, so wechat gems provide different set of command.
|
300
300
|
|
301
|
-
Feel safe if you can not read Chinese in the comments, it's
|
301
|
+
Feel safe if you can not read Chinese in the comments, it's kept there in order to copy & find in the official documentation easier.
|
302
302
|
|
303
303
|
#### Public account command line
|
304
304
|
|
@@ -440,7 +440,7 @@ $ wechat menu
|
|
440
440
|
|
441
441
|
##### Menu create
|
442
442
|
|
443
|
-
Running command `rails g wechat:menu` to generate a menu
|
443
|
+
Running command `rails g wechat:menu` to generate a menu definition yaml file:
|
444
444
|
|
445
445
|
```
|
446
446
|
button:
|
@@ -476,18 +476,18 @@ button:
|
|
476
476
|
url: "http://blog.cloud-mes.com/"
|
477
477
|
```
|
478
478
|
|
479
|
-
Running below
|
479
|
+
Running command below to upload the menu:
|
480
480
|
|
481
481
|
```
|
482
482
|
$ wechat menu_create menu.yaml
|
483
483
|
```
|
484
484
|
|
485
|
-
Caution: make sure you
|
485
|
+
Caution: make sure you have management privilege for this application, otherwise you will get [60011](http://qydev.weixin.qq.com/wiki/index.php?title=%E5%85%A8%E5%B1%80%E8%BF%94%E5%9B%9E%E7%A0%81%E8%AF%B4%E6%98%8E) error.
|
486
486
|
|
487
|
-
#####
|
487
|
+
##### Send custom news
|
488
488
|
|
489
489
|
|
490
|
-
Sending custom_news should also defined as a yaml file, like `articles.yml`
|
490
|
+
Sending custom_news should also be defined as a yaml file, like `articles.yml`
|
491
491
|
|
492
492
|
```
|
493
493
|
articles:
|
@@ -498,16 +498,16 @@ articles:
|
|
498
498
|
pic_url: "http://i3.sinaimg.cn/dy/c/2014-04-01/1396366518_bYays1.jpg"
|
499
499
|
```
|
500
500
|
|
501
|
-
After that, can
|
501
|
+
After that, you can run this command:
|
502
502
|
|
503
503
|
```
|
504
504
|
$ wechat custom_news oCfEht9oM*********** articles.yml
|
505
505
|
|
506
506
|
```
|
507
507
|
|
508
|
-
#####
|
508
|
+
##### Send template message
|
509
509
|
|
510
|
-
Sending template message via yaml file similar too,
|
510
|
+
Sending template message via yaml file is similar, too, define `template.yml` and content is just the template content.
|
511
511
|
|
512
512
|
```
|
513
513
|
template:
|
@@ -516,7 +516,7 @@ template:
|
|
516
516
|
topcolor: "#FF0000"
|
517
517
|
data:
|
518
518
|
first:
|
519
|
-
value: "Hello, you successfully
|
519
|
+
value: "Hello, you successfully registered"
|
520
520
|
color: "#0A0A0A"
|
521
521
|
keynote1:
|
522
522
|
value: "5km Health Running"
|
@@ -533,7 +533,7 @@ template:
|
|
533
533
|
|
534
534
|
```
|
535
535
|
|
536
|
-
After that, can
|
536
|
+
After that, you can run this command:
|
537
537
|
|
538
538
|
```
|
539
539
|
$ wechat template_message oCfEht9oM*********** template.yml
|
@@ -546,7 +546,7 @@ template = YAML.load(File.read(template_yaml_path))
|
|
546
546
|
Wechat.api.template_message_send Wechat::Message.to(openid).template(template["template"])
|
547
547
|
```
|
548
548
|
|
549
|
-
If using wechat_api or wechat_responder in
|
549
|
+
If using wechat_api or wechat_responder in controller, can also use wechat as shortcut (supports multi account):
|
550
550
|
|
551
551
|
```ruby
|
552
552
|
template = YAML.load(File.read(template_yaml_path))
|
@@ -555,7 +555,7 @@ wechat.template_message_send Wechat::Message.to(openid).template(template["templ
|
|
555
555
|
|
556
556
|
## wechat_api - Rails Controller Wechat API
|
557
557
|
|
558
|
-
Although user can always access all wechat
|
558
|
+
Although user can always access all wechat features via Wechat.api, but it's highly recommended to use `wechat` directly in the controller. It's not only mandatory required if you plan to support multi-account, it also helps to separate the wechat specific logic from the model layer.
|
559
559
|
|
560
560
|
```ruby
|
561
561
|
class WechatReportsController < ApplicationController
|
@@ -570,13 +570,13 @@ end
|
|
570
570
|
|
571
571
|
## wechat_responder - Rails Responder Controller DSL
|
572
572
|
|
573
|
-
In order to
|
573
|
+
In order to respond to the message user sent, Rails developer needs to create a wechat responder controller and define the routing in `routes.rb`
|
574
574
|
|
575
575
|
```ruby
|
576
|
-
resource :wechat, only:[:show, :create]
|
576
|
+
resource :wechat, only: [:show, :create]
|
577
577
|
```
|
578
578
|
|
579
|
-
So the ActionController should defined like below:
|
579
|
+
So the ActionController should be defined like below:
|
580
580
|
|
581
581
|
```ruby
|
582
582
|
class WechatsController < ActionController::Base
|
@@ -592,9 +592,9 @@ class WechatsController < ActionController::Base
|
|
592
592
|
request.reply.text 'help content'
|
593
593
|
end
|
594
594
|
|
595
|
-
# When receive '<n>news', will match and will
|
595
|
+
# When receive '<n>news', will match and will get count as <n> as parameter
|
596
596
|
on :text, with: /^(\d+) news$/ do |request, count|
|
597
|
-
# Wechat article can only contain max 10 items, large than 10 will dropped.
|
597
|
+
# Wechat article can only contain max 10 items, large than 10 will be dropped.
|
598
598
|
news = (1..count.to_i).each_with_object([]) { |n, memo| memo << { title: 'News title', content: "No. #{n} news content" } }
|
599
599
|
request.reply.news(news) do |article, n, index| # article is return object
|
600
600
|
article.item title: "#{index} #{n[:title]}", description: n[:content], pic_url: 'http://www.baidu.com/img/bdlogo.gif', url: 'http://www.baidu.com/'
|
@@ -606,7 +606,7 @@ class WechatsController < ActionController::Base
|
|
606
606
|
end
|
607
607
|
|
608
608
|
# When unsubscribe user scan qrcode qrscene_xxxxxx to subscribe in public account
|
609
|
-
# notice user will subscribe public account at same time, so wechat won't trigger subscribe event
|
609
|
+
# notice user will subscribe public account at the same time, so wechat won't trigger subscribe event anymore
|
610
610
|
on :scan, with: 'qrscene_xxxxxx' do |request, ticket|
|
611
611
|
request.reply.text "Unsubscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
612
612
|
end
|
@@ -616,7 +616,7 @@ class WechatsController < ActionController::Base
|
|
616
616
|
request.reply.text "Subscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
617
617
|
end
|
618
618
|
|
619
|
-
# When no any on :scan responder can match subscribe user
|
619
|
+
# When no any on :scan responder can match subscribe user scanned scene_id
|
620
620
|
on :event, with: 'scan' do |request|
|
621
621
|
if request[:EventKey].present?
|
622
622
|
request.reply.text "event scan got EventKey #{request[:EventKey]} Ticket #{request[:Ticket]}"
|
@@ -635,38 +635,38 @@ class WechatsController < ActionController::Base
|
|
635
635
|
end
|
636
636
|
end
|
637
637
|
|
638
|
-
# When user
|
638
|
+
# When user clicks the menu button
|
639
639
|
on :click, with: 'BOOK_LUNCH' do |request, key|
|
640
640
|
request.reply.text "User: #{request[:FromUserName]} click #{key}"
|
641
641
|
end
|
642
642
|
|
643
|
-
# When user
|
643
|
+
# When user views URL in the menu button
|
644
644
|
on :view, with: 'http://wechat.somewhere.com/view_url' do |request, view|
|
645
645
|
request.reply.text "#{request[:FromUserName]} view #{view}"
|
646
646
|
end
|
647
647
|
|
648
|
-
# When user
|
648
|
+
# When user sends an image
|
649
649
|
on :image do |request|
|
650
650
|
request.reply.image(request[:MediaId]) # Echo the sent image to user
|
651
651
|
end
|
652
652
|
|
653
|
-
# When user
|
653
|
+
# When user sends a voice
|
654
654
|
on :voice do |request|
|
655
655
|
request.reply.voice(request[:MediaId]) # Echo the sent voice to user
|
656
656
|
end
|
657
657
|
|
658
|
-
# When user
|
658
|
+
# When user sends a video
|
659
659
|
on :video do |request|
|
660
660
|
nickname = wechat.user(request[:FromUserName])['nickname'] # Call wechat api to get sender nickname
|
661
661
|
request.reply.video(request[:MediaId], title: 'Echo', description: "Got #{nickname} sent video") # Echo the sent video to user
|
662
662
|
end
|
663
663
|
|
664
|
-
# When user
|
664
|
+
# When user sends location message with label
|
665
665
|
on :label_location do |request|
|
666
666
|
request.reply.text("Label: #{request[:Label]} Location_X: #{request[:Location_X]} Location_Y: #{request[:Location_Y]} Scale: #{request[:Scale]}")
|
667
667
|
end
|
668
668
|
|
669
|
-
# When user
|
669
|
+
# When user sends location
|
670
670
|
on :location do |request|
|
671
671
|
request.reply.text("Latitude: #{request[:Latitude]} Longitude: #{request[:Longitude]} Precision: #{request[:Precision]}")
|
672
672
|
end
|
@@ -675,37 +675,37 @@ class WechatsController < ActionController::Base
|
|
675
675
|
request.reply.success # user can not receive this message
|
676
676
|
end
|
677
677
|
|
678
|
-
# When user
|
678
|
+
# When user enters the app / agent app
|
679
679
|
on :event, with: 'enter_agent' do |request|
|
680
680
|
request.reply.text "#{request[:FromUserName]} enter agent app now"
|
681
681
|
end
|
682
682
|
|
683
|
-
# When batch job create/update user (incremental) finished.
|
683
|
+
# When batch job "create/update user (incremental)" is finished.
|
684
684
|
on :batch_job, with: 'sync_user' do |request, batch_job|
|
685
685
|
request.reply.text "sync_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
686
686
|
end
|
687
687
|
|
688
|
-
# When batch job replace user (full sync) finished.
|
688
|
+
# When batch job "replace user (full sync)" is finished.
|
689
689
|
on :batch_job, with: 'replace_user' do |request, batch_job|
|
690
690
|
request.reply.text "replace_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
691
691
|
end
|
692
692
|
|
693
|
-
# When batch job
|
693
|
+
# When batch job "invite user" is finished.
|
694
694
|
on :batch_job, with: 'invite_user' do |request, batch_job|
|
695
695
|
request.reply.text "invite_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
696
696
|
end
|
697
697
|
|
698
|
-
# When batch job replace department (full sync) finished.
|
698
|
+
# When batch job "replace department (full sync)" is finished.
|
699
699
|
on :batch_job, with: 'replace_party' do |request, batch_job|
|
700
700
|
request.reply.text "replace_party job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
701
701
|
end
|
702
702
|
|
703
|
-
#
|
703
|
+
# If no match above will fallback to below
|
704
704
|
on :fallback, respond: 'fallback message'
|
705
705
|
end
|
706
706
|
```
|
707
707
|
|
708
|
-
So the
|
708
|
+
So the important statement is only `wechat_responder`, all other is just a DSL:
|
709
709
|
|
710
710
|
```
|
711
711
|
on <message_type> do |message|
|
@@ -713,10 +713,10 @@ on <message_type> do |message|
|
|
713
713
|
end
|
714
714
|
```
|
715
715
|
|
716
|
-
The block code will be running to
|
716
|
+
The block code will be running to respond to user's message.
|
717
717
|
|
718
718
|
|
719
|
-
Below
|
719
|
+
Below are currently supported message_types:
|
720
720
|
|
721
721
|
- :text text message, using `:with` to match text content like `on(:text, with:'help'){|message, content| ...}`
|
722
722
|
- :image image message
|
@@ -725,13 +725,13 @@ Below is current supported message_type:
|
|
725
725
|
- :video video message
|
726
726
|
- :label_location location message with label
|
727
727
|
- :link link message
|
728
|
-
- :event event message, using `:with` to match particular event,
|
729
|
-
- :click virtual event message, wechat still
|
730
|
-
- :view virtual view message, wechat still
|
731
|
-
- :scan virtual scan message, wechat still
|
728
|
+
- :event event message, using `:with` to match particular event, supports regular expression match similar to text message.
|
729
|
+
- :click virtual event message, wechat still sends event message,but gems will map to menu click event.
|
730
|
+
- :view virtual view message, wechat still sends event message,but gems will map to menu view page event.
|
731
|
+
- :scan virtual scan message, wechat still sends event message, but gems will map to scan event.
|
732
732
|
- :batch_job virtual batch job message
|
733
733
|
- :location virtual location message
|
734
|
-
- :fallback default message, when no other responder can handle incoming
|
734
|
+
- :fallback default message, when no other responder can handle incoming message, will be used as a fallback handler
|
735
735
|
|
736
736
|
### Transfer to customer service
|
737
737
|
|
@@ -744,11 +744,11 @@ class WechatsController < ActionController::Base
|
|
744
744
|
end
|
745
745
|
```
|
746
746
|
|
747
|
-
Caution: do not
|
747
|
+
Caution: do not set default text responder if you want to use [multiply human customer service](http://dkf.qq.com/), other will lead text message can not transfer.
|
748
748
|
|
749
749
|
### Notifications
|
750
750
|
|
751
|
-
* `wechat.responder.after_create` data
|
751
|
+
* `wechat.responder.after_create` data includes request <Wechat::Message> and response <Wechat::Message>.
|
752
752
|
|
753
753
|
Example:
|
754
754
|
|
@@ -758,8 +758,8 @@ ActiveSupport::Notifications.subscribe('wechat.responder.after_create') do |name
|
|
758
758
|
end
|
759
759
|
```
|
760
760
|
|
761
|
-
## Known
|
761
|
+
## Known Issues
|
762
762
|
|
763
|
-
*
|
764
|
-
* Enterprise batch replace users
|
765
|
-
* If you using unicorn behind nginx and https, you need
|
763
|
+
* Sometimes, enterprise account can not receive the menu message due to Tencent server unable to resolve DNS, so using IP as a callback URL is more stable, but it never happens for user sent text messages.
|
764
|
+
* Enterprise batch "replace users" uses a CSV format file, but if you are using the downloaded template directly, it's [not working](http://qydev.weixin.qq.com/qa/index.php?qa=13978), must open the CSV file in Excel first, then save as CSV format again, seems Tencent only supports Excel "Save as CSV" file format.
|
765
|
+
* If you using unicorn behind nginx and https, you need to set `trusted_domain_fullname` and point it to https, otherwise it will be http and will lead to invalid signature in the JS-SDK.
|
data/lib/wechat/api.rb
CHANGED
data/lib/wechat/cipher.rb
CHANGED
@@ -15,7 +15,7 @@ module Wechat
|
|
15
15
|
cipher.padding = 0
|
16
16
|
key_data = Base64.decode64(encoding_aes_key + '=')
|
17
17
|
cipher.key = key_data
|
18
|
-
cipher.iv = key_data
|
18
|
+
cipher.iv = [key_data].pack('H*')
|
19
19
|
|
20
20
|
cipher.update(plain) + cipher.final
|
21
21
|
end
|
@@ -27,7 +27,7 @@ module Wechat
|
|
27
27
|
cipher.padding = 0
|
28
28
|
key_data = Base64.decode64(encoding_aes_key + '=')
|
29
29
|
cipher.key = key_data
|
30
|
-
cipher.iv = key_data
|
30
|
+
cipher.iv = [key_data].pack('H*')
|
31
31
|
|
32
32
|
plain = cipher.update(msg) + cipher.final
|
33
33
|
decode_padding(plain)
|
data/lib/wechat/http_client.rb
CHANGED
@@ -68,7 +68,8 @@ module Wechat
|
|
68
68
|
content_type = response.headers[:content_type]
|
69
69
|
parse_as = {
|
70
70
|
%r{^application\/json} => :json,
|
71
|
-
%r{^image\/.*}
|
71
|
+
%r{^image\/.*} => :file,
|
72
|
+
%r{^text\/html} => :xml
|
72
73
|
}.each_with_object([]) { |match, memo| memo << match[1] if content_type =~ match[0] }.first || as || :text
|
73
74
|
|
74
75
|
case parse_as
|
@@ -81,6 +82,8 @@ module Wechat
|
|
81
82
|
|
82
83
|
when :json
|
83
84
|
data = JSON.parse response.body.to_s.gsub(/[\u0000-\u001f]+/, '')
|
85
|
+
when :xml
|
86
|
+
data = Hash.from_xml(response.body.to_s)
|
84
87
|
else
|
85
88
|
data = response.body
|
86
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wechat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Skinnyworm
|
@@ -31,7 +31,7 @@ cert_chain:
|
|
31
31
|
R5k6Ma92sW8jupX4cqbSu9rntdVQkNRpoHIrfU0MZT0cKsg/D1zMteylxrO3KMsz
|
32
32
|
SPQRv+nrI1J0zevFqb8010heoR8SDyUA0Mm3+Q==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date:
|
34
|
+
date: 2017-01-12 00:00:00.000000000 Z
|
35
35
|
dependencies:
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activesupport
|
metadata.gz.sig
CHANGED
Binary file
|