Class: QQBot::Bot
- Inherits:
-
Object
- Object
- QQBot::Bot
- Defined in:
- lib/qqbot/bot.rb
Class Method Summary collapse
- .build_friend_list(json) ⇒ Object
- .check_response_json(code, body) ⇒ Object
- .check_send_msg_response(code, body) ⇒ Object
Instance Method Summary collapse
- #build_user_info(result) ⇒ Object
- #close_qrcode_server ⇒ Object
- #get_account_info ⇒ Object
- #get_discuss_info(discuss_id) ⇒ Object
- #get_discuss_list ⇒ Object
- #get_friend_info(friend_id) ⇒ Object
- #get_friend_list ⇒ Object
- #get_friend_list_with_category ⇒ Object
- #get_group_info(group_code) ⇒ Object
- #get_group_list ⇒ Object
- #get_online_friends ⇒ Object
- #get_psessionid_and_uin(ptwebqq) ⇒ Object
- #get_ptwebqq(url) ⇒ Object
- #get_qq_by_id(id) ⇒ Object
- #get_qrcode ⇒ Object
- #get_recent_list ⇒ Object
- #get_vfwebqq(ptwebqq) ⇒ Object
-
#initialize ⇒ Bot
constructor
A new instance of Bot.
- #login ⇒ Object
- #poll ⇒ Object
- #relogin ⇒ Object
- #send_to_discuss(discuss_id, content) ⇒ Object
- #send_to_friend(friend_id, content) ⇒ Object
- #send_to_group(group_id, content) ⇒ Object
- #send_to_sess(sess_id, content) ⇒ Object
- #verify_qrcode ⇒ Object
Constructor Details
#initialize ⇒ Bot
Returns a new instance of Bot.
5 6 7 8 |
# File 'lib/qqbot/bot.rb', line 5 def initialize @api = QQBot::Api.new @api. = login end |
Class Method Details
.build_friend_list(json) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/qqbot/bot.rb', line 269 def self.build_friend_list(json) friend_map = {} friends = json['friends'] friends.each do |item| friend = QQBot::Friend.new friend.id = item['uin'] friend.category_id = item['categories'] friend_map[friend.id] = friend end marknames = json['marknames'] marknames.each do |item| friend_map[item['uin']].markname = item['markname'] end vipinfo = json['vipinfo'] vipinfo.each do |item| friend = friend_map[item['u']] friend.is_vip = item['is_vip'] friend.vip_level = item['vip_level'] end info = json['info'] info.each do |item| friend_map[item['uin']].nickname = item['nick'] end friend_map.values end |
.check_response_json(code, body) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/qqbot/bot.rb', line 10 def self.check_response_json(code, body) if code == '200' json = JSON.parse body retcode = json['retcode'] if retcode == 0 return json['result'] elsif retcode == 100012 QQBot::LOGGER.info '因为掉线此次请求失败,尝试重新登录' @api. = relogin else QQBot::LOGGER.info "请求失败,JSON返回码 #{retcode}" end else QQBot::LOGGER.info "请求失败,HTTP返回码 #{code}" end end |
.check_send_msg_response(code, body) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/qqbot/bot.rb', line 27 def self.check_send_msg_response(code, body) if code == '200' json = JSON.parse body if json['errCode'] == 0 QQBot::LOGGER.info '发送成功' return true else QQBot::LOGGER.info "发送失败,JSON返回码 #{json['retcode']}" end else QQBot::LOGGER.info "请求失败,HTTP返回码 #{code}" end end |
Instance Method Details
#build_user_info(result) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/qqbot/bot.rb', line 355 def build_user_info(result) user_info = QQBot::UserInfo.new user_info.phone = result['phone'] user_info.occupation = result['occupation'] user_info.college = result['college'] user_info.id = result['uin'] user_info.blood = result['blood'] user_info.slogan = result['lnick'] user_info.homepage = result['homepage'] user_info.vip_info = result['vip_info'] user_info.city = result['city'] user_info.country = result['country'] user_info.province = result['province'] user_info.personal = result['personal'] user_info.shengxiao = result['shengxiao'] user_info.nickname = result['nick'] user_info.email = result['email'] user_info.account = result['account'] user_info.gender = result['gender'] user_info.mobile = result['mobile'] birthday = QQBot::Birthday.new birthday.year = result['birthday']['year'] birthday.month = result['birthday']['month'] birthday.day = result['birthday']['day'] user_info.birthday = birthday user_info end |
#close_qrcode_server ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/qqbot/bot.rb', line 88 def close_qrcode_server if @pid QQBot::LOGGER.info '关闭web服务进程' Process.kill('KILL', @pid) @pid = nil end end |
#get_account_info ⇒ Object
335 336 337 338 339 340 341 342 343 |
# File 'lib/qqbot/bot.rb', line 335 def get_account_info code, body = @api.get_account_info result = self.class.check_response_json(code, body) if result # TODO build_user_info(result) end end |
#get_discuss_info(discuss_id) ⇒ Object
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/qqbot/bot.rb', line 471 def get_discuss_info(discuss_id) code, body = @api.get_discuss_info(discuss_id) result = self.class.check_response_json(code, body) if result discuss_info = QQBot::DiscussInfo.new info = result['info'] discuss_info.id = info['did'] discuss_info.name = info['discu_name'] member_map = {} mem_info = result['mem_info'] mem_info.each do |item| member = QQBot::DiscussMember.new member.id = item['uin'] member.nickname = item['nick'] member_map[member.id] = member end mem_status = result['mem_status'] mem_status.each do |item| member = member_map[item['uin']] member.client_type = item['client_type'] member.status = item['status'] end discuss_info.members = member_map.values discuss_info end end |
#get_discuss_list ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/qqbot/bot.rb', line 300 def get_discuss_list code, body = @api.get_discuss_list result = self.class.check_response_json(code, body) if result dnamelist = result['dnamelist'] dnamelist.collect do |item| discuss = QQBot::Discuss.new discuss.name = item['name'] discuss.id = item['did'] discuss end end end |
#get_friend_info(friend_id) ⇒ Object
345 346 347 348 349 350 351 352 353 |
# File 'lib/qqbot/bot.rb', line 345 def get_friend_info(friend_id) code, body = @api.get_friend_info(friend_id) result = self.class.check_response_json(code, body) if result # TODO build_user_info(result) end end |
#get_friend_list ⇒ Object
260 261 262 263 264 265 266 267 |
# File 'lib/qqbot/bot.rb', line 260 def get_friend_list code, body = @api.get_friend_list result = self.class.check_response_json(code, body) if result self.class.build_friend_list(result) end end |
#get_friend_list_with_category ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/qqbot/bot.rb', line 228 def get_friend_list_with_category code, body = @api.get_friend_list result = self.class.check_response_json(code, body) if result friend_list = self.class.build_friend_list result categories = result['categories'] has_default_category = false category_list = categories.collect do |item| category = QQBot::Category.new category.name = item['name'] category.sort = item['sort'] category.id = item['index'] category.friends = friend_list.select { |friend| friend.category_id == category.id } has_default_category ||= (category.id == 0) category end unless has_default_category category = QQBot::Category.new category.name = '我的好友(默认)' category.sort = 1 category.id = 0 category.friends = friend_list.select { |friend| friend.category_id == category.id } category_list << category end category_list end end |
#get_group_info(group_code) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/qqbot/bot.rb', line 421 def get_group_info(group_code) code, body = @api.get_group_info(group_code) result = self.class.check_response_json(code, body) if result group_info = QQBot::GroupInfo.new ginfo = result['ginfo'] group_info.id = ginfo['gid'] group_info.create_time = ginfo['createtime'] group_info.memo = ginfo['memo'] group_info.name = ginfo['name'] group_info.owner_id = ginfo['owner'] group_info.markname = ginfo['markname'] member_map = {} minfo = result['minfo'] minfo.each do |item| member = QQBot::GroupMember.new member.id = item['uin'] member.nickname = item['nick'] member.gender = item['gender'] member.country = item['country'] member.city = item['city'] member_map[member.id] = member end cards = result['cards'] cards.each { |item| member_map[item['muin']].markname = item['card'] } if cards vipinfo = result['vipinfo'] vipinfo.each do |item| member = member_map[item['u']] member.is_vip = item['is_vip'] member.vip_level = item['vip_level'] end stats = result['stats'] stats.each do |item| member = member_map[item['uin']] member.client_type = item['client_type'] member.status = item['stat'] end group_info.members = member_map.values group_info end end |
#get_group_list ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/qqbot/bot.rb', line 203 def get_group_list code, body = @api.get_group_list result = self.class.check_response_json(code, body) if result group_map = {} gnamelist = result['gnamelist'] gnamelist.each do |item| group = QQBot::Group.new group.name = item['name'] group.id = item['gid'] group.code = item['code'] group_map[group.id] = group end gmarklist = result['gmarklist'] gmarklist.each do |item| group_map[item['uin']].markname = item['markname'] end group_map.values end end |
#get_online_friends ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/qqbot/bot.rb', line 407 def get_online_friends code, body = @api.get_online_friends result = self.class.check_response_json(code, body) if result result.collect do |item| online = QQBot::Online.new online.id = item['uin'] online.client_type = item['client_type'] online end end end |
#get_psessionid_and_uin(ptwebqq) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/qqbot/bot.rb', line 113 def get_psessionid_and_uin(ptwebqq) code, body = @api.get_psessionid_and_uin ptwebqq result = self.class.check_response_json(code, body) if result return result['psessionid'], result['uin'] end end |
#get_ptwebqq(url) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/qqbot/bot.rb', line 96 def get_ptwebqq(url) code, ptwebqq = @api.get_ptwebqq url if code == '302' ptwebqq else QQBot::LOGGER.info "请求失败,返回码#{code}" end end |
#get_qq_by_id(id) ⇒ Object
398 399 400 401 402 403 404 405 |
# File 'lib/qqbot/bot.rb', line 398 def get_qq_by_id(id) code, body = @api.get_qq_by_id(id) result = self.class.check_response_json(code, body) if result result['account'] end end |
#get_qrcode ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/qqbot/bot.rb', line 41 def get_qrcode code, body = @api.get_qrcode if code == '200' file_name = File.('qrcode.png', File.dirname(__FILE__)); File.open(file_name, 'wb') do |file| file.write body file.close end QQBot::LOGGER.info "二维码已经保存在#{file_name}中" unless @pid QQBot::LOGGER.info '开启web服务进程' @pid = spawn("ruby -run -e httpd #{file_name} -p 9090") end QQBot::LOGGER.info '也可以通过访问 http://localhost:9090 查看二维码' return true else QQBot::LOGGER.info "请求失败,返回码#{code}" end end |
#get_recent_list ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/qqbot/bot.rb', line 384 def get_recent_list code, body = @api.get_recent_list result = self.class.check_response_json(code, body) if result result.collect do |item| recent = QQBot::Recent.new recent.id = item['uin'] recent.type = item['type'] recent end end end |
#get_vfwebqq(ptwebqq) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/qqbot/bot.rb', line 106 def get_vfwebqq(ptwebqq) code, body = @api.get_vfwebqq(ptwebqq) result = self.class.check_response_json(code, body) result['vfwebqq'] if result end |
#login ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/qqbot/bot.rb', line 122 def login QQBot::LOGGER.info '开始获取二维码' raise QQBot::Error::LoginFailed unless get_qrcode QQBot::LOGGER.info '等待扫描二维码' url = verify_qrcode raise QQBot::Error::LoginFailed unless url close_qrcode_server QQBot::LOGGER.info '开始获取ptwebqq' @ptwebqq = get_ptwebqq url raise QQBot::Error::LoginFailed unless @ptwebqq QQBot::LOGGER.info '开始获取vfwebqq' vfwebqq = get_vfwebqq @ptwebqq raise QQBot::Error::LoginFailed unless vfwebqq QQBot::LOGGER.info '开始获取psessionid和uin' psessionid, uin = get_psessionid_and_uin @ptwebqq raise QQBot::Error::LoginFailed unless uin && psessionid { ptwebqq: @ptwebqq, vfwebqq: vfwebqq, psessionid: psessionid, uin: uin } end |
#poll ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/qqbot/bot.rb', line 169 def poll loop do code, body = @api.poll result = self.class.check_response_json(code, body) if result result.each do |item| = QQBot::Message.new value = item['value'] .type, .from_id, .send_id = case item['poll_type'] when 'message' then [0, value['from_uin'], value['from_uin']] when 'group_message' then [1, value['from_uin'], value['send_uin']] when 'discu_message' then [2, value['from_uin'], value['send_uin']] else 3 end .time = value['time'] .content = value['content'][1] font = QQBot::Font.new font_json = value['content'][0][1] font.color = font_json['color'] font.name = font_json['name'] font.size = font_json['size'] font.style = font_json['style'] .font = font yield if block_given? end end sleep 1 end end |
#relogin ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/qqbot/bot.rb', line 152 def relogin QQBot::LOGGER.info '开始获取vfwebqq' vfwebqq = get_vfwebqq @ptwebqq raise QQBot::Error::LoginFailed unless vfwebqq QQBot::LOGGER.info '开始获取psessionid和uin' psessionid, uin = get_psessionid_and_uin @ptwebqq raise QQBot::Error::LoginFailed unless uin && psessionid { ptwebqq: @ptwebqq, vfwebqq: vfwebqq, psessionid: psessionid, uin: uin } end |
#send_to_discuss(discuss_id, content) ⇒ Object
325 326 327 328 |
# File 'lib/qqbot/bot.rb', line 325 def send_to_discuss(discuss_id, content) code, body = @api.send_to_discuss(discuss_id, content) self.class.check_send_msg_response(code, body) end |
#send_to_friend(friend_id, content) ⇒ Object
315 316 317 318 |
# File 'lib/qqbot/bot.rb', line 315 def send_to_friend(friend_id, content) code, body = @api.send_to_friend(friend_id, content) self.class.check_send_msg_response(code, body) end |
#send_to_group(group_id, content) ⇒ Object
320 321 322 323 |
# File 'lib/qqbot/bot.rb', line 320 def send_to_group(group_id, content) code, body = @api.send_to_group(group_id, content) self.class.check_send_msg_response(code, body) end |
#send_to_sess(sess_id, content) ⇒ Object
330 331 332 333 |
# File 'lib/qqbot/bot.rb', line 330 def send_to_sess(sess_id, content) code, body = @api.send_to_sess(sess_id, content) self.class.check_send_msg_response(code, body) end |
#verify_qrcode ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/qqbot/bot.rb', line 66 def verify_qrcode url = '' until url.start_with? 'http' do sleep 5 code, body = @api.verify_qrcode if code == '200' result = body.force_encoding("UTF-8") if result.include? '二维码已失效' QQBot::LOGGER.info '二维码已失效,准备重新获取' return unless get_qrcode elsif result.include? 'http' QQBot::LOGGER.info '认证成功' return URI.extract(result).first end else QQBot::LOGGER.info "请求失败,返回码#{code}" end end end |