Class: WeixinPublic::WeixinPubClient

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

Instance Method Summary collapse

Constructor Details

#initialize(username, password, cert = nil, sig = nil) ⇒ WeixinPubClient

Returns a new instance of WeixinPubClient.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/weixin_public.rb', line 40

def initialize(username,password,cert=nil,sig=nil)
  @username=username
  @password=password
  @sig = sig
  @cert = cert
  @conn = Faraday.new(:url => 'https://mp.weixin.qq.com')
  @conn_multipart = Faraday.new(:url => 'https://mp.weixin.qq.com') do |faraday|
    faraday.request :multipart
    faraday.adapter :net_http      
  end
end

Instance Method Details

#avatar_download(file, url) ⇒ Object



190
191
192
193
194
# File 'lib/weixin_public.rb', line 190

def avatar_download(file,url)
  puts "download-#{url}"
  ret = request(:get,url,{},nil)
  File.open(file, 'wb') { |fp| fp.write(ret.body) }
end

#avatar_upload(avatar, type = "image/png") ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/weixin_public.rb', line 155

def avatar_upload(avatar,type="image/png")
  return nil if !@cookie && (@username,@password) =~ /failed/
  payload = { :uploadFile => Faraday::UploadIO.new(avatar, type)}
  @conn_multipart.headers["Cookie"] = @cookie
  @conn_multipart.headers["Referer"]="http://mp.weixin.qq.com/cgi-bin/indexpage"
  ret=@conn_multipart.post "/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token=#{@token}&t=iframe-uploadfile&lang=zh_CN&formId=null&f=json",payload
  return ret.body.to_s[/\'.*?\'/m][1..-2] if ret.body =~ /suc/m
  return nil
end

#create_appmsg(title, avatar, content, desc = "") ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/weixin_public.rb', line 165

def create_appmsg(title,avatar,content,desc="")
  return nil if !@cookie && (@username,@password) =~ /failed/
  if avatar =~ /^http/ then
  end
  fileid = avatar_upload(avatar)
  return nil if !fileid
  params = {:error=>false,:count=>1,:title0=>title,:digest0=>desc,:content0=>content,:fileid0=>fileid,:ajax=>1,:sub=>"create"} 
  ret = request(:post,"/cgi-bin/operate_appmsg?t=ajax-response&lang=zh_CN",params,"https://mp.weixin.qq.com/cgi-bin/operate_appmsg")
  print ret.body
  lst=get_appmsg_lst
  return lst[0].appId if lst
  return nil
end

#download_file(message) ⇒ Object



131
132
133
134
# File 'lib/weixin_public.rb', line 131

def download_file(message)
  url="/cgi-bin/downloadfile?msgid=#{message.id}&source="
  avatar_download(message.filePath,url)
end

#get_appmsg_lstObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/weixin_public.rb', line 179

def get_appmsg_lst
  return nil if !@cookie && (@username,@password) =~ /failed/
  ret = request(:get,"/cgi-bin/operate_appmsg?sub=list&t=wxm-appmsgs-list-new&type=10&pageIdx=0&pagesize=10&subtype=3&f=json",{},nil)
  doc = Nokogiri::HTML(ret.body)
  posts = doc.css('#json-msglist').to_s
  posts = JSON.parse(posts[posts.index(/{/)..posts.rindex(/}/)])["list"]
  ret = []
  posts.each {|po| ret << AppMsg.new(po)}
  ret
end

#get_fansObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/weixin_public.rb', line 70

def get_fans
  return if !@cookie && (@username,@password) =~ /failed/
  ret = []
  for i in 0..100 do
    res = request(:get,"/cgi-bin/contactmanagepage?t=wxm-friend&lang=zh_CN&pagesize=&pageidx=0&type=0&groupid=0&pageidx=#{i}",{},nil)
    doc = Nokogiri::HTML(res.body)
    fans = JSON.parse(doc.css('#json-friendList').to_s[/\[.*?\]/m])
    break if fans.length == 0
    fans.each { |f| ret<< WeixinFan.new(f) }
  end
  ret
end

#get_messages(fakeId, download = false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/weixin_public.rb', line 83

def get_messages(fakeId,download=false)
  return if !@cookie && (@username,@password) =~ /failed/
  #doc.force_encoding('gbk')
  #doc.encode!("utf-8", :undef => :replace, :invalid => :replace)
  doc = Nokogiri::HTML(request(:get,"/cgi-bin/singlemsgpage?fromfakeid=#{fakeId}&msgid=&source=&count=20&t=wxm-singlechat&f=json",{},"https://mp.weixin.qq.com/cgi-bin/getmessage").body) 
  messages = doc.css('#json-msgList').to_s.encode("UTF-8", "UTF-8",:invalid => :replace)
  messages = JSON.parse(messages[messages.index(/\[/)..messages.rindex(/\]/)])
  ret = []
  messages.each do |m| 
    next if m["type"]=="10" || m["fakeId"]!=fakeId
    if m["type"]!="10" then
      ret << WeixinMessage.new(m)
      ret[-1].dateTime = Time.at(m["dateTime"].to_i)
    end
    if download then
      download_file(ret[-1])
    end
  end
  ret
end

#get_messages_number_new(lastmsgid) ⇒ Object



104
105
106
107
108
# File 'lib/weixin_public.rb', line 104

def get_messages_number_new(lastmsgid)
  return if !@cookie && (@username,@password) =~ /failed/
  res = request(:post,"/cgi-bin/getnewmsgnum?t=ajax-getmsgnum&lastmsgid=#{lastmsgid}")
  ret = JSON.parse(res.body)["newTotalMsgCount"]
end

#get_messages_today(lastmsgid = 0, step = 50) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/weixin_public.rb', line 110

def get_messages_today(lastmsgid=0,step=50)
  return if !@cookie && (@username,@password) =~ /failed/
  offset = 0
  ret = []
  loop do
    params = {"t"=>"ajax-message","lang"=>"zh_CN","count"=>'50',"timeline"=>'1','day'=>'0','cgi'=>'getmessage','offset'=>offset} 
    res = request(:post,"/cgi-bin/getmessage",params,nil)
    messages = JSON.parse(res.body)
    n = 0
    messages.each do |m| 
      break if m["id"].to_i <= lastmsgid
      ret << WeixinMessage.new(m)
        ret[-1].dateTime = Time.at(m["dateTime"].to_i)
        n = n+1
    end
    break if n < step
    offset = offset + step
  end
  ret
end

#login(username, pwd) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/weixin_public.rb', line 53

def (username,pwd)
  puts "login with#{username}-#{pwd}"
  pwd = Digest::MD5.hexdigest(pwd)
  @cookie = @cert?"cert=#{@cert}":""
  @cookie = "#{@cookie};sig=#{@sig}"if @sig
  params = {"username"=>username,"pwd"=>pwd,"imgcode"=>'',"f"=>'json'} 
  ret = request(:post,'/cgi-bin/login?lang=zh_CN',params,"https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN")
  return 'login failed' if !ret.headers["set-cookie"] 
  ret.headers["set-cookie"].split(',').each do |c|
    @cookie << c.split(';')[0] <<";"
  end
  msg = JSON.parse(ret.body)["ErrMsg"]
  @token = msg[msg =~ /token/..-1].split('=')[1]
  ret = request(:get,"/cgi-bin/indexpage?token=#{@token}&t=wxm-index&lang=zh_CN",{"f"=>'json'},nil)
  return ret.status.to_s
end

#operadvancedfunc(type = 1, on_off = 0) ⇒ Object

type:1 -edit mode 2 -dev mode 4 -auto-reply



197
198
199
200
# File 'lib/weixin_public.rb', line 197

def operadvancedfunc(type=1,on_off = 0)
  return nil if !@cookie && (@username,@password) =~ /failed/
  ret = request(:post,"/cgi-bin/operadvancedfunc?op=switch&lang=zh_CN",{:flag=>on_off,:type=>type,:ajax=>1},nil)
end

#request(method, url, params, referer) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/weixin_public.rb', line 213

def request(method,url,params,referer)
  @conn.headers["Cookie"] = @cookie
  @conn.headers["Referer"] = referer if referer
  if method == :post then
    ret = @conn.post do |req|
      req.url url
      req.body = params
      req.body['token']=@token if @token
      p req.headers["Content-Length"]
    end
  else
    ret = @conn.get do |req|
      req.url url
      @conn.params['token']=@token
      #@conn.params['lang']="zh_CN"
    end
  end
  ret
end

#send_appmsg(appmsgid, reciever) ⇒ Object



148
149
150
151
152
# File 'lib/weixin_public.rb', line 148

def send_appmsg(appmsgid,reciever)
  return if !@cookie && (@username,@password) =~ /failed/
  body = {"error"=>false,"ajax"=>1,"f"=>"json","tofakeid"=>reciever,"fid"=>appmsgid,"appmsgid"=>appmsgid,"type"=>10}
  ret = request(:post,"/cgi-bin/singlesend?t=ajax-response&lang=zh_CN",body,"https://mp.weixin.qq.com/cgi-bin/singlemsgpage")
end

#send_message(content, reciever, type = "1") ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/weixin_public.rb', line 136

def send_message(content,reciever,type="1")
  return if !@cookie && (@username,@password) =~ /failed/
  body = {"error"=>false,"ajax"=>1,"f"=>"json","type"=>type,"tofakeid"=>reciever}
  if type == "1"
    body["content"]=content
  else
    body["fid"]= content
    body["fileid"]=content
  end
  ret = request(:post,"/cgi-bin/singlesend?t=ajax-response&lang=zh_CN",body,"https://mp.weixin.qq.com/cgi-bin/singlemsgpage")
end

#set_dev_callback(url, token) ⇒ Object



208
209
210
211
# File 'lib/weixin_public.rb', line 208

def set_dev_callback(url, token)
  return nil if !@cookie && (@username,@password) =~ /failed/
  ret = request(:post,"/cgi-bin/callbackprofile?t=ajax-response&lang=zh_CN",{:url=>url,:callback_token=>token,:ajax=>1},nil)
end

#switch_dev_modeObject



202
203
204
205
206
# File 'lib/weixin_public.rb', line 202

def switch_dev_mode
  operadvancedfunc(4,0)
  operadvancedfunc(1,0)
  operadvancedfunc(2,1)
end