Class: WxExt::WeiXin

Inherits:
Object
  • Object
show all
Defined in:
lib/wx_ext/wei_xin.rb

Overview

weixin extention of mp.weixin.qq.com

Author:

  • FuShengYang

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, password) ⇒ WeiXin

Returns a new instance of WeiXin.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wx_ext/wei_xin.rb', line 17

def initialize(, password)
  @account = 
  @password = password
  @home_url = 'https://mp.weixin.qq.com'
  @token = nil
  @ticket = nil
  @user_name = nil
  @ticket_id = nil
  @cookies = {}
  @operation_seq = ''
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def 
  @account
end

#cookiesObject

Returns the value of attribute cookies.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def cookies
  @cookies
end

#home_urlObject

Returns the value of attribute home_url.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def home_url
  @home_url
end

#operation_seqObject

Returns the value of attribute operation_seq.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def operation_seq
  @operation_seq
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def password
  @password
end

#ticketObject

Returns the value of attribute ticket.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def ticket
  @ticket
end

#ticket_idObject

Returns the value of attribute ticket_id.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def ticket_id
  @ticket_id
end

#tokenObject

Returns the value of attribute token.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def token
  @token
end

#user_nameObject

Returns the value of attribute user_name.



14
15
16
# File 'lib/wx_ext/wei_xin.rb', line 14

def user_name
  @user_name
end

Instance Method Details

#broadcast_msg(msg_params) ⇒ Hash

Broadcast news to mp.weixin.qq.com.

Parameters:

  • msg_params (Hash)

Returns:

  • (Hash)

    A json parse hash.



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/wx_ext/wei_xin.rb', line 196

def broadcast_msg(msg_params)
  uri = "cgi-bin/masssend?t=ajax-response&token=#{@token}&lang=zh_CN"
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
             "?t=mass/send&token=#{token}&lang=zh_CN",
    host: 'mp.weixin.qq.com'
  }
  resource = RestClient::Resource.new(@home_url, headers: headers,
                                                 cookies: @cookies)
  post_msg_res = resource[uri].post msg_params
  JSON.parse post_msg_res.to_s
end

#collect_msg(msgid) ⇒ Hash

Collect msg of user.

Parameters:

  • msgid (String)

Returns:

  • (Hash)

    A json parse hash.



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/wx_ext/wei_xin.rb', line 401

def collect_msg(msgid)
  uri = "cgi-bin/setstarmessage?t=ajax-setstarmessage&token=#{ @token }&lang=zh_CN"
  params = {
    ajax: 1,
    f: 'json',
    lang: 'zh_CN',
    msgid: msgid,
    random: rand,
    token: @token,
    value: 1
  }
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
             "?t=message/list&token=#{ @token }&count=20&day=7"
  }
  resource = RestClient::Resource.new(@home_url, headers: headers,
                                                 cookies: @cookies)
  res = resource[uri].post params
  JSON.parse res.to_s
end

#get_app_msg_list(msg_begin = 0, msg_count = 10) ⇒ Hash

Get all news.

Parameters:

  • msg_begin (Integer) (defaults to: 0)
  • msg_count (Integer) (defaults to: 10)

Returns:

  • (Hash)

    A json parse hash.



214
215
216
217
218
219
220
221
# File 'lib/wx_ext/wei_xin.rb', line 214

def get_app_msg_list(msg_begin = 0, msg_count = 10)
  url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?type=10&action=list'\
        "&begin=#{msg_begin}&count=#{msg_count}&f=json&token=#{@token}"\
        "&lang=zh_CN&token=#{@token}&lang=zh_CN&f=json&ajax=1"\
        "&random=#{rand}"
  msg_json = RestClient.get url, cookies: @cookies
  JSON.parse msg_json.to_s
end

#get_contact_info(fakeid) ⇒ Hash

Get user info.

Parameters:

  • fakeid (String)

Returns:

  • (Hash)

    A json parse hash.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/wx_ext/wei_xin.rb', line 251

def get_contact_info(fakeid)
  uri = 'cgi-bin/getcontactinfo?t=ajax-getcontactinfo'\
        "&lang=zh_CN&fakeid=#{fakeid}"
  post_params = {
    ajax: 1,
    f: 'json',
    lang: 'zh_CN',
    random: rand,
    token: @token
  }
  post_headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index'\
             "&pagesize=10&pageidx=0&type=0&token=#{@token}&lang=zh_CN"
  }
  resource = RestClient::Resource.new(@home_url, headers: post_headers,
                                                 cookies: @cookies)
  res = resource[uri].post post_params
  JSON.parse res.to_s
end

#get_country_listHash

Get country list.

Returns:

  • (Hash)

    A json parse hash.



274
275
276
277
278
279
280
281
# File 'lib/wx_ext/wei_xin.rb', line 274

def get_country_list
  url = 'https://mp.weixin.qq.com/cgi-bin/getregions'\
        "?t=setting/ajax-getregions&id=0&token=#{@token}&lang=zh_CN"\
        "&token=#{@token}&lang=zh_CN&f=json&ajax=1&random=#{rand}"
  resource = RestClient::Resource.new(url, cookies: @cookies)
  res = resource.get
  JSON.parse res.to_s
end

#get_day_msg_countInteger

Get this weixin can broadcast news count.

Returns:

  • (Integer)

    day msg count.



286
287
288
289
290
291
292
293
294
# File 'lib/wx_ext/wei_xin.rb', line 286

def get_day_msg_count
  url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
        "?t=mass/send&token=#{@token}&lang=zh_CN"
  res = RestClient.get(url, cookies: @cookies)
  day_msg_count = 0
  reg = /.*mass_send_left\s*:\s*can_verify_apply\s*\?\s*\'(\d*)\'\*/
  day_msg_count = $1 if reg =~ res.to_s
  day_msg_count.to_i
end

#get_fans_countHash

Get fans count.

Returns:

  • (Hash)

    Fans count with friends_list, group_list etc.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/wx_ext/wei_xin.rb', line 339

def get_fans_count
  url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index'\
  "&pagesize=10&pageidx=0&type=0&token=#{ @token }&lang=zh_CN"
  res = RestClient.get(url, cookies: @cookies)
  reg = /.*pageIdx\s*:\s*(\d*).*pageCount\s*:\s*(\d*).*pageSize\s*:\s*(\d*).*groupsList\s*:\s*\((.*)\)\.groups,.*friendsList\s*:\s*\((.*)\)\.contacts,.*totalCount\s*:\s*\'(\d*)\'\s*\*\s*.*/m
  return_hash = {
    status: -1,
    msg: 'system_error'
  }
  if reg =~ res.to_s
    return_hash = {
      status: 0,
      msg: 'ok',
      page_index: $1,
      page_count: $2,
      page_size: $3,
      group_list: JSON.parse($4)['groups'],
      friends_list: JSON.parse($5)['contacts'],
      total_count: $6
    }
  end
  return_hash
end

#get_msg_items(count = 20, day = 7, filterivrmsg = 1, action = '', keyword = '', frommsgid = '', offset = '') ⇒ Hash

Get msg items.

Parameters:

  • count (Integer) (defaults to: 20)
  • day (Integer) (defaults to: 7)
  • filterivrmsg (Integer) (defaults to: 1)
  • action (String) (defaults to: '')
  • keyword (String) (defaults to: '')
  • offset (String) (defaults to: '')

Returns:

  • (Hash)

    A json parse hash.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/wx_ext/wei_xin.rb', line 305

def get_msg_items(count = 20, day = 7, filterivrmsg = 1, action='', keyword='', frommsgid='', offset='')
  url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list'\
        "&action=#{action}&keyword=#{keyword}&frommsgid=#{frommsgid}&offset=#{offset}&count=#{count}"\
        "&day=#{day}filterivrmsg=#{filterivrmsg}&token=#{@token}&lang=zh_CN"
  resource = RestClient::Resource.new(url, cookies: @cookies)
  res = resource.get
  reg = /.*total_count\s*:\s*(\d*).*latest_msg_id\s*:\s*\'(\d*)\'.*list\s*:\s*\((.*)\)\.msg_item,.*filterivrmsg\s*:\s*\"(\d*)\".*/m
  return_hash = {
    status: -1,
    msg: 'system_error'
  }
  if reg =~ res.to_s
    return_hash = {
      status: 0,
      msg: 'ok',
      total_count: $1,
      latest_msg_id: $2,
      count: 20,
      day: 7,
      frommsgid: '',
      can_search_msg: '1',
      offset: '',
      action: '',
      keyword: '',
      msg_items: JSON.parse($3)['msg_item'],
      filterivrmsg: $4
    }
  end
  return_hash
end

#get_new_msg_num(last_msg_id) ⇒ Hash

Get new coming msgs from user.

Parameters:

  • last_msg_id (String)

Returns:

  • (Hash)

    A json parse hash.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/wx_ext/wei_xin.rb', line 227

def get_new_msg_num(last_msg_id)
  uri = 'cgi-bin/getnewmsgnum?f=json&t=ajax-getmsgnum'\
        "&lastmsgid=#{last_msg_id}&token=#{@token}&lang=zh_CN"
  post_params = {
    ajax: 1,
    f: 'json',
    lang: 'zh_CN',
    random: rand,
    token: @token
  }
  post_headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list'\
             "&count=20&day=7&token=#{@token}&lang=zh_CN"
  }
  resource = RestClient::Resource.new(@home_url, headers: post_headers,
                                                 cookies: @cookies)
  res = resource[uri].post post_params
  JSON.parse res.to_s
end

#initBoolean

Init ticket, cookies, operation_seq, user_name etc.

Returns:

  • (Boolean)

    Init ticket, cookies, operation_seq, user_name true or false.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wx_ext/wei_xin.rb', line 88

def init
  home_url = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token=#{@token}"
  headers = {
    host: 'mp.weixin.qq.com',
    referer: 'https://mp.weixin.qq.com/'
  }

  @cookies = {
    data_bizuin: URI.unescape(cookies['data_bizuin']),
    data_ticket: URI.unescape(cookies['data_ticket']),
    slave_user: URI.unescape(cookies['slave_user']),
    slave_sid: URI.unescape(cookies['slave_sid']),
    bizuin: URI.unescape(cookies['bizuin'])
  }

  msg_send_url = 'https://mp.weixin.qq.com/cgi-bin/masssendpage'\
                 "?t=mass/send&token=#{@token}&lang=zh_CN"
  msg_send_page = RestClient.get msg_send_url, cookies: @cookies

  ticket_reg = /.*ticket\s*:\s*\"(\w+)\".*user_name\s*:\s*\"(.*)\",.*nick_name\s*:\s*\"(.*)\".*/m
  operation_seq_reg = /.*operation_seq\s*:\s*\"(\d+)\".*/
  @operation_seq = $1 if operation_seq_reg =~ msg_send_page.to_s
  if ticket_reg =~ msg_send_page.to_s
    @ticket = $1
    @user_name = @ticket_id= $2
    true
  else
    false
  end
end

#loginHash

模拟登陆微信公众平台, 初始化 access_token, cookies

Returns:

  • (Hash)

    Hash with login status and msg.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wx_ext/wei_xin.rb', line 32

def 
  password = Digest::MD5.hexdigest @password
   = {
    referer: 'https://mp.weixin.qq.com/'
  }
   = {
    username: @account,
    pwd: password,
    imgcode: '',
    f: 'json'
  }

  resource = RestClient::Resource.new(@home_url, headers: )
  res = resource['cgi-bin/login'].post 
  @cookies = res.cookies
  return_hash = {
    status: 0,
    msg: 'ok'
  }

  # 0: "ok", "redirect_url": ""
  # -8: "need verify code"
  # -23: "acct\/password error"
  # -21: "user not exist"
  res_hash = JSON.parse res.to_s
  sta = res_hash['base_resp']['ret'].to_s
  if sta == '0'
    token_reg = /.*token=(\d+)\".*/
    @token = $1 if token_reg =~ res.to_s
  elsif sta == '-8'
    return_hash = {
      status: -8,
      msg: 'need_varify_code'
    }
  elsif sta == '-23'
    return_hash = {
      status: -23,
      msg: 'password_error'
    }
  elsif sta == '-21'
    return_hash = {
      status: -21,
      msg: 'user_not_exist'
    }
  else
    return_hash = {
      status: -1,
      msg: 'system_error'
    }
  end
  return_hash
end

#preview_msg(msg_params_with_name) ⇒ Hash

Preview broadcast news to user. system error: no data_bizuin and data_ticket in cookie

Parameters:

  • msg_params_with_name (Hash)

Returns:

  • (Hash)

    A json parse hash.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/wx_ext/wei_xin.rb', line 176

def preview_msg(msg_params_with_name)
  uri = 'cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview'\
        "&type=10&token=#{@token}&lang=zh_CN"
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit'\
             "&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN&token=#{@token}",
    host: 'mp.weixin.qq.com',
    x_requested_with: 'XMLHttpRequest'
  }
  resource = RestClient::Resource.new(@home_url, headers: headers, cookies: @cookies)

  res = resource[uri].post msg_params_with_name
  # "ret":"0", "msg":"preview send success!", "appMsgId":"201796045", "fakeid":""
  JSON.parse res.to_s
end

#quick_reply(content, quickreplyid, tofakeid) ⇒ Hash

Quick reply to user.

Parameters:

  • content (String)
  • quickreplyid (String)
  • tofakeid (String)

Returns:

  • (Hash)

    A json parse hash.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/wx_ext/wei_xin.rb', line 369

def quick_reply(content, quickreplyid, tofakeid)
  post_uri = 'cgi-bin/singlesend'\
             "?t=ajax-response&f=json&token=#{ @token }&lang=zh_CN"
  params = {
    ajax: 1,
    content: content,
    f: 'json',
    imgcode: '',
    lang: 'zh_CN',
    mask: false,
    quickreplyid: quickreplyid,
    random: rand,
    tofakeid: tofakeid,
    token: @token,
    type: 1
  }
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
             "?t=message/list&count=20&day=7&token=#{ @token }&lang=zh_CN"
  }
  resource = RestClient::Resource.new(@home_url, headers: headers,
                                                 cookies: @cookies)
  res = resource[post_uri].post params
  #
  # 10706: "customer block": "48小时内的才行"
  JSON.parse res.to_s
end

#un_collect_msg(msgid) ⇒ Hash

Un collect msg of user.

Parameters:

  • msgid (String)

Returns:

  • (Hash)

    A json parse hash.



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/wx_ext/wei_xin.rb', line 426

def un_collect_msg(msgid)
  uri = "cgi-bin/setstarmessage?t=ajax-setstarmessage&token=#{ @token }&lang=zh_CN"
  params = {
    ajax: 1,
    f: 'json',
    lang: 'zh_CN',
    msgid: msgid,
    random: rand,
    token: @token,
    value: 0
  }
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/message'\
             "?t=message/list&token=#{ @token }&count=20&day=7"
  }
  resource = RestClient::Resource.new(@home_url, headers: headers,
                                                 cookies: @cookies)
  res = resource[uri].post params
  JSON.parse res.to_s
end

#upload_file(file, file_name, folder = '/cgi-bin/uploads') ⇒ Hash

Upload file to file.weixin.qq.com

Parameters:

  • file (File)
  • file_name (String)
  • folder (String) (defaults to: '/cgi-bin/uploads')

Returns:

  • (Hash)

    A json parse hash.



125
126
127
128
129
130
131
132
133
134
# File 'lib/wx_ext/wei_xin.rb', line 125

def upload_file(file, file_name, folder = '/cgi-bin/uploads')
  upload_url = 'https://mp.weixin.qq.com/cgi-bin/filetransfer'\
               '?action=upload_material&f=json&writetype=doublewrite'\
               "&groupid=1&ticket_id=#{@ticket_id}"\
               "&ticket=#{@ticket}&token=#{@token}&lang=zh_CN"
  response = RestClient.post upload_url, file: file,
                                         Filename: file_name,
                                         folder: folder
  JSON.parse response.to_s
end

#upload_multi_msg(msg_params) ⇒ Hash

Upload multi news to mp.weixin.qq.com

Parameters:

  • msg_params (Hash)

Returns:

  • (Hash)

    A json parse hash.



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/wx_ext/wei_xin.rb', line 158

def upload_multi_msg(msg_params)
  uri = 'cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=10'\
  "&token=#{@token}&lang=zh_CN"
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg'\
    '?t=media/appmsg_edit&action=edit&type=10'\
    "&isMul=1&isNew=1&lang=zh_CN&token=#{@token}"
  }
  resource = RestClient::Resource.new(@home_url, headers: headers, cookies: @cookies)
  post_msg_res = resource[uri].post msg_params
  JSON.parse post_msg_res.to_s
end

#upload_single_msg(single_msg_params) ⇒ Hash

Upload single news to mp.weixin.qq.com

Parameters:

  • single_msg_params (Hash)

Returns:

  • (Hash)

    A json parse hash.



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/wx_ext/wei_xin.rb', line 140

def upload_single_msg(single_msg_params)
  post_single_msg_uri = 'cgi-bin/operate_appmsg'\
                        '?t=ajax-response&sub=create&type=10&token'\
                        "=#{@token}&lang=zh_CN"
  headers = {
    referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit'\
             '&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN'\
             "&token=#{@token}"
  }
  resource = RestClient::Resource.new(@home_url, headers: headers, cookies: @cookies)
  res = resource[post_single_msg_uri].post single_msg_params
  JSON.parse res.to_s
end