wx_ext 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/lib/wx_ext/version.rb +2 -1
- data/lib/wx_ext.rb +116 -76
- data/spec/spec_helper.rb +2 -1
- data/spec/wx_ext/weixin_spec.rb +41 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8a9175b614f4a055da85f6303f46a43e49960fd
|
4
|
+
data.tar.gz: fa906a29a4b0b5c7aa4cebc193941af670912185
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe8b3ff1f7086f4bb70ee9f12ed3faaf0c90dda65a548a0c0ab05db31ef508c5540e65a95e4c40ee2da7c9000a960fad8d5af9a4a07518a215a9cd58c2ec5a03
|
7
|
+
data.tar.gz: 361b995e41b4b3bc565bd71bfa3ec74d81db33a8519bc09bc2ed26cfd4f745f05fd46ea1b34cad2ea4ebe1400eec4eed3a9e8440effc4bf44274b4bdfe7bd93a
|
data/README.md
CHANGED
data/Rakefile
CHANGED
data/lib/wx_ext/version.rb
CHANGED
data/lib/wx_ext.rb
CHANGED
@@ -22,50 +22,62 @@ module WxExt
|
|
22
22
|
end
|
23
23
|
|
24
24
|
# 使用用户名和密码登陆微信公众平台获取access_token, 过期时间是7200s
|
25
|
-
def
|
26
|
-
login_name = @account
|
25
|
+
def login
|
27
26
|
password = Digest::MD5.hexdigest @password
|
28
|
-
home_url = @home_url
|
29
|
-
|
30
27
|
login_headers = {
|
31
|
-
|
28
|
+
referer: 'https://mp.weixin.qq.com/'
|
32
29
|
}
|
33
30
|
login_params = {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
username: @account,
|
32
|
+
pwd: password,
|
33
|
+
imgcode: '',
|
34
|
+
f: 'json'
|
38
35
|
}
|
39
36
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
resource = RestClient::Resource.new(@home_url, headers: login_headers)
|
38
|
+
res = resource['cgi-bin/login'].post login_params
|
39
|
+
@cookies = res.cookies
|
40
|
+
return_hash = {
|
41
|
+
status: 0,
|
42
|
+
msg: 'ok'
|
43
|
+
}
|
44
44
|
# {"base_resp"=>{"ret"=>0, "err_msg"=>"ok"}, "redirect_url"=>"/cgi-bin/home?t=home/index&lang=zh_CN&token=1869497342"}
|
45
45
|
# {"base_resp":{"ret":-8,"err_msg":"need verify code"}}
|
46
46
|
# https://mp.weixin.qq.com/cgi-bin/verifycode?username=tuodan@thecampus.cc&r=1415774604450
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
@token = $1
|
47
|
+
res_hash = JSON.parse res.to_s
|
48
|
+
if res_hash['base_resp']['ret'].to_s == '0'
|
49
|
+
token_reg = /.*token=(\d+)\".*/
|
50
|
+
@token = $1 if token_reg =~ res.to_s
|
51
|
+
elsif res_hash['bash_resp']['ret'].to_s == '-8'
|
52
|
+
return_hash = {
|
53
|
+
status: -8,
|
54
|
+
msg: 'need_varify_code'
|
55
|
+
}
|
56
|
+
else
|
57
|
+
return_hash = {
|
58
|
+
status: -1,
|
59
|
+
msg: 'system_error'
|
60
|
+
}
|
51
61
|
end
|
62
|
+
return_hash
|
63
|
+
end
|
52
64
|
|
53
|
-
|
54
|
-
|
55
|
-
|
65
|
+
# 初始化 ticket, cookies, operation_seq, user_name 等等信息
|
66
|
+
def init
|
67
|
+
msg_send_url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
68
|
+
"?t=mass/send&token=#{@token}&lang=zh_CN"
|
69
|
+
msg_send_page = RestClient.get msg_send_url, cookies: @cookies
|
70
|
+
@cookies = @cookies.merge msg_send_page.cookies
|
56
71
|
|
57
72
|
ticket_reg = /.*ticket\s*:\s*\"(\w+)\".*user_name\s*:\s*\"(\w+)\".*nick_name\s*:\s*\"(.*)\".*/m
|
58
|
-
|
59
73
|
operation_seq_reg = /.*operation_seq\s*:\s*\"(\d+)\".*/
|
60
|
-
if operation_seq_reg =~ msg_send_page.to_s
|
61
|
-
@operation_seq = $1
|
62
|
-
end
|
74
|
+
@operation_seq = $1 if operation_seq_reg =~ msg_send_page.to_s
|
63
75
|
if ticket_reg =~ msg_send_page.to_s
|
64
76
|
@ticket = $1
|
65
77
|
@user_name = @ticket_id= $2
|
66
78
|
true
|
67
79
|
else
|
68
|
-
|
80
|
+
false
|
69
81
|
end
|
70
82
|
end
|
71
83
|
|
@@ -83,54 +95,63 @@ module WxExt
|
|
83
95
|
post_single_msg_uri = 'cgi-bin/operate_appmsg'\
|
84
96
|
'?t=ajax-response&sub=create&type=10&token'\
|
85
97
|
"=#{@token}&lang=zh_CN"
|
86
|
-
|
98
|
+
headers = {
|
87
99
|
referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit'\
|
88
|
-
|
100
|
+
'&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN'\
|
101
|
+
"&token=#{@token}"
|
89
102
|
}
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
post_single_msg_res = post_single_msg_resource[post_single_msg_uri].post single_msg_params
|
103
|
+
resource = RestClient::Resource.new(@home_url, headers: headers,
|
104
|
+
cookies: @cookies)
|
105
|
+
res = resource[post_single_msg_uri].post single_msg_params
|
94
106
|
# {"ret":"0", "msg":"OK"}
|
95
|
-
|
107
|
+
JSON.parse res.to_s
|
96
108
|
end
|
97
109
|
|
98
110
|
# 发送多图文到素材中心
|
99
111
|
def upload_multi_msg(msg_params)
|
100
|
-
|
101
|
-
|
102
|
-
|
112
|
+
uri = 'cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=10'\
|
113
|
+
"&token=#{@token}&lang=zh_CN"
|
114
|
+
headers = {
|
115
|
+
referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg'\
|
116
|
+
'?t=media/appmsg_edit&action=edit&type=10'\
|
117
|
+
"&isMul=1&isNew=1&lang=zh_CN&token=#{@token}"
|
103
118
|
}
|
104
|
-
|
105
|
-
|
119
|
+
resource = RestClient::Resource.new(@home_url, headers: headers,
|
120
|
+
cookies: @cookies)
|
121
|
+
post_msg_res = resource[uri].post msg_params
|
106
122
|
# {"ret":"0", "msg":"OK"}
|
107
|
-
|
123
|
+
JSON.parse post_msg_res.to_s
|
108
124
|
end
|
109
125
|
|
110
126
|
# 群发图文消息
|
111
127
|
def broadcast_msg(msg_params)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
128
|
+
uri = "cgi-bin/masssend?t=ajax-response&token=#{@token}&lang=zh_CN"
|
129
|
+
headers = {
|
130
|
+
referer: 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
131
|
+
"?t=mass/send&token=#{token}&lang=zh_CN",
|
132
|
+
host: 'mp.weixin.qq.com'
|
116
133
|
}
|
117
|
-
|
118
|
-
|
134
|
+
resource = RestClient::Resource.new(@home_url, headers: headers,
|
135
|
+
cookies: @cookies)
|
136
|
+
post_msg_res = resource[uri].post msg_params
|
119
137
|
# {"ret":"0", "msg":"OK"}
|
120
|
-
|
121
|
-
res_hash = JSON.parse post_msg_res.to_s
|
138
|
+
JSON.parse post_msg_res.to_s
|
122
139
|
end
|
123
140
|
|
124
141
|
# 获取所有的图文列表
|
125
142
|
def get_app_msg_list(msg_begin = 0, msg_count = 10)
|
126
|
-
|
127
|
-
|
128
|
-
|
143
|
+
url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?type=10&action=list'\
|
144
|
+
"&begin=#{msg_begin}&count=#{msg_count}&f=json&token=#{@token}"\
|
145
|
+
"&lang=zh_CN&token=#{@token}&lang=zh_CN&f=json&ajax=1"\
|
146
|
+
"&random=#{rand}"
|
147
|
+
msg_json = RestClient.get url, cookies: @cookies
|
148
|
+
JSON.parse msg_json.to_s
|
129
149
|
end
|
130
150
|
|
131
151
|
# 轮训新消息条数
|
132
152
|
def get_new_msg_num(last_msg_id)
|
133
|
-
uri =
|
153
|
+
uri = 'cgi-bin/getnewmsgnum?f=json&t=ajax-getmsgnum'\
|
154
|
+
"&lastmsgid=#{last_msg_id}&token=#{@token}&lang=zh_CN"
|
134
155
|
post_params = {
|
135
156
|
ajax: 1,
|
136
157
|
f: 'json',
|
@@ -139,16 +160,19 @@ module WxExt
|
|
139
160
|
token: @token
|
140
161
|
}
|
141
162
|
post_headers = {
|
142
|
-
|
163
|
+
referer: 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list'\
|
164
|
+
"&count=20&day=7&token=#{@token}&lang=zh_CN"
|
143
165
|
}
|
144
|
-
resource = RestClient::Resource.new(@home_url, :
|
166
|
+
resource = RestClient::Resource.new(@home_url, headers: post_headers,
|
167
|
+
cookies: @cookies)
|
145
168
|
res = resource[uri].post post_params
|
146
|
-
|
169
|
+
JSON.parse res.to_s
|
147
170
|
end
|
148
171
|
|
149
172
|
# 获取联系人信息
|
150
173
|
def get_contact_info(fakeid)
|
151
|
-
uri =
|
174
|
+
uri = 'cgi-bin/getcontactinfo?t=ajax-getcontactinfo'\
|
175
|
+
"&lang=zh_CN&fakeid=#{fakeid}"
|
152
176
|
post_params = {
|
153
177
|
ajax: 1,
|
154
178
|
f: 'json',
|
@@ -157,43 +181,57 @@ module WxExt
|
|
157
181
|
token: @token
|
158
182
|
}
|
159
183
|
post_headers = {
|
160
|
-
|
184
|
+
referer: 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index'\
|
185
|
+
"&pagesize=10&pageidx=0&type=0&token=#{@token}&lang=zh_CN"
|
161
186
|
}
|
162
|
-
resource = RestClient::Resource.new(@home_url, :
|
187
|
+
resource = RestClient::Resource.new(@home_url, headers: post_headers,
|
188
|
+
cookies: @cookies)
|
163
189
|
res = resource[uri].post post_params
|
164
|
-
|
190
|
+
JSON.parse res.to_s
|
165
191
|
end
|
166
192
|
|
167
193
|
# 获取国家列表
|
168
194
|
def get_country_list
|
169
|
-
url =
|
195
|
+
url = 'https://mp.weixin.qq.com/cgi-bin/getregions'\
|
196
|
+
"?t=setting/ajax-getregions&id=0&token=#{@token}&lang=zh_CN"\
|
197
|
+
"&token=#{@token}&lang=zh_CN&f=json&ajax=1&random=#{rand}"
|
170
198
|
resource = RestClient::Resource.new(url, cookies: @cookies)
|
171
199
|
res = resource.get
|
172
|
-
|
200
|
+
JSON.parse res.to_s
|
173
201
|
end
|
174
202
|
|
175
203
|
# 获取每日可推送消息数
|
176
204
|
def get_day_msg_count
|
177
|
-
url =
|
178
|
-
|
205
|
+
url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
|
206
|
+
"?t=mass/send&token=#{@token}&lang=zh_CN"
|
207
|
+
res = RestClient.get(url, cookies: @cookies)
|
179
208
|
day_msg_count = 0
|
180
|
-
|
181
|
-
if
|
182
|
-
day_msg_count = $1
|
183
|
-
end
|
209
|
+
reg = /.*mass_send_left\s*:\s*can_verify_apply\s*\?\s*\'(\d*)\'\*/
|
210
|
+
day_msg_count = $1 if reg =~ res.to_s
|
184
211
|
day_msg_count.to_i
|
185
212
|
end
|
186
213
|
|
187
|
-
#
|
188
|
-
|
189
|
-
|
190
|
-
|
214
|
+
# 获取 last_msg_id 和 msg_item
|
215
|
+
def get_msg_item
|
216
|
+
url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20'\
|
217
|
+
"&day=7&token=#{@token}&lang=zh_CN"
|
191
218
|
resource = RestClient::Resource.new(url, cookies: @cookies)
|
192
219
|
res = resource.get
|
193
|
-
|
194
|
-
|
195
|
-
|
220
|
+
reg = /.*total_count\s*:\s*(\d*).*latest_msg_id\s*:\s*\'(\d*)\'.*list\s*:\s*\((.*)\)\.msg_item,.*/m
|
221
|
+
return_hash = {
|
222
|
+
status: -1,
|
223
|
+
msg: 'system_error'
|
224
|
+
}
|
225
|
+
if reg =~ res.to_s
|
226
|
+
return_hash = {
|
227
|
+
status: 0,
|
228
|
+
msg: 'ok',
|
229
|
+
total_count: $1,
|
230
|
+
latest_msg_id: $2,
|
231
|
+
msg_item: JSON.parse($3)['msg_item']
|
232
|
+
}
|
196
233
|
end
|
234
|
+
return_hash
|
197
235
|
end
|
198
236
|
|
199
237
|
# https://mp.weixin.qq.com/cgi-bin/message?t=message/list&token=1664040225&count=20&day=7
|
@@ -208,19 +246,21 @@ module WxExt
|
|
208
246
|
end
|
209
247
|
|
210
248
|
private
|
249
|
+
|
211
250
|
def set_cookie(page, k, v)
|
212
251
|
case Capybara.current_session.driver
|
213
252
|
when Capybara::Poltergeist::Driver
|
214
|
-
page.driver.set_cookie(k,v)
|
253
|
+
page.driver.set_cookie(k, v)
|
215
254
|
when Capybara::RackTest::Driver
|
216
255
|
headers = {}
|
217
|
-
Rack::Utils.set_cookie_header!(headers,k,v)
|
256
|
+
Rack::Utils.set_cookie_header!(headers, k, v)
|
218
257
|
cookie_string = headers['Set-Cookie']
|
219
258
|
Capybara.current_session.driver.browser.set_cookie(cookie_string)
|
220
259
|
when Capybara::Selenium::Driver
|
221
|
-
page.driver.browser.manage.add_cookie(:
|
260
|
+
page.driver.browser.manage.add_cookie(name: k, value: v)
|
222
261
|
else
|
223
|
-
|
262
|
+
fail 'no cookie-setter implemented for driver '\
|
263
|
+
"#{Capybara.current_session.driver.class.name}"
|
224
264
|
end
|
225
265
|
end
|
226
266
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/wx_ext/weixin_spec.rb
CHANGED
@@ -3,10 +3,16 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe WxExt::WeiXin do
|
5
5
|
before(:all) do
|
6
|
-
@weixin = WxExt::WeiXin.new 'flowerwrong
|
6
|
+
@weixin = WxExt::WeiXin.new 'flowerwrong', '11111111'
|
7
7
|
end
|
8
8
|
|
9
|
-
it '
|
9
|
+
it 'should login to the mp' do
|
10
|
+
res_hash = @weixin.login
|
11
|
+
expect(res_hash[:status]).to eql(0)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should init method should init all params' do
|
15
|
+
res_hash = @weixin.login
|
10
16
|
flag = @weixin.init
|
11
17
|
if flag
|
12
18
|
token = @weixin.token
|
@@ -15,29 +21,36 @@ describe WxExt::WeiXin do
|
|
15
21
|
end
|
16
22
|
end
|
17
23
|
|
18
|
-
it 'should get day msg count' do
|
19
|
-
flag = @weixin.init
|
20
|
-
if flag
|
21
|
-
day_msg_count = @weixin.get_day_msg_count
|
22
|
-
expect(day_msg_count.to_s).to match(/\d*/)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# 未完待续
|
27
24
|
it 'should get fakeids and msg ids' do
|
25
|
+
res_hash = @weixin.login
|
28
26
|
flag = @weixin.init
|
29
|
-
|
30
|
-
puts
|
27
|
+
res_hash = @weixin.get_msg_item if flag
|
28
|
+
puts '==' * 20
|
29
|
+
puts res_hash
|
30
|
+
expect(res_hash[:status]).to eql(0)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should get new msg num" do
|
34
|
+
res_hash = @weixin.login
|
34
35
|
flag = @weixin.init
|
35
|
-
|
36
|
+
msg_res_hash = @weixin.get_msg_item if flag
|
37
|
+
res_hash = @weixin.get_new_msg_num(msg_res_hash[:latest_msg_id=])
|
38
|
+
puts '==' * 20
|
36
39
|
puts res_hash
|
37
40
|
expect(res_hash['ret'].to_s).to eql('0')
|
38
41
|
end
|
39
42
|
|
43
|
+
it 'should get day msg count' do
|
44
|
+
res_hash = @weixin.login
|
45
|
+
flag = @weixin.init
|
46
|
+
if flag
|
47
|
+
day_msg_count = @weixin.get_day_msg_count
|
48
|
+
expect(day_msg_count.to_s).to match(/\d*/)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
40
52
|
it "should get contact info" do
|
53
|
+
res_hash = @weixin.login
|
41
54
|
flag = @weixin.init
|
42
55
|
res_hash = @weixin.get_contact_info('204060720')
|
43
56
|
puts res_hash
|
@@ -45,13 +58,15 @@ describe WxExt::WeiXin do
|
|
45
58
|
end
|
46
59
|
|
47
60
|
it "should return a country list" do
|
61
|
+
res_hash = @weixin.login
|
48
62
|
flag = @weixin.init
|
49
63
|
res_hash = @weixin.get_country_list
|
50
64
|
puts res_hash
|
51
65
|
expect(res_hash["num"].to_s).to match(/\d+/)
|
52
66
|
end
|
53
67
|
|
54
|
-
it
|
68
|
+
it 'should upload_file method should return a right hash' do
|
69
|
+
res_hash = @weixin.login
|
55
70
|
flag = @weixin.init
|
56
71
|
puts @weixin.token
|
57
72
|
puts @weixin.ticket_id
|
@@ -63,7 +78,8 @@ describe WxExt::WeiXin do
|
|
63
78
|
expect(file_hash["base_resp"]["ret"].to_s).to eql("0")
|
64
79
|
end
|
65
80
|
|
66
|
-
it "upload_single_msg method should upload a msg to sucaizhongxin" do
|
81
|
+
it "should upload_single_msg method should upload a msg to sucaizhongxin" do
|
82
|
+
res_hash = @weixin.login
|
67
83
|
flag = @weixin.init
|
68
84
|
file = File.new("/home/yang/dev/ruby/gem/hack_wx/spec/hack_wx/test_spec.jpg", 'rb')
|
69
85
|
file_hash = @weixin.upload_file(file, "test_spec.jpg")
|
@@ -91,7 +107,8 @@ describe WxExt::WeiXin do
|
|
91
107
|
expect(msg_hash["ret"].to_s).to eql("0")
|
92
108
|
end
|
93
109
|
|
94
|
-
it "upload_multi_msg method should upload multi msg to sucaizhongxin" do
|
110
|
+
it "should upload_multi_msg method should upload multi msg to sucaizhongxin" do
|
111
|
+
res_hash = @weixin.login
|
95
112
|
flag = @weixin.init
|
96
113
|
file = File.new("/home/yang/dev/ruby/gem/hack_wx/spec/hack_wx/test_spec.jpg", 'rb')
|
97
114
|
file_hash = @weixin.upload_file(file, "test_spec.jpg")
|
@@ -126,7 +143,8 @@ describe WxExt::WeiXin do
|
|
126
143
|
expect(msg_hash["ret"].to_s).to eql("0")
|
127
144
|
end
|
128
145
|
|
129
|
-
it "broadcast msg to all user
|
146
|
+
it "should broadcast msg to all user" do
|
147
|
+
res_hash = @weixin.login
|
130
148
|
flag = @weixin.init
|
131
149
|
|
132
150
|
msg_hash = @weixin.get_app_msg_list
|
@@ -135,8 +153,8 @@ describe WxExt::WeiXin do
|
|
135
153
|
|
136
154
|
msg_params = {
|
137
155
|
ajax: 1,
|
138
|
-
appmsgid: app_msg_id,
|
139
|
-
cardlimit: 1,
|
156
|
+
appmsgid: app_msg_id,
|
157
|
+
cardlimit: 1,
|
140
158
|
city: '',
|
141
159
|
country: '',
|
142
160
|
province: '',
|
@@ -144,7 +162,7 @@ describe WxExt::WeiXin do
|
|
144
162
|
groupid: '-1',
|
145
163
|
imgcode: '',
|
146
164
|
lang: 'zh_CN',
|
147
|
-
operation_seq: @weixin.operation_seq,
|
165
|
+
operation_seq: @weixin.operation_seq,
|
148
166
|
random: rand,
|
149
167
|
sex: 0,
|
150
168
|
synctxweibo: 0,
|
@@ -158,7 +176,8 @@ describe WxExt::WeiXin do
|
|
158
176
|
expect(msg_hash["ret"].to_s).to eql("0")
|
159
177
|
end
|
160
178
|
|
161
|
-
it
|
179
|
+
it 'should get app msg list with json' do
|
180
|
+
res_hash = @weixin.login
|
162
181
|
flag = @weixin.init
|
163
182
|
|
164
183
|
msg_hash = @weixin.get_app_msg_list
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wx_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- flowerwrong
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|