class WeixinPublic::WeixinPubClient
Public Class Methods
new(username,password,cert=nil,sig=nil)
click to toggle source
# 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
Public Instance Methods
avatar_download(file,url)
click to toggle source
# 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")
click to toggle source
# File lib/weixin_public.rb, line 155 def avatar_upload(avatar,type="image/png") return nil if !@cookie && login(@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="")
click to toggle source
# File lib/weixin_public.rb, line 165 def create_appmsg(title,avatar,content,desc="") return nil if !@cookie && login(@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)
click to toggle source
# 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_lst()
click to toggle source
# File lib/weixin_public.rb, line 179 def get_appmsg_lst return nil if !@cookie && login(@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_fans()
click to toggle source
# File lib/weixin_public.rb, line 70 def get_fans return if !@cookie && login(@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)
click to toggle source
# File lib/weixin_public.rb, line 83 def get_messages(fakeId,download=false) return if !@cookie && login(@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)
click to toggle source
# File lib/weixin_public.rb, line 104 def get_messages_number_new(lastmsgid) return if !@cookie && login(@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)
click to toggle source
# File lib/weixin_public.rb, line 110 def get_messages_today(lastmsgid=0,step=50) return if !@cookie && login(@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)
click to toggle source
# File lib/weixin_public.rb, line 53 def login(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)
click to toggle source
type:1 -edit mode 2 -dev mode 4 -auto-reply
# File lib/weixin_public.rb, line 197 def operadvancedfunc(type=1,on_off = 0) return nil if !@cookie && login(@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)
click to toggle source
# 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)
click to toggle source
# File lib/weixin_public.rb, line 148 def send_appmsg(appmsgid,reciever) return if !@cookie && login(@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")
click to toggle source
# File lib/weixin_public.rb, line 136 def send_message(content,reciever,type="1") return if !@cookie && login(@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)
click to toggle source
# File lib/weixin_public.rb, line 208 def set_dev_callback(url, token) return nil if !@cookie && login(@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_mode()
click to toggle source
# File lib/weixin_public.rb, line 202 def switch_dev_mode operadvancedfunc(4,0) operadvancedfunc(1,0) operadvancedfunc(2,1) end