`
鹤惊昆仑
  • 浏览: 223050 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基于web飞信接口的飞信应答机器人

    博客分类:
  • ruby
阅读更多
fetion-robot是基于web飞信接口的飞信机器人(一小段ruby脚本)。最初目标是做一个可以发新浪微博的飞信机器人。 目前实现了
  • 登录(验证码)
  • 信息
  • 发信息等功能

应答机器人最合适
源码仅仅145行--意味着任何人都可以很容易地修改以便于自用
本脚本目标定位于应答机器人,所以我会尽量不增加不必要的功能,保证代码量不膨胀。
最新源码http://code.google.com/p/fetion-robot/

#! /usr/bin/ruby
#author newdongyuwei@gmail.com

$KCODE = 'UTF-8'#解决中文乱码问题
%w(rubygems fileutils sinatra sinatra/base net/http net/https json).each{|lib|require lib}

class FetionRobot < Sinatra::Base
    @@Version = 0
    
    enable :sessions
    use Rack::Static, :urls => ["/images","/css","/js" ], :root => "public"
    set  :environment, :development
    set  :run, true
    
    def request(path,method,body,headers,hasVersion)
        if not body
            body =  "ssid=#{session['webim_sessionid']}"
        end
        if not headers
             headers = {
                'Referer' => 'https://webim.feixin.10086.cn/',
                'Content-Type' => 'application/x-www-form-urlencoded'
             }
        end
        http = Net::HTTP.new('webim.feixin.10086.cn', 443)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE # turn off SSL warning
        if hasVersion
            path = "#{path}?Version=#{@@Version}"
            @@Version = @@Version + 1
        end
        
        if method == 'GET'
            return (resp, data = http.get(path, nil))
        else
            return (resp, data = http.post(path, body,headers))
        end
    end
    
    def get_code_img
        resp, data = request( '/WebIM/GetPicCode.aspx?Type=ccpsession','GET',nil,nil,false)
        dir = File.join(File.dirname(__FILE__),"public","images")
        FileUtils.mkdir_p(dir)
        File.open(File.join(dir,"fetion_code.jpeg"), "wb"){|file|
            file.write(resp.body)
            file.close
        }
        cookie = resp.response['set-cookie'].split(';')[0]
        session['ccpsession'] = cookie
    end 
    
    def get_personal_info
        resp, data = request(  "/WebIM/GetPersonalInfo.aspx",'POST',nil,nil,true)
        puts data
        info = JSON(data)
        if info['rc'] == 200
            self.get_contact_list()
        end
    end
    
    def get_contact_list
        resp, data = request(  "/WebIM/GetContactList.aspx",'POST',nil,nil,true)
        info = JSON(data)
        puts data
        if info['rc'] == 200
            self.keep_alive
        end
    end
    
    def keep_alive
        Thread.new { 
            loop { 
                puts "---------------------------------keep_alive-----------------------------------"
                resp, data = request(  "/WebIM/GetConnect.aspx",'POST',nil,nil,true)
                info = JSON(data)
                puts info['rc']
                puts resp,data
                if info['rc']== 200
                    info['rv'].each do|item|
                        data = item['Data']
                        if item['DataType'] ==3 and data
                            self.send_msg(data['fromUid'],data['msg']+' - -feedfack')#ping-pong-test
                        end
                    end
                end
                sleep 5
             }
         }
    end
    
    def send_msg(to='660122812',msg='test中文',isSendSms='0')
        puts '--------------------send msg ------------------------------'
        body = "To=#{to}&IsSendSms=#{isSendSms}&msg=#{msg}&ssid=#{session['webim_sessionid']}"
        resp, data = request(  "/WebIM/SendMsg.aspx",'POST',body,nil,true)
        info = JSON(data)
        puts data
        if info['rc']== 200
            puts 'send msg ok!'
        end
        return data
    end
    
    get '/' do
        self.get_code_img()
         "<form action='/login'>\
            <LABEL for='UserName' >手机或飞信号</LABEL><input name='UserName'/>\
            <LABEL for='Pwd' >密码</LABEL><input name='Pwd'/>\
            <LABEL for='Ccp' >验证码</LABEL><input name='Ccp'/>\
            <img onclick='window.location.reload(true)'  src='/images/fetion_code.jpeg' />\
            <input type='submit' value='登录飞信'/>\
         </form>"
    end

    get '/login' do
        body = "UserName=#{params['UserName']}&Pwd=#{params['Pwd']}&OnlineStatus=400&Ccp=#{params['Ccp']}"
        puts body
        headers = {
            'Cookie' => session['ccpsession'],
            'Referer' => 'https://webim.feixin.10086.cn/',
            'Content-Type' => 'application/x-www-form-urlencoded'
         }
        resp, data = request(  "/WebIM/Login.aspx",'POST',body,headers,false)
        puts data
        rc = JSON(data)['rc']
        if rc == 200
            webim_sessionid = resp.response['set-cookie'].split('webim_sessionid=')[1].split(';')[0]
            session['webim_sessionid'] = webim_sessionid
            puts webim_sessionid
            self.get_personal_info()
            'login ok!'
        else
            redirect "/"
        end
    end
end

FetionRobot.run!({:port => 3000})
3
0
分享到:
评论
4 楼 ganky 2011-02-07  
终于搞定了,我郁闷啊……
我是用JAVA写的,在登录成功后使用getHeaderField("Set-Cookie"),获取到的Set-Cookie只有“webim_userstatus=400; path=/”
但是使用getHeaderFields(),打印所有Header就能获取Set-Cookie里面的webim_sessionid,打印如下
Set-Cookie:[webim_userstatus=400; path=/, webim_usersid=850006634; path=/, webim_sessionid=470824898p6060-7ea82fe1-c208-4588-b24c-98a7c06a470c; path=/]

看来getHeaderField("Set-Cookie")方法只获取字符串集合的第一个元素,居然在这低级问题上白搞了一天。

幸好看到这篇文章,让我确定webim_sessionid是在登录成功后获取的,如果没有找到这篇文章,估计我还会被这问题纠缠几天,差点都吧web飞信的js文件给研究透了
3 楼 ganky 2011-02-07  
之前也有搞了一下,只实现了登录,因为一直找不到webim_sessionid在哪获取,呵呵
研究一下这文章~~
2 楼 kewin 2010-12-03  
现在127行了哦
1 楼 kewin 2010-12-03  
值得膜拜哦  和和

相关推荐

Global site tag (gtag.js) - Google Analytics