Class: VkLongpollBot::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/vk_longpoll_bot/bot.rb

Overview

Main class, which contains all the methods of bot.

Instance Attribute Summary collapse

Events collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, group_id, options = {}) ⇒ Bot

Initialize bot. This method don’t start longpoll session.

Parameters:

  • access_token (String)

    group access token.

  • group_id (Integer)

    group of associated ID.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • api_version (Gem::Version, String) — default: VK_API_URL_BASE

    version of VK API.

  • longpoll_wait (Integer)

    longpoll (LONGPOLL_STANDART_WAIT) requests timeout.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vk_longpoll_bot/bot.rb', line 24

def initialize(access_token, group_id, options = {})
  @event_listeners = Hash.new { |hash, key| hash[key] = Array.new }
  @on_start = []
  @on_finish = []

  @access_token = access_token.to_s
  @id = group_id.to_i
  
  @api_version = options[:api_version] || VK_API_CURRENT_VERSION
  @longpoll_wait = options[:longpoll_wait] || LONGPOLL_STANDART_WAIT
  
  @longpoll = {}
end

Instance Attribute Details

#event_listenersHash<Symbol, Array<EventListener>> (readonly)

Returns hash with listeners for each of event type.

Returns:

  • (Hash<Symbol, Array<EventListener>>)

    hash with listeners for each of event type.



13
14
15
# File 'lib/vk_longpoll_bot/bot.rb', line 13

def event_listeners
  @event_listeners
end

#idInteger (readonly)

Returns group ID.

Returns:

  • (Integer)

    group ID.



9
10
11
# File 'lib/vk_longpoll_bot/bot.rb', line 9

def id
  @id
end

Instance Method Details

#api(method_name, parameters = {}) ⇒ Hash

Call for API method.

Parameters:

Returns:

  • (Hash)


47
48
49
# File 'lib/vk_longpoll_bot/bot.rb', line 47

def api(method_name, parameters = {})
  Request.api(method_name, parameters, @access_token, @api_version)
end

#disable_onlinenil

Disable group online status

Returns:

  • (nil)


87
88
89
90
91
92
93
94
# File 'lib/vk_longpoll_bot/bot.rb', line 87

def disable_online
  begin
    api("groups.disableOnline", group_id: @id)
  rescue
    # Online is already disabled
  end
  nil
end

#enable_onlinenil

Enable group online status

Returns:

  • (nil)


74
75
76
77
78
79
80
81
# File 'lib/vk_longpoll_bot/bot.rb', line 74

def enable_online
  begin
    api("groups.enableOnline", group_id: @id)
  rescue
    # Online is already enabled
  end
  nil
end

#on(options) {|event| ... } ⇒ nil

Add new event listener.

Parameters:

  • options (Hash)

Options Hash (options):

  • subtype (String)

Yield Parameters:

Returns:

  • (nil)

Raises:

  • (ArgumentError)


112
113
114
115
116
# File 'lib/vk_longpoll_bot/bot.rb', line 112

def on(options, &block)
  raise ArgumentError.new("Got subtype #{options[:subtype]} of class #{options[:subtype].class}") unless String === options[:subtype] && Events.valid_subtype?(options[:subtype])
  @event_listeners[options[:subtype]] << Events::EventListener.new(options, &block)
  nil
end

#on_finish(&block) ⇒ Object

Add code to be executed right after bot finishes.



126
127
128
# File 'lib/vk_longpoll_bot/bot.rb', line 126

def on_finish(&block)
  @on_finish << block
end

#on_start(&block) ⇒ Object

Add code to be executed right after bot starts.



120
121
122
# File 'lib/vk_longpoll_bot/bot.rb', line 120

def on_start(&block)
  @on_start << block
end

#runvoid

This method returns an undefined value.

Start bot. This methods freeze current thread until #stop method is called.



137
138
139
140
141
142
143
144
# File 'lib/vk_longpoll_bot/bot.rb', line 137

def run
  @on_start.each(&:call)

  init_longpoll
  run_longpoll
  
  @on_finish.each(&:call)
end

#send_message(target, content, options = {}) ⇒ Hash

Send text message.

Parameters:

  • target (Integer)

    ID of receiver.

  • content (String)

    text of message.

  • options (Hash) (defaults to: {})

    additional options which must be sent with request. Options user_id, message and random_id will be overwritten

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
# File 'lib/vk_longpoll_bot/bot.rb', line 60

def send_message(target, content, options = {})
  target_id = target.to_i
  forced_options = {
    user_id: target_id,
    message: content,
    random_id: Utility.random_id(target_id)
  }
  api("messages.send", options.merge(forced_options))
end

#stopvoid

This method returns an undefined value.

Stop bot.



150
151
152
# File 'lib/vk_longpoll_bot/bot.rb', line 150

def stop
  @finish_flag = true
end