Class: ICQ::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/icqbot/bot.rb,
lib/icqbot/functional/edit_msg.rb,
lib/icqbot/functional/send_msg.rb,
lib/icqbot/functional/delete_msg.rb,
lib/icqbot/functional/chats/get_info.rb,
lib/icqbot/functional/chats/get_admins.rb,
lib/icqbot/functional/chats/get_members.rb,
lib/icqbot/functional/chats/get_blocked_users.rb,
lib/icqbot/functional/chats/administration/set_about.rb,
lib/icqbot/functional/chats/administration/set_rules.rb,
lib/icqbot/functional/chats/administration/set_title.rb,
lib/icqbot/functional/chats/administration/pin_unpin_msg.rb,
lib/icqbot/functional/chats/administration/block_unblock_user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, pool_time = 30) {|_self| ... } ⇒ Bot

Returns a new instance of Bot.

Yields:

  • (_self)

Yield Parameters:

  • _self (ICQ::Bot)

    the object that the method was called on



23
24
25
26
27
28
29
30
31
# File 'lib/icqbot/bot.rb', line 23

def initialize token, pool_time=30
  @token = token
  @pool_time = pool_time
  @last_event_id = 0
  @loop = true
  @handlers = {}
  @callback_handlers = {}
  yield self if block_given?
end

Instance Attribute Details

#loopObject

Returns the value of attribute loop.



21
22
23
# File 'lib/icqbot/bot.rb', line 21

def loop
  @loop
end

Instance Method Details

#add_callback_handler(data, handler) ⇒ Object



64
65
66
# File 'lib/icqbot/bot.rb', line 64

def add_callback_handler data, handler
  @callback_handlers[data] = handler
end

#add_handler(text, handler) ⇒ Object



60
61
62
# File 'lib/icqbot/bot.rb', line 60

def add_handler text, handler
  @handlers[text] = handler
end

#block_user(user_id, chat_id, del_last_msg = false) ⇒ Object



6
7
8
9
10
# File 'lib/icqbot/functional/chats/administration/block_unblock_user.rb', line 6

def block_user user_id, chat_id, del_last_msg=false
  params = base_req_for_block_unblock_user user_id, chat_id, del_last_msg
  JSON::load Requests.get(
    URLS_API::BLOCK_USER, params: params).body
end

#delete_msg(msg_id, chat_id) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/delete_msg.rb', line 6

def delete_msg msg_id, chat_id
  # FIXME: fix this trash
  params = base_req chat_id
  r = "https://api.icq.net/bot/v1/messages/deleteMessages?token=#{params[:token]}&chatId=#{chat_id}&msgId=#{msg_id}" # HACK: super trash
  JSON::load Requests.get(r).body
end

#edit_msg(msg, msg_id, chat_id) ⇒ Object



6
7
8
9
10
# File 'lib/icqbot/functional/edit_msg.rb', line 6

def edit_msg msg, msg_id, chat_id
  params = create_message_params msg, chat_id
  params['msgId'] = msg_id
  JSON::load Requests.get(URLS_API::EDIT_MSG, params: params).body
end

#get_admins(chat_id) ⇒ Object



6
7
8
9
10
# File 'lib/icqbot/functional/chats/get_admins.rb', line 6

def get_admins chat_id
  _ = JSON::load Requests.get(
    URLS_API::GET_ADMINS, params: base_req(chat_id)).body
  _['admins']
end

#get_blocked_users(chat_id) ⇒ Object



6
7
8
9
10
# File 'lib/icqbot/functional/chats/get_blocked_users.rb', line 6

def get_blocked_users chat_id
  _ = JSON::load Requests.get(
    URLS_API::GET_BLOCKED_USERS, params: base_req(chat_id)).body
  _['users']
end

#get_eventsObject

/events/get



33
34
35
36
37
38
39
40
# File 'lib/icqbot/bot.rb', line 33

def get_events # /events/get
  params = {
    'token': @token,
    'lastEventId': @last_event_id,
    'pollTime': @pool_time
  }
  Requests.get(URLS_API::GET_EVENTS, params: params)
end

#get_info(chat_id) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/get_info.rb', line 6

def get_info chat_id
  # TODO: make a class for chats, kind of
  params = base_req chat_id
  json = JSON::load Requests.get(URLS_API::GET_INFO, params: params).body
  return ICQ::User.new json if json['firstName']
end

#get_members(chat_id, cursor = nil) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/get_members.rb', line 6

def get_members chat_id, cursor=nil
  # TODO: сделать что-то с cursor
  _ = JSON::load Requests.get(
    URLS_API::GET_MEMBRS, params: base_req(chat_id)).body
  _['members']
end

#listenObject

event loop



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/icqbot/bot.rb', line 42

def listen # event loop
  while @loop
    events = JSON::load(get_events.body)
    if events and events['events'] and events['events'] != []
      last_event = events['events'].last
      @last_event_id = last_event['eventId']
      last_event = ICQ::Event.new last_event
      if @handlers.has_key? last_event.text
        @handlers[last_event.text].call last_event
      elsif last_event.type == ICQ::TypeEvent::CALLBACK and @callback_handlers.has_key? last_event.data 
        @callback_handlers[last_event.text].call last_event
      else
        yield last_event
      end
    end
  end
end

#pin_msg(msg_id, chat_id) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/administration/pin_unpin_msg.rb', line 6

def pin_msg msg_id, chat_id
  params = base_req chat_id
  params['msgId'] = msg_id
  JSON::load Requests.get(
    URLS_API::PIN_MSG, params: params).body
end

#send_msg(msg, chat_id) ⇒ Object



6
7
8
9
# File 'lib/icqbot/functional/send_msg.rb', line 6

def send_msg msg, chat_id
  params = create_message_params msg, chat_id
  JSON::load Requests.get(URLS_API::SEND_MSG, params: params).body
end

#set_about(chat_id, about) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/administration/set_about.rb', line 6

def set_about chat_id, about
  params = base_req chat_id
  params['about'] = about
  JSON::load Requests.get(
    URLS_API::SET_ABOUT, params: params).body
end

#set_rules(chat_id, rules) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/administration/set_rules.rb', line 6

def set_rules chat_id, rules
  params = base_req chat_id
  params['rules'] = rules
  JSON::load Requests.get(
    URLS_API::SET_RULES, params: params).body
end

#set_title(chat_id, title) ⇒ Object



6
7
8
9
10
11
# File 'lib/icqbot/functional/chats/administration/set_title.rb', line 6

def set_title chat_id, title
  params = base_req chat_id
  params['title'] = title
  JSON::load Requests.get(
    URLS_API::SET_TITLE, params: params).body
end

#title=(o) ⇒ Object



13
14
15
# File 'lib/icqbot/functional/chats/administration/set_title.rb', line 13

def title= o
  set_title *o
end

#unblock_user(user_id, chat_id) ⇒ Object



12
13
14
15
16
# File 'lib/icqbot/functional/chats/administration/block_unblock_user.rb', line 12

def unblock_user user_id, chat_id
  params = base_req_for_block_unblock_user user_id, chat_id
  JSON::load Requests.get(
    URLS_API::UNBLOCK_USER, params: params).body
end

#unpin_msg(msg_id, chat_id) ⇒ Object



13
14
15
16
17
18
# File 'lib/icqbot/functional/chats/administration/pin_unpin_msg.rb', line 13

def unpin_msg msg_id, chat_id
  params = base_req chat_id
  params['msgId'] = msg_id
  JSON::load Requests.get(
    URLS_API::UNPIN_MSG, params: params).body
end