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

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



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

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

Instance Attribute Details

#loopObject

Returns the value of attribute loop.



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

def loop
  @loop
end

Instance Method Details

#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



30
31
32
33
34
35
36
37
# File 'lib/icqbot/bot.rb', line 30

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

#last_eventObject

FIXME: its can delete



50
51
52
53
54
55
56
57
# File 'lib/icqbot/bot.rb', line 50

def last_event # FIXME: its can delete

  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']
    yield ICQ::Event.new last_event
  end
end

#listenObject

event loop



39
40
41
42
43
44
45
46
47
48
# File 'lib/icqbot/bot.rb', line 39

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']
      yield ICQ::Event.new last_event
    end
  end
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