class ICQ::Bot

Attributes

loop[RW]

Public Class Methods

new(token, pool_time=30) { |self| ... } click to toggle source
# 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

Public Instance Methods

add_callback_handler(data, handler) click to toggle source
# File lib/icqbot/bot.rb, line 64
def add_callback_handler data, handler
  @callback_handlers[data] = handler
end
add_handler(text, handler) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_events() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
listen() { |last_event| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/icqbot/functional/chats/administration/set_title.rb, line 13
def title= o
  set_title *o
end
unblock_user(user_id, chat_id) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

base_req(chat_id) click to toggle source
# File lib/icqbot/bot.rb, line 69
def base_req chat_id
  {
    'token': @token,
    'chatId': chat_id
  }
end
base_req_for_block_unblock_user(user_id, chat_id, del_last_msg=nil) click to toggle source
# File lib/icqbot/functional/chats/administration/block_unblock_user.rb, line 19
def base_req_for_block_unblock_user user_id, chat_id, del_last_msg=nil
  params = base_req chat_id
  params['userId'] = user_id
  params['delLastMessages'] = del_last_msg if del_last_msg
  return params
end
create_message_params(msg, chat_id) click to toggle source
# File lib/icqbot/functional/send_msg.rb, line 12
def create_message_params msg, chat_id
  params = base_req chat_id
  if msg.is_a? ICQ::Message
    params['text'] = msg.text
    if msg.keyboard and msg.keyboard != []
      kb = msg.keyboard.map(&:to_h).map(&:to_json)
      params['inlineKeyboardMarkup'] = "[[#{kb.join(',')}]]"
    end
  elsif msg.is_a? String
    params['text'] = msg
  else
    raise ArgumentError.new "message can't be a #{msg.class}"
  end
  return params
end