wx_ext 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/wx_ext/api/base.rb +26 -17
- data/lib/wx_ext/api/customer_service.rb +33 -0
- data/lib/wx_ext/api/js.rb +21 -4
- data/lib/wx_ext/api/mass.rb +81 -0
- data/lib/wx_ext/api/menu.rb +19 -3
- data/lib/wx_ext/api/msg.rb +37 -8
- data/lib/wx_ext/api/qrcode.rb +23 -0
- data/lib/wx_ext/api/semantic.rb +27 -0
- data/lib/wx_ext/api/template_msg.rb +50 -0
- data/lib/wx_ext/api/user/group.rb +38 -8
- data/lib/wx_ext/api/user.rb +63 -10
- data/lib/wx_ext/api.rb +6 -5
- data/lib/wx_ext/helper.rb +13 -1
- data/lib/wx_ext/sougou_weixin.rb +16 -4
- data/lib/wx_ext/version.rb +1 -1
- data/lib/wx_ext/wei_xin.rb +117 -67
- data/lib/wx_ext.rb +14 -1
- data/spec/wx_ext/api/customer_service_spec.rb +5 -0
- data/spec/wx_ext/api/mass_spec.rb +5 -0
- data/spec/wx_ext/api/semantic_spec.rb +0 -0
- data/spec/wx_ext/api/template_msg_spec.rb +0 -0
- metadata +14 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 203fdbfdf2af62627187c6032990c5197801c986
|
4
|
+
data.tar.gz: d5b91a0de4da6424b4a7eb3dce028b3ddcc83b2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5321d5ffd737870b445b26493bbfe6c64c440d41a05b9af6224d99264896993fcc861ff6aa191942a9428a910421e5ed5ac0b7955cc93e4a18fb20235a93f00f
|
7
|
+
data.tar.gz: 0185ceab9c0dc273315275f3a88d9c596698e3c274ce55066b85f27f78b0c0447f58ed0e594e70034a72aa11e0e7ed4a6321af85525b5d545fadb9c1fcf23e1c
|
data/README.md
CHANGED
@@ -24,6 +24,10 @@ And then execute:
|
|
24
24
|
2. rspec spec/wx_ext/weixin_spec.rb
|
25
25
|
3. rspec spec/wx_ext/sougou_weixin_spec.rb
|
26
26
|
|
27
|
+
## yard
|
28
|
+
|
29
|
+
`yardoc lib/*.rb lib/wx_ext/*.rb lib/wx_ext/api/*.rb lib/wx_ext/api/user/*.rb`
|
30
|
+
|
27
31
|
## Contributing
|
28
32
|
|
29
33
|
1. Fork it ( https://github.com/FlowerWrong/wx_ext/fork )
|
data/lib/wx_ext/api/base.rb
CHANGED
@@ -1,37 +1,42 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require 'digest'
|
4
3
|
require 'rest_client'
|
5
|
-
require 'json'
|
6
|
-
require 'wx_ext/helper'
|
7
4
|
|
8
5
|
module WxExt
|
6
|
+
# Weixin api
|
7
|
+
#
|
8
|
+
# @author FuShengYang
|
9
9
|
module Api
|
10
|
-
|
11
|
-
#
|
10
|
+
# User api of weixin.
|
11
|
+
#
|
12
12
|
# @author FuShengYang
|
13
13
|
module Base
|
14
14
|
class << self
|
15
|
-
# Get the access_token for other apis.
|
16
|
-
# http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html
|
15
|
+
# Get the access_token for other apis via get.
|
17
16
|
#
|
18
|
-
# @param [String] appid
|
19
|
-
# @param [String] secret
|
20
|
-
# @param [String] grant_type
|
21
|
-
# @return [Hash]
|
17
|
+
# @param [Enumerable<String>] appid
|
18
|
+
# @param [Enumerable<String>] secret
|
19
|
+
# @param [Enumerable<String>] grant_type
|
20
|
+
# @return [Hash] "access_token":"ACCESS_TOKEN","expires_in":7200
|
22
21
|
def get_access_token(appid, secret, grant_type = 'client_credential')
|
23
22
|
url = 'https://api.weixin.qq.com/cgi-bin/token'\
|
24
23
|
"?grant_type=#{grant_type}&appid=#{appid}&secret=#{secret}"
|
25
24
|
Helper.http_get(url, { accept: :json })
|
26
25
|
end
|
27
26
|
|
28
|
-
#
|
27
|
+
# Get weixin server ips.
|
28
|
+
#
|
29
|
+
# @param [Enumerable<String>] access_token
|
30
|
+
# @return [Hash] A json parse hash.
|
29
31
|
def get_weixin_ips(access_token)
|
30
32
|
url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip'\
|
31
33
|
"?access_token=#{access_token}"
|
32
34
|
Helper.http_get(url, { accept: :json })
|
33
35
|
end
|
34
36
|
|
37
|
+
# Get weixin error_code and msg map.
|
38
|
+
#
|
39
|
+
# @return [Hash] A json parse hash.
|
35
40
|
def code_msg
|
36
41
|
{
|
37
42
|
-1 => '系统繁忙,此时请开发者稍候再试',
|
@@ -130,18 +135,22 @@ module WxExt
|
|
130
135
|
end
|
131
136
|
|
132
137
|
# Upload media to weixin.
|
133
|
-
# http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html
|
134
138
|
#
|
135
|
-
# @param [String] access_token
|
136
|
-
# @param [String] type
|
137
|
-
# @param [
|
138
|
-
# @return [Hash]
|
139
|
+
# @param [Enumerable<String>] access_token
|
140
|
+
# @param [Enumerable<String>] type 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
141
|
+
# @param [File] media form-data中媒体文件标识,有filename、filelength、content-type等信息
|
142
|
+
# @return [Hash] "type":"TYPE","media_id":"MEDIA_ID","created_at":123456789
|
139
143
|
def upload_media(access_token, type, media)
|
140
144
|
url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload'\
|
141
145
|
"?access_token=#{access_token}&type=#{type}"
|
142
146
|
Helper.http_post(url, {file: media})
|
143
147
|
end
|
144
148
|
|
149
|
+
# Download media from weixin.
|
150
|
+
#
|
151
|
+
# @param [Enumerable<String>] access_token
|
152
|
+
# @param [Enumerable<String>] media_id
|
153
|
+
# @return [Image] Download img.
|
145
154
|
def download_media(access_token, media_id)
|
146
155
|
url = 'http://file.api.weixin.qq.com/cgi-bin/media/get'\
|
147
156
|
"?access_token=#{access_token}&media_id=#{media_id}"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
7
|
+
module Api
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
10
|
+
# @author FuShengYang
|
11
|
+
module CustomerService
|
12
|
+
class << self
|
13
|
+
# Reply msg via post.
|
14
|
+
#
|
15
|
+
# @param [Enumerable<String>] access_token
|
16
|
+
# @param [Enumerable<String>] to_user_openid
|
17
|
+
# @param [Enumerable<String>] msg_type
|
18
|
+
# @param [Hash] msg_hash
|
19
|
+
# @return [Hash] Json based hash.
|
20
|
+
def reply_msg(access_token, to_user_openid, msg_type, msg_hash)
|
21
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send'\
|
22
|
+
"?access_token=#{access_token}"
|
23
|
+
msg_hash = {
|
24
|
+
:touser => to_user_openid,
|
25
|
+
:msgtype => msg_type,
|
26
|
+
"#{msg_type}".to_sym => msg_hash
|
27
|
+
}
|
28
|
+
Helper.http_post(url, msg_hash.to_json)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/wx_ext/api/js.rb
CHANGED
@@ -1,21 +1,32 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
require 'digest'
|
4
|
-
require 'rest_client'
|
5
|
-
require 'json'
|
6
4
|
|
7
5
|
module WxExt
|
6
|
+
# Weixin api
|
7
|
+
#
|
8
|
+
# @author FuShengYang
|
8
9
|
module Api
|
9
|
-
|
10
|
-
#
|
10
|
+
# User api of weixin.
|
11
|
+
#
|
11
12
|
# @author FuShengYang
|
12
13
|
module Js
|
13
14
|
class << self
|
15
|
+
# Get js api ticket.
|
16
|
+
#
|
17
|
+
# @param [Enumerable<String>] access_token
|
18
|
+
# @return [Hash] Json based hash.
|
14
19
|
def get_jsapi_ticket(access_token)
|
15
20
|
url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi"
|
16
21
|
Helper.http_get(url, { accept: :json })
|
17
22
|
end
|
18
23
|
|
24
|
+
# Get js api config hash.
|
25
|
+
#
|
26
|
+
# @param [Enumerable<String>] access_token
|
27
|
+
# @param [Enumerable<String>] url
|
28
|
+
# @param [Enumerable<String>] app_id
|
29
|
+
# @return [Hash] Json based hash.
|
19
30
|
def get_jsapi_config(access_token, url, app_id)
|
20
31
|
config_hash = {}
|
21
32
|
jsapi_ticket_hash = get_jsapi_ticket(access_token)
|
@@ -37,10 +48,16 @@ module WxExt
|
|
37
48
|
|
38
49
|
private
|
39
50
|
|
51
|
+
# Set the noncestr of 16 byte.
|
52
|
+
#
|
53
|
+
# @return [String] 16 byte str.
|
40
54
|
def set_noncestr
|
41
55
|
[*'a'..'z',*'0'..'9',*'A'..'Z'].sample(16).join
|
42
56
|
end
|
43
57
|
|
58
|
+
# Set the timestamp.
|
59
|
+
#
|
60
|
+
# @return [String] timestamp str.
|
44
61
|
def set_timestamp
|
45
62
|
Time.now.to_i.to_s
|
46
63
|
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
7
|
+
module Api
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
10
|
+
# @author FuShengYang
|
11
|
+
module Mass
|
12
|
+
class << self
|
13
|
+
# Upload news to weixin server via post.
|
14
|
+
#
|
15
|
+
# @param [Enumerable<String>] access_token
|
16
|
+
# @param [Hash] news_hash
|
17
|
+
# @return [Hash] Json based hash.
|
18
|
+
def upload_news(access_token, news_hash)
|
19
|
+
url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews'\
|
20
|
+
"?access_token=#{access_token}"
|
21
|
+
Helper.http_post(url, news_hash.to_json)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Mass to users by filter group via post.
|
25
|
+
#
|
26
|
+
# @param [Enumerable<String>] access_token
|
27
|
+
# @param [Hash] filter_hash
|
28
|
+
# @return [Hash] Json based hash.
|
29
|
+
def mass_by_filter_group(access_token, filter_hash)
|
30
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall'\
|
31
|
+
"?access_token=#{access_token}"
|
32
|
+
Helper.http_post(url, filter_hash.to_json)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Mass to users by openid via post.
|
36
|
+
#
|
37
|
+
# @param [Enumerable<String>] access_token
|
38
|
+
# @param [Hash] openid_hash
|
39
|
+
# @return [Hash] Json based hash.
|
40
|
+
def mass_by_openid(access_token, openid_hash)
|
41
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send'\
|
42
|
+
"?access_token=#{access_token}"
|
43
|
+
Helper.http_post(url, openid_hash.to_json)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Del mass via post.
|
47
|
+
#
|
48
|
+
# @param [Enumerable<String>] access_token
|
49
|
+
# @param [String] msg_id
|
50
|
+
# @return [Hash] Json based hash.
|
51
|
+
def del_mass(access_token, msg_id)
|
52
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete'\
|
53
|
+
"?access_token=#{access_token}"
|
54
|
+
Helper.http_post(url, { msg_id: msg_id }.to_json)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Preview mass via post.
|
58
|
+
#
|
59
|
+
# @param [Enumerable<String>] access_token
|
60
|
+
# @param [Hash] preview_hash
|
61
|
+
# @return [Hash] Json based hash.
|
62
|
+
def preview_mass(access_token, preview_hash)
|
63
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview'\
|
64
|
+
"?access_token=#{access_token}"
|
65
|
+
Helper.http_post(url, preview_hash.to_json)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Get mass status via post.
|
69
|
+
#
|
70
|
+
# @param [Enumerable<String>] access_token
|
71
|
+
# @param [String] msg_id
|
72
|
+
# @return [Hash] Json based hash.
|
73
|
+
def get_mass_status(access_token, msg_id)
|
74
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/mass/get'\
|
75
|
+
"?access_token=#{access_token}"
|
76
|
+
Helper.http_post(url, { msg_id: msg_id }.to_json)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/wx_ext/api/menu.rb
CHANGED
@@ -1,25 +1,41 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
4
7
|
module Api
|
5
|
-
|
6
|
-
#
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
7
10
|
# @author FuShengYang
|
8
11
|
module Menu
|
9
12
|
class << self
|
10
13
|
|
14
|
+
# Create menu via post.
|
15
|
+
#
|
16
|
+
# @param [Enumerable<String>] access_token
|
17
|
+
# @param [Hash] menu_hash
|
18
|
+
# @return [Hash] Json based hash.
|
11
19
|
def create_menu(access_token, menu_hash)
|
12
20
|
url = 'https://api.weixin.qq.com/cgi-bin/menu/create'\
|
13
21
|
"?access_token=#{access_token}"
|
14
|
-
Helper.http_post(url, menu_hash)
|
22
|
+
Helper.http_post(url, menu_hash.to_json)
|
15
23
|
end
|
16
24
|
|
25
|
+
# Get menus via get.
|
26
|
+
#
|
27
|
+
# @param [Enumerable<String>] access_token
|
28
|
+
# @return [Hash] Json based hash.
|
17
29
|
def menus(access_token)
|
18
30
|
url = 'https://api.weixin.qq.com/cgi-bin/menu/get'\
|
19
31
|
"?access_token=#{access_token}"
|
20
32
|
Helper.http_get(url, { accept: :json })
|
21
33
|
end
|
22
34
|
|
35
|
+
# Del menu via get.
|
36
|
+
#
|
37
|
+
# @param [Enumerable<String>] access_token
|
38
|
+
# @return [Hash] Json based hash.
|
23
39
|
def del_menu(access_token)
|
24
40
|
url = 'https://api.weixin.qq.com/cgi-bin/menu/delete'\
|
25
41
|
"?access_token=#{access_token}"
|
data/lib/wx_ext/api/msg.rb
CHANGED
@@ -1,18 +1,47 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require 'digest'
|
4
|
-
|
5
3
|
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
6
7
|
module Api
|
7
|
-
|
8
|
-
#
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
9
10
|
# @author FuShengYang
|
10
|
-
module
|
11
|
+
module Qrcode
|
11
12
|
class << self
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
# Create ticket via post.
|
15
|
+
#
|
16
|
+
# @param [Enumerable<String>] access_token
|
17
|
+
# @param [Hash] ticket_hash
|
18
|
+
# @return [Hash] Json based hash.
|
19
|
+
def create_ticket(access_token, ticket_hash)
|
20
|
+
url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create'\
|
21
|
+
"?access_token=#{access_token}"
|
22
|
+
Helper.http_post(url, ticket_hash.to_json)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get the qrcode via ticket.
|
26
|
+
#
|
27
|
+
# @param [Enumerable<String>] ticket
|
28
|
+
# @return [Hash] Json based hash.
|
29
|
+
def get_qrcode_by_ticket(ticket)
|
30
|
+
url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'\
|
31
|
+
"?ticket=#{ticket}"
|
32
|
+
Helper.http_get url
|
33
|
+
end
|
34
|
+
|
35
|
+
# Long url to short url via post.
|
36
|
+
#
|
37
|
+
# @param [Enumerable<String>] access_token
|
38
|
+
# @param [Enumerable<String>] action
|
39
|
+
# @param [Enumerable<String>] long_url
|
40
|
+
# @return [Hash] Json based hash.
|
41
|
+
def long_url_2_short(access_token, action='long2short', long_url)
|
42
|
+
url = 'https://api.weixin.qq.com/cgi-bin/shorturl'\
|
43
|
+
"?access_token=#{access_token}"
|
44
|
+
Helper.http_post(url, { action: action, long_url: long_url }.to_json)
|
16
45
|
end
|
17
46
|
end
|
18
47
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
7
|
+
module Api
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
10
|
+
# @author FuShengYang
|
11
|
+
module Msg
|
12
|
+
class << self
|
13
|
+
# Check the signature before send msg.
|
14
|
+
#
|
15
|
+
# @return [Boolean] Check the signature true or false.
|
16
|
+
def check_signature(signature, timestamp, nonce, token)
|
17
|
+
array = [token, timestamp, nonce].sort
|
18
|
+
signature == Digest::SHA1.hexdigest(array.join) ? true : false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
7
|
+
module Api
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
10
|
+
# @author FuShengYang
|
11
|
+
module Semantic
|
12
|
+
class << self
|
13
|
+
|
14
|
+
# Create semantic search via post.
|
15
|
+
#
|
16
|
+
# @param [Enumerable<String>] access_token
|
17
|
+
# @param [Hash] semantic_hash
|
18
|
+
# @return [Hash] Json based hash.
|
19
|
+
def semantic_search(access_token, semantic_hash)
|
20
|
+
url = 'https://api.weixin.qq.com/semantic/semproxy/search'\
|
21
|
+
"?access_token=#{access_token}"
|
22
|
+
Helper.http_post(url, semantic_hash.to_json)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
7
|
+
module Api
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
10
|
+
# @author FuShengYang
|
11
|
+
module TemplateMsg
|
12
|
+
class << self
|
13
|
+
|
14
|
+
# Set the industry via post.
|
15
|
+
#
|
16
|
+
# @param [Enumerable<String>] access_token
|
17
|
+
# @param [Enumerable<String>] industry_id1
|
18
|
+
# @param [Enumerable<String>] industry_id2
|
19
|
+
# @return [Hash] Json based hash.
|
20
|
+
def set_industry(access_token, industry_id1, industry_id2)
|
21
|
+
url = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry'\
|
22
|
+
"?access_token=#{access_token}"
|
23
|
+
Helper.http_post(url, { industry_id1: industry_id1, industry_id2: industry_id2 }.to_json)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Get the template ID via post.
|
27
|
+
#
|
28
|
+
# @param [Enumerable<String>] access_token
|
29
|
+
# @param [Enumerable<String>] template_id_short
|
30
|
+
# @return [Hash] Json based hash.
|
31
|
+
def get_template_id(access_token, template_id_short)
|
32
|
+
url = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template'\
|
33
|
+
"?access_token=#{access_token}"
|
34
|
+
Helper.http_post(url, { template_id_short: template_id_short })
|
35
|
+
end
|
36
|
+
|
37
|
+
# Send the template msg via post.
|
38
|
+
#
|
39
|
+
# @param [Enumerable<String>] access_token
|
40
|
+
# @param [Hash] template_msg_hash
|
41
|
+
# @return [Hash] Json based hash.
|
42
|
+
def send_template_msg(access_token, template_msg_hash)
|
43
|
+
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send'\
|
44
|
+
"?access_token=#{access_token}"
|
45
|
+
Helper.http_post(url, template_msg_hash.to_json)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,43 +1,73 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require 'rest_client'
|
4
|
-
|
5
3
|
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
6
7
|
module Api
|
7
|
-
|
8
|
-
#
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
9
10
|
# @author FuShengYang
|
10
11
|
module User
|
12
|
+
# User group api of weixin.
|
13
|
+
#
|
14
|
+
# @author FuShengYang
|
11
15
|
module Group
|
12
16
|
class << self
|
17
|
+
# Create user group via post.
|
18
|
+
#
|
19
|
+
# @param [Enumerable<String>] access_token
|
20
|
+
# @param [Enumerable<String>] group_name
|
21
|
+
# @return [Hash] A json parse hash.
|
13
22
|
def create_group(access_token, group_name)
|
14
23
|
url = 'https://api.weixin.qq.com/cgi-bin/groups/create'\
|
15
24
|
"?access_token=#{access_token}"
|
16
25
|
Helper.http_post(url, { group: { name: group_name } }.to_json)
|
17
26
|
end
|
18
27
|
|
28
|
+
# Get all groups via get.
|
29
|
+
#
|
30
|
+
# @param [Enumerable<String>] access_token
|
31
|
+
# @return [Hash] A json parse hash.
|
19
32
|
def groups(access_token)
|
20
33
|
url = 'https://api.weixin.qq.com/cgi-bin/groups/get'\
|
21
34
|
"?access_token=#{access_token}"
|
22
|
-
Helper.http_get(url, { accept: :json })
|
35
|
+
Helper.http_get(url, { accept: :json }.to_json)
|
23
36
|
end
|
24
37
|
|
38
|
+
# Get user of group via post.
|
39
|
+
#
|
40
|
+
# @param [Enumerable<String>] access_token
|
41
|
+
# @param [Enumerable<String>] openid
|
42
|
+
# @return [Hash] A json parse hash.
|
25
43
|
def user_of_group(access_token, openid)
|
26
44
|
url = 'https://api.weixin.qq.com/cgi-bin/groups/getid'\
|
27
45
|
"?access_token=#{access_token}"
|
28
|
-
Helper.http_post(url, { openid: openid })
|
46
|
+
Helper.http_post(url, { openid: openid }.to_json)
|
29
47
|
end
|
30
48
|
|
49
|
+
# Update user group name via post.
|
50
|
+
#
|
51
|
+
# @param [Enumerable<String>] access_token
|
52
|
+
# @param [Enumerable<String>] id
|
53
|
+
# @param [Enumerable<String>] name
|
54
|
+
# @return [Hash] A json parse hash.
|
31
55
|
def update_group_name(access_token, id, name)
|
32
56
|
url = 'https://api.weixin.qq.com/cgi-bin/groups/update'\
|
33
57
|
"?access_token=#{access_token}"
|
34
|
-
Helper.http_post(url, { group: { id: id, name: name } })
|
58
|
+
Helper.http_post(url, { group: { id: id, name: name } }.to_json)
|
35
59
|
end
|
36
60
|
|
61
|
+
# Move user group via post.
|
62
|
+
#
|
63
|
+
# @param [Enumerable<String>] access_token
|
64
|
+
# @param [Enumerable<String>] openid
|
65
|
+
# @param [Enumerable<String>] to_groupid
|
66
|
+
# @return [Hash] A json parse hash.
|
37
67
|
def mv_user_group(access_token, openid, to_groupid)
|
38
68
|
url = 'https://api.weixin.qq.com/cgi-bin/groups/members/update'\
|
39
69
|
"?access_token=#{access_token}"
|
40
|
-
Helper.http_post(url, { openid: openid, to_groupid: to_groupid })
|
70
|
+
Helper.http_post(url, { openid: openid, to_groupid: to_groupid }.to_json)
|
41
71
|
end
|
42
72
|
end
|
43
73
|
end
|
data/lib/wx_ext/api/user.rb
CHANGED
@@ -1,41 +1,94 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
require 'digest'
|
4
|
-
require 'rest_client'
|
5
|
-
require 'json'
|
6
|
-
require 'wx_ext/api/user/group'
|
7
|
-
|
8
3
|
module WxExt
|
4
|
+
# Weixin api
|
5
|
+
#
|
6
|
+
# @author FuShengYang
|
9
7
|
module Api
|
10
|
-
|
11
|
-
#
|
8
|
+
# User api of weixin.
|
9
|
+
#
|
12
10
|
# @author FuShengYang
|
13
11
|
module User
|
14
12
|
class << self
|
15
|
-
def change_remark(access_token, openid, remark) end
|
16
13
|
|
17
|
-
|
14
|
+
# Change the remark of user via post.
|
15
|
+
#
|
16
|
+
# @param [Enumerable<String>] access_token
|
17
|
+
# @param [Enumerable<String>] openid
|
18
|
+
# @param [Enumerable<String>] remark
|
19
|
+
# @return [Hash] Json based hash.
|
20
|
+
def change_remark(access_token, openid, remark)
|
21
|
+
url = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark'\
|
22
|
+
"?access_token=#{access_token}"
|
23
|
+
Helper.http_post(url, { openid: openid, remark: remark }.to_json)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Get the user base info.
|
27
|
+
#
|
28
|
+
# @param [Enumerable<String>] access_token
|
29
|
+
# @param [Enumerable<String>] openid
|
30
|
+
# @param [Enumerable<String>] lang
|
31
|
+
# @return [Hash] Json based hash.
|
32
|
+
def get_user_base_info(access_token, openid, lang='zh_CN')
|
33
|
+
url = 'https://api.weixin.qq.com/cgi-bin/user/info'\
|
34
|
+
"?access_token=#{access_token}&openid=#{openid}&lang=#{lang}"
|
35
|
+
Helper.http_get(url, { accept: :json })
|
36
|
+
end
|
18
37
|
|
19
|
-
|
38
|
+
# Get user list of weixin.
|
39
|
+
#
|
40
|
+
# @param [Enumerable<String>] access_token
|
41
|
+
# @param [Enumerable<String>] next_openid
|
42
|
+
# @return [Hash] Json based hash.
|
43
|
+
def user_list(access_token, next_openid)
|
44
|
+
url = 'https://api.weixin.qq.com/cgi-bin/user/get'\
|
45
|
+
"?access_token=#{access_token}"
|
46
|
+
url += "&next_openid=#{next_openid}" if next_openid
|
47
|
+
Helper.http_get(url, { accept: :json })
|
48
|
+
end
|
20
49
|
|
50
|
+
# Get oauth2 token with code.
|
51
|
+
#
|
52
|
+
# @param [Enumerable<String>] app_id
|
53
|
+
# @param [Enumerable<String>] app_secret
|
54
|
+
# @param [Enumerable<String>] code
|
55
|
+
# @param [Enumerable<String>] grant_type
|
56
|
+
# @return [Hash] Json based hash.
|
21
57
|
def get_oauth2_token_with_code(app_id, app_secret, code, grant_type='authorization_code')
|
22
58
|
url = 'https://api.weixin.qq.com/sns/oauth2/access_token'\
|
23
59
|
"?appid=#{app_id}&secret=#{app_secret}&code=CODE&grant_type=#{grant_type}"
|
24
60
|
Helper.http_get(url, { accept: :json })
|
25
61
|
end
|
26
62
|
|
63
|
+
# Refresh oauth2 token.
|
64
|
+
#
|
65
|
+
# @param [Enumerable<String>] app_id
|
66
|
+
# @param [Enumerable<String>] refresh_token
|
67
|
+
# @param [Enumerable<String>] grant_type
|
68
|
+
# @return [Hash] Json based hash.
|
27
69
|
def refresh_oauth2_token(app_id, refresh_token, grant_type='refresh_token')
|
28
70
|
url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token'\
|
29
71
|
"?appid=#{app_id}&grant_type=#{grant_type}&refresh_token=#{refresh_token}"
|
30
72
|
Helper.http_get(url, { accept: :json })
|
31
73
|
end
|
32
74
|
|
75
|
+
# Get user info with snsapi_userinfo.
|
76
|
+
#
|
77
|
+
# @param [Enumerable<String>] oauth2_token
|
78
|
+
# @param [Enumerable<String>] openid
|
79
|
+
# @param [Enumerable<String>] lang
|
80
|
+
# @return [Hash] Json based hash.
|
33
81
|
def get_user_info_with_snsapi_userinfo(oauth2_token, openid, lang)
|
34
82
|
url = 'https://api.weixin.qq.com/sns/userinfo'\
|
35
83
|
"?access_token=#{oauth2_token}&#{openid}=OPENID&lang=#{lang}"
|
36
84
|
Helper.http_get(url, { accept: :json })
|
37
85
|
end
|
38
86
|
|
87
|
+
# Check the oauth2_token.
|
88
|
+
#
|
89
|
+
# @param [Enumerable<String>] openid
|
90
|
+
# @param [Enumerable<String>] oauth2_token
|
91
|
+
# @return [Hash] Json based hash.
|
39
92
|
def check_oauth2_token(openid, oauth2_token)
|
40
93
|
url = 'https://api.weixin.qq.com/sns/auth'\
|
41
94
|
"?access_token=#{oauth2_token}&openid=#{openid}"
|
data/lib/wx_ext/api.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
#require 'wx_ext/api/qrcode'
|
4
|
-
#require 'wx_ext/api/media'
|
5
|
-
#require 'wx_ext/api/oauth'
|
6
|
-
|
7
3
|
require 'wx_ext/api/base'
|
4
|
+
require 'wx_ext/api/customer_service'
|
8
5
|
require 'wx_ext/api/js'
|
6
|
+
require 'wx_ext/api/mass'
|
7
|
+
require 'wx_ext/api/menu'
|
9
8
|
require 'wx_ext/api/msg'
|
9
|
+
require 'wx_ext/api/qrcode'
|
10
|
+
require 'wx_ext/api/semantic'
|
11
|
+
require 'wx_ext/api/template_msg'
|
10
12
|
require 'wx_ext/api/user'
|
11
|
-
require 'wx_ext/api/menu'
|
data/lib/wx_ext/helper.rb
CHANGED
@@ -4,15 +4,27 @@ require 'rest_client'
|
|
4
4
|
require 'json'
|
5
5
|
|
6
6
|
module WxExt
|
7
|
-
|
7
|
+
# WxExt Helper like get and post short method.
|
8
|
+
#
|
8
9
|
# @author FuShengYang
|
9
10
|
module Helper
|
10
11
|
class << self
|
12
|
+
# Http get helper of this gem, always return a hash.
|
13
|
+
#
|
14
|
+
# @param [Enumerable<String>] url
|
15
|
+
# @param [Hash] headers
|
16
|
+
# @return [Hash] A json parse hash.
|
11
17
|
def http_get(url, headers = {})
|
12
18
|
res = RestClient.get url, headers
|
13
19
|
JSON.parse res
|
14
20
|
end
|
15
21
|
|
22
|
+
# Http post helper of this gem, always return a hash.
|
23
|
+
#
|
24
|
+
# @param [Enumerable<String>] url
|
25
|
+
# @param [Hash] params
|
26
|
+
# @param [Hash] headers
|
27
|
+
# @return [Hash] A json parse hash.
|
16
28
|
def http_post(url, params, headers = {})
|
17
29
|
res = RestClient.post url, params, headers
|
18
30
|
JSON.parse res
|
data/lib/wx_ext/sougou_weixin.rb
CHANGED
@@ -6,9 +6,16 @@ require 'json'
|
|
6
6
|
require 'open-uri'
|
7
7
|
|
8
8
|
module WxExt
|
9
|
-
#
|
9
|
+
# Spider post from http://weixin.sogou.com
|
10
|
+
#
|
10
11
|
# @author FuShengYang
|
11
12
|
class SougouWeixin
|
13
|
+
# Spider posts from sougou, only one page.
|
14
|
+
#
|
15
|
+
# @param [Enumerable<String>] openid
|
16
|
+
# @param [Integer] page_index
|
17
|
+
# @param [Enumerable<String>] date_last
|
18
|
+
# @return [Hash] A spider posts hash with total_pages etc.
|
12
19
|
def self.spider_posts_from_sougou(openid, page_index = 1, date_last = (Time.now - 3600 * 24 * 10).strftime("%Y-%m-%d"))
|
13
20
|
json_url = "http://weixin.sogou.com/gzhjs?&openid=#{openid}&page=#{page_index}"
|
14
21
|
res = RestClient.get json_url
|
@@ -75,6 +82,11 @@ module WxExt
|
|
75
82
|
}
|
76
83
|
end
|
77
84
|
|
85
|
+
# Spider posts from sougou, last date.
|
86
|
+
#
|
87
|
+
# @param [Enumerable<String>] openid
|
88
|
+
# @param [Enumerable<String>] date_last
|
89
|
+
# @return [Hash] A spider posts hash with total_pages etc.
|
78
90
|
def self.spider_posts_later_date(openid, date_last = (Time.now - 3600 * 24 * 10).strftime("%Y-%m-%d"))
|
79
91
|
spider_posts_first_page_hash = spider_posts_from_sougou(openid, 1, date_last)
|
80
92
|
total_pages = spider_posts_first_page_hash[:total_pages].to_i
|
@@ -88,9 +100,9 @@ module WxExt
|
|
88
100
|
end
|
89
101
|
end
|
90
102
|
{
|
91
|
-
|
92
|
-
|
93
|
-
|
103
|
+
total_items: spider_posts_first_page_hash[:total_items],
|
104
|
+
total_pages: total_pages,
|
105
|
+
spider_posts: spider_posts.uniq
|
94
106
|
}
|
95
107
|
end
|
96
108
|
end
|
data/lib/wx_ext/version.rb
CHANGED
data/lib/wx_ext/wei_xin.rb
CHANGED
@@ -6,11 +6,13 @@ require 'json'
|
|
6
6
|
require 'nokogiri'
|
7
7
|
|
8
8
|
module WxExt
|
9
|
-
#
|
9
|
+
# weixin extention of mp.weixin.qq.com
|
10
|
+
#
|
10
11
|
# @author FuShengYang
|
11
12
|
class WeiXin
|
12
13
|
attr_accessor :account, :password, :home_url, :token, :user_name, \
|
13
|
-
|
14
|
+
:ticket_id, :ticket, :cookies, :operation_seq
|
15
|
+
|
14
16
|
def initialize(account, password)
|
15
17
|
@account = account
|
16
18
|
@password = password
|
@@ -25,7 +27,7 @@ module WxExt
|
|
25
27
|
|
26
28
|
# 模拟登陆微信公众平台, 初始化 access_token, cookies
|
27
29
|
#
|
28
|
-
# @return [Hash]
|
30
|
+
# @return [Hash] Hash with login status and msg.
|
29
31
|
def login
|
30
32
|
password = Digest::MD5.hexdigest @password
|
31
33
|
login_headers = {
|
@@ -45,11 +47,11 @@ module WxExt
|
|
45
47
|
status: 0,
|
46
48
|
msg: 'ok'
|
47
49
|
}
|
48
|
-
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
50
|
+
|
51
|
+
# 0: "ok", "redirect_url": ""
|
52
|
+
# -8: "need verify code"
|
53
|
+
# -23: "acct\/password error"
|
54
|
+
# -21: "user not exist"
|
53
55
|
res_hash = JSON.parse res.to_s
|
54
56
|
sta = res_hash['base_resp']['ret'].to_s
|
55
57
|
if sta == '0'
|
@@ -79,10 +81,12 @@ module WxExt
|
|
79
81
|
return_hash
|
80
82
|
end
|
81
83
|
|
82
|
-
#
|
84
|
+
# Init ticket, cookies, operation_seq, user_name etc.
|
85
|
+
#
|
86
|
+
# @return [Boolean] Init ticket, cookies, operation_seq, user_name true or false.
|
83
87
|
def init
|
84
88
|
msg_send_url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
85
|
-
|
89
|
+
"?t=mass/send&token=#{@token}&lang=zh_CN"
|
86
90
|
msg_send_page = RestClient.get msg_send_url, cookies: @cookies
|
87
91
|
@cookies = @cookies.merge msg_send_page.cookies
|
88
92
|
|
@@ -98,36 +102,46 @@ module WxExt
|
|
98
102
|
end
|
99
103
|
end
|
100
104
|
|
101
|
-
#
|
105
|
+
# Upload file to file.weixin.qq.com
|
106
|
+
#
|
107
|
+
# @param [File] file
|
108
|
+
# @param [String] file_name
|
109
|
+
# @param [String] folder
|
110
|
+
# @return [Hash] A json parse hash.
|
102
111
|
def upload_file(file, file_name, folder = '/cgi-bin/uploads')
|
103
112
|
upload_url = 'https://mp.weixin.qq.com/cgi-bin/filetransfer'\
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
response = RestClient.post upload_url, file: file,
|
108
|
-
|
109
|
-
|
113
|
+
'?action=upload_material&f=json&writetype=doublewrite'\
|
114
|
+
"&groupid=1&ticket_id=#{@ticket_id}"\
|
115
|
+
"&ticket=#{@ticket}&token=#{@token}&lang=zh_CN"
|
116
|
+
response = RestClient.post upload_url, file: file,
|
117
|
+
Filename: file_name,
|
118
|
+
folder: folder
|
110
119
|
JSON.parse response.to_s
|
111
120
|
end
|
112
121
|
|
113
|
-
#
|
122
|
+
# Upload single news to mp.weixin.qq.com
|
123
|
+
#
|
124
|
+
# @param [Hash] single_msg_params
|
125
|
+
# @return [Hash] A json parse hash.
|
114
126
|
def upload_single_msg(single_msg_params)
|
115
127
|
post_single_msg_uri = 'cgi-bin/operate_appmsg'\
|
116
|
-
|
117
|
-
|
128
|
+
'?t=ajax-response&sub=create&type=10&token'\
|
129
|
+
"=#{@token}&lang=zh_CN"
|
118
130
|
headers = {
|
119
131
|
referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit'\
|
120
|
-
|
121
|
-
|
132
|
+
'&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN'\
|
133
|
+
"&token=#{@token}"
|
122
134
|
}
|
123
135
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
124
136
|
cookies: @cookies)
|
125
137
|
res = resource[post_single_msg_uri].post single_msg_params
|
126
|
-
# {"ret":"0", "msg":"OK"}
|
127
138
|
JSON.parse res.to_s
|
128
139
|
end
|
129
140
|
|
130
|
-
#
|
141
|
+
# Upload multi news to mp.weixin.qq.com
|
142
|
+
#
|
143
|
+
# @param [Hash] msg_params
|
144
|
+
# @return [Hash] A json parse hash.
|
131
145
|
def upload_multi_msg(msg_params)
|
132
146
|
uri = 'cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=10'\
|
133
147
|
"&token=#{@token}&lang=zh_CN"
|
@@ -139,11 +153,13 @@ module WxExt
|
|
139
153
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
140
154
|
cookies: @cookies)
|
141
155
|
post_msg_res = resource[uri].post msg_params
|
142
|
-
# {"ret":"0", "msg":"OK"}
|
143
156
|
JSON.parse post_msg_res.to_s
|
144
157
|
end
|
145
158
|
|
146
|
-
#
|
159
|
+
# Preview broadcast news to user.
|
160
|
+
#
|
161
|
+
# @param [Hash] msg_params_with_name
|
162
|
+
# @return [Hash] A json parse hash.
|
147
163
|
def preview_msg(msg_params_with_name)
|
148
164
|
uri = 'cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview'\
|
149
165
|
"&type=10&token=#{@token}&lang=zh_CN"
|
@@ -155,39 +171,48 @@ module WxExt
|
|
155
171
|
cookies: @cookies)
|
156
172
|
|
157
173
|
res = resource[uri].post msg_params_with_name
|
158
|
-
#
|
174
|
+
# "ret":"0", "msg":"preview send success!", "appMsgId":"201796045", "fakeid":""
|
159
175
|
JSON.parse res.to_s
|
160
176
|
end
|
161
177
|
|
162
|
-
#
|
178
|
+
# Broadcast news to mp.weixin.qq.com.
|
179
|
+
#
|
180
|
+
# @param [Hash] msg_params
|
181
|
+
# @return [Hash] A json parse hash.
|
163
182
|
def broadcast_msg(msg_params)
|
164
183
|
uri = "cgi-bin/masssend?t=ajax-response&token=#{@token}&lang=zh_CN"
|
165
184
|
headers = {
|
166
185
|
referer: 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
167
|
-
|
186
|
+
"?t=mass/send&token=#{token}&lang=zh_CN",
|
168
187
|
host: 'mp.weixin.qq.com'
|
169
188
|
}
|
170
189
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
171
|
-
|
190
|
+
cookies: @cookies)
|
172
191
|
post_msg_res = resource[uri].post msg_params
|
173
|
-
# {"ret":"0", "msg":"OK"}
|
174
192
|
JSON.parse post_msg_res.to_s
|
175
193
|
end
|
176
194
|
|
177
|
-
#
|
195
|
+
# Get all news.
|
196
|
+
#
|
197
|
+
# @param [Integer] msg_begin
|
198
|
+
# @param [Integer] msg_count
|
199
|
+
# @return [Hash] A json parse hash.
|
178
200
|
def get_app_msg_list(msg_begin = 0, msg_count = 10)
|
179
201
|
url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?type=10&action=list'\
|
180
|
-
|
181
|
-
|
182
|
-
|
202
|
+
"&begin=#{msg_begin}&count=#{msg_count}&f=json&token=#{@token}"\
|
203
|
+
"&lang=zh_CN&token=#{@token}&lang=zh_CN&f=json&ajax=1"\
|
204
|
+
"&random=#{rand}"
|
183
205
|
msg_json = RestClient.get url, cookies: @cookies
|
184
206
|
JSON.parse msg_json.to_s
|
185
207
|
end
|
186
208
|
|
187
|
-
#
|
209
|
+
# Get new coming msgs from user.
|
210
|
+
#
|
211
|
+
# @param [String] last_msg_id
|
212
|
+
# @return [Hash] A json parse hash.
|
188
213
|
def get_new_msg_num(last_msg_id)
|
189
214
|
uri = 'cgi-bin/getnewmsgnum?f=json&t=ajax-getmsgnum'\
|
190
|
-
|
215
|
+
"&lastmsgid=#{last_msg_id}&token=#{@token}&lang=zh_CN"
|
191
216
|
post_params = {
|
192
217
|
ajax: 1,
|
193
218
|
f: 'json',
|
@@ -197,18 +222,21 @@ module WxExt
|
|
197
222
|
}
|
198
223
|
post_headers = {
|
199
224
|
referer: 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list'\
|
200
|
-
|
225
|
+
"&count=20&day=7&token=#{@token}&lang=zh_CN"
|
201
226
|
}
|
202
227
|
resource = RestClient::Resource.new(@home_url, headers: post_headers,
|
203
|
-
|
228
|
+
cookies: @cookies)
|
204
229
|
res = resource[uri].post post_params
|
205
230
|
JSON.parse res.to_s
|
206
231
|
end
|
207
232
|
|
208
|
-
#
|
233
|
+
# Get user info.
|
234
|
+
#
|
235
|
+
# @param [String] fakeid
|
236
|
+
# @return [Hash] A json parse hash.
|
209
237
|
def get_contact_info(fakeid)
|
210
238
|
uri = 'cgi-bin/getcontactinfo?t=ajax-getcontactinfo'\
|
211
|
-
|
239
|
+
"&lang=zh_CN&fakeid=#{fakeid}"
|
212
240
|
post_params = {
|
213
241
|
ajax: 1,
|
214
242
|
f: 'json',
|
@@ -218,28 +246,32 @@ module WxExt
|
|
218
246
|
}
|
219
247
|
post_headers = {
|
220
248
|
referer: 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index'\
|
221
|
-
|
249
|
+
"&pagesize=10&pageidx=0&type=0&token=#{@token}&lang=zh_CN"
|
222
250
|
}
|
223
251
|
resource = RestClient::Resource.new(@home_url, headers: post_headers,
|
224
|
-
|
252
|
+
cookies: @cookies)
|
225
253
|
res = resource[uri].post post_params
|
226
254
|
JSON.parse res.to_s
|
227
255
|
end
|
228
256
|
|
229
|
-
#
|
257
|
+
# Get country list.
|
258
|
+
#
|
259
|
+
# @return [Hash] A json parse hash.
|
230
260
|
def get_country_list
|
231
261
|
url = 'https://mp.weixin.qq.com/cgi-bin/getregions'\
|
232
|
-
|
233
|
-
|
262
|
+
"?t=setting/ajax-getregions&id=0&token=#{@token}&lang=zh_CN"\
|
263
|
+
"&token=#{@token}&lang=zh_CN&f=json&ajax=1&random=#{rand}"
|
234
264
|
resource = RestClient::Resource.new(url, cookies: @cookies)
|
235
265
|
res = resource.get
|
236
266
|
JSON.parse res.to_s
|
237
267
|
end
|
238
268
|
|
239
|
-
#
|
269
|
+
# Get this weixin can broadcast news count.
|
270
|
+
#
|
271
|
+
# @return [Integer] day msg count.
|
240
272
|
def get_day_msg_count
|
241
273
|
url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
242
|
-
|
274
|
+
"?t=mass/send&token=#{@token}&lang=zh_CN"
|
243
275
|
res = RestClient.get(url, cookies: @cookies)
|
244
276
|
day_msg_count = 0
|
245
277
|
reg = /.*mass_send_left\s*:\s*can_verify_apply\s*\?\s*\'(\d*)\'\*/
|
@@ -247,11 +279,19 @@ module WxExt
|
|
247
279
|
day_msg_count.to_i
|
248
280
|
end
|
249
281
|
|
250
|
-
#
|
282
|
+
# Get msg items.
|
283
|
+
#
|
284
|
+
# @param [Integer] count
|
285
|
+
# @param [Integer] day
|
286
|
+
# @param [Integer] filterivrmsg
|
287
|
+
# @param [String] action
|
288
|
+
# @param [String] keyword
|
289
|
+
# @param [String] offset
|
290
|
+
# @return [Hash] A json parse hash.
|
251
291
|
def get_msg_items(count = 20, day = 7, filterivrmsg = 1, action='', keyword='', frommsgid='', offset='')
|
252
292
|
url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list'\
|
253
|
-
|
254
|
-
|
293
|
+
"&action=#{action}&keyword=#{keyword}&frommsgid=#{frommsgid}&offset=#{offset}&count=#{count}"\
|
294
|
+
"&day=#{day}filterivrmsg=#{filterivrmsg}&token=#{@token}&lang=zh_CN"
|
255
295
|
resource = RestClient::Resource.new(url, cookies: @cookies)
|
256
296
|
res = resource.get
|
257
297
|
reg = /.*total_count\s*:\s*(\d*).*latest_msg_id\s*:\s*\'(\d*)\'.*list\s*:\s*\((.*)\)\.msg_item,.*filterivrmsg\s*:\s*\"(\d*)\".*/m
|
@@ -279,7 +319,9 @@ module WxExt
|
|
279
319
|
return_hash
|
280
320
|
end
|
281
321
|
|
282
|
-
#
|
322
|
+
# Get fans count.
|
323
|
+
#
|
324
|
+
# @return [Hash] Fans count with friends_list, group_list etc.
|
283
325
|
def get_fans_count
|
284
326
|
url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index'\
|
285
327
|
"&pagesize=10&pageidx=0&type=0&token=#{ @token }&lang=zh_CN"
|
@@ -304,12 +346,15 @@ module WxExt
|
|
304
346
|
return_hash
|
305
347
|
end
|
306
348
|
|
307
|
-
#
|
308
|
-
#
|
309
|
-
#
|
349
|
+
# Quick reply to user.
|
350
|
+
#
|
351
|
+
# @param [String] content
|
352
|
+
# @param [String] quickreplyid
|
353
|
+
# @param [String] tofakeid
|
354
|
+
# @return [Hash] A json parse hash.
|
310
355
|
def quick_reply(content, quickreplyid, tofakeid)
|
311
356
|
post_uri = 'cgi-bin/singlesend'\
|
312
|
-
|
357
|
+
"?t=ajax-response&f=json&token=#{ @token }&lang=zh_CN"
|
313
358
|
params = {
|
314
359
|
ajax: 1,
|
315
360
|
content: content,
|
@@ -325,17 +370,20 @@ module WxExt
|
|
325
370
|
}
|
326
371
|
headers = {
|
327
372
|
referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
|
328
|
-
|
373
|
+
"?t=message/list&count=20&day=7&token=#{ @token }&lang=zh_CN"
|
329
374
|
}
|
330
375
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
331
|
-
|
376
|
+
cookies: @cookies)
|
332
377
|
res = resource[post_uri].post params
|
378
|
+
#
|
379
|
+
# 10706: "customer block": "48小时内的才行"
|
333
380
|
JSON.parse res.to_s
|
334
381
|
end
|
335
382
|
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
383
|
+
# Collect msg of user.
|
384
|
+
#
|
385
|
+
# @param [String] msgid
|
386
|
+
# @return [Hash] A json parse hash.
|
339
387
|
def collect_msg(msgid)
|
340
388
|
uri = "cgi-bin/setstarmessage?t=ajax-setstarmessage&token=#{ @token }&lang=zh_CN"
|
341
389
|
params = {
|
@@ -349,15 +397,18 @@ module WxExt
|
|
349
397
|
}
|
350
398
|
headers = {
|
351
399
|
referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
|
352
|
-
|
400
|
+
"?t=message/list&token=#{ @token }&count=20&day=7"
|
353
401
|
}
|
354
402
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
355
|
-
|
403
|
+
cookies: @cookies)
|
356
404
|
res = resource[uri].post params
|
357
405
|
JSON.parse res.to_s
|
358
406
|
end
|
359
407
|
|
360
|
-
#
|
408
|
+
# Un collect msg of user.
|
409
|
+
#
|
410
|
+
# @param [String] msgid
|
411
|
+
# @return [Hash] A json parse hash.
|
361
412
|
def un_collect_msg(msgid)
|
362
413
|
uri = "cgi-bin/setstarmessage?t=ajax-setstarmessage&token=#{ @token }&lang=zh_CN"
|
363
414
|
params = {
|
@@ -371,12 +422,11 @@ module WxExt
|
|
371
422
|
}
|
372
423
|
headers = {
|
373
424
|
referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
|
374
|
-
|
425
|
+
"?t=message/list&token=#{ @token }&count=20&day=7"
|
375
426
|
}
|
376
427
|
resource = RestClient::Resource.new(@home_url, headers: headers,
|
377
|
-
|
428
|
+
cookies: @cookies)
|
378
429
|
res = resource[uri].post params
|
379
|
-
# { "ret":0, "msg":"sys ok"}
|
380
430
|
JSON.parse res.to_s
|
381
431
|
end
|
382
432
|
end
|
data/lib/wx_ext.rb
CHANGED
@@ -5,17 +5,30 @@ require 'wx_ext/sougou_weixin'
|
|
5
5
|
require 'wx_ext/wei_xin'
|
6
6
|
require 'wx_ext/api'
|
7
7
|
|
8
|
-
#
|
8
|
+
# Weixin extention, sougou spider and weixin api.
|
9
|
+
#
|
9
10
|
# @author FuShengYang
|
10
11
|
module WxExt
|
12
|
+
|
13
|
+
# Return the root path of this gem.
|
14
|
+
#
|
15
|
+
# @return [String] Path of the gem's root.
|
11
16
|
def self.root
|
12
17
|
File.dirname __dir__
|
13
18
|
end
|
14
19
|
|
20
|
+
|
21
|
+
# Return the lib path of this gem.
|
22
|
+
#
|
23
|
+
# @return [String] Path of the gem's lib.
|
15
24
|
def self.lib
|
16
25
|
File.join root, 'lib'
|
17
26
|
end
|
18
27
|
|
28
|
+
|
29
|
+
# Return the spec path of this gem.
|
30
|
+
#
|
31
|
+
# @return [String] Path of the gem's spec.
|
19
32
|
def self.spec
|
20
33
|
File.join root, 'spec'
|
21
34
|
end
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wx_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- flowerwrong
|
@@ -109,9 +109,14 @@ files:
|
|
109
109
|
- lib/wx_ext.rb
|
110
110
|
- lib/wx_ext/api.rb
|
111
111
|
- lib/wx_ext/api/base.rb
|
112
|
+
- lib/wx_ext/api/customer_service.rb
|
112
113
|
- lib/wx_ext/api/js.rb
|
114
|
+
- lib/wx_ext/api/mass.rb
|
113
115
|
- lib/wx_ext/api/menu.rb
|
114
116
|
- lib/wx_ext/api/msg.rb
|
117
|
+
- lib/wx_ext/api/qrcode.rb
|
118
|
+
- lib/wx_ext/api/semantic.rb
|
119
|
+
- lib/wx_ext/api/template_msg.rb
|
115
120
|
- lib/wx_ext/api/user.rb
|
116
121
|
- lib/wx_ext/api/user/group.rb
|
117
122
|
- lib/wx_ext/helper.rb
|
@@ -121,9 +126,13 @@ files:
|
|
121
126
|
- spec/spec_helper.rb
|
122
127
|
- spec/test.png
|
123
128
|
- spec/wx_ext/api/base_spec.rb
|
129
|
+
- spec/wx_ext/api/customer_service_spec.rb
|
124
130
|
- spec/wx_ext/api/js_spec.rb
|
131
|
+
- spec/wx_ext/api/mass_spec.rb
|
125
132
|
- spec/wx_ext/api/menu_spec.rb
|
126
133
|
- spec/wx_ext/api/msg_spec.rb
|
134
|
+
- spec/wx_ext/api/semantic_spec.rb
|
135
|
+
- spec/wx_ext/api/template_msg_spec.rb
|
127
136
|
- spec/wx_ext/api/user/group_spec.rb
|
128
137
|
- spec/wx_ext/api/user_spec.rb
|
129
138
|
- spec/wx_ext/sougou_weixin_spec.rb
|
@@ -157,9 +166,13 @@ test_files:
|
|
157
166
|
- spec/spec_helper.rb
|
158
167
|
- spec/test.png
|
159
168
|
- spec/wx_ext/api/base_spec.rb
|
169
|
+
- spec/wx_ext/api/customer_service_spec.rb
|
160
170
|
- spec/wx_ext/api/js_spec.rb
|
171
|
+
- spec/wx_ext/api/mass_spec.rb
|
161
172
|
- spec/wx_ext/api/menu_spec.rb
|
162
173
|
- spec/wx_ext/api/msg_spec.rb
|
174
|
+
- spec/wx_ext/api/semantic_spec.rb
|
175
|
+
- spec/wx_ext/api/template_msg_spec.rb
|
163
176
|
- spec/wx_ext/api/user/group_spec.rb
|
164
177
|
- spec/wx_ext/api/user_spec.rb
|
165
178
|
- spec/wx_ext/sougou_weixin_spec.rb
|