Class: VkLongpollBot::Bot
- Inherits:
-
Object
- Object
- VkLongpollBot::Bot
- Defined in:
- lib/vk_longpoll_bot/bot.rb
Overview
Main class, which contains all the methods of bot.
Instance Attribute Summary collapse
-
#event_listeners ⇒ Hash<Symbol, Array<EventListener>>
readonly
Hash with listeners for each of event type.
-
#id ⇒ Integer
readonly
Group ID.
Events collapse
-
#on(options) {|event| ... } ⇒ nil
Add new event listener.
-
#on_finish(&block) ⇒ Object
Add code to be executed right after bot finishes.
-
#on_start(&block) ⇒ Object
Add code to be executed right after bot starts.
Instance Method Summary collapse
-
#api(method_name, parameters = {}) ⇒ Hash
Call for API method.
-
#disable_online ⇒ nil
Disable group online status.
-
#enable_online ⇒ nil
Enable group online status.
-
#initialize(access_token, group_id, options = {}) ⇒ Bot
constructor
Initialize bot.
-
#run ⇒ void
Start bot.
-
#send_message(target, content, options = {}) ⇒ Hash
Send text message.
-
#stop ⇒ void
Stop bot.
Constructor Details
#initialize(access_token, group_id, options = {}) ⇒ Bot
Initialize bot. This method don’t start longpoll session.
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, = {}) @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 = [:api_version] || VK_API_CURRENT_VERSION @longpoll_wait = [:longpoll_wait] || LONGPOLL_STANDART_WAIT @longpoll = {} end |
Instance Attribute Details
#event_listeners ⇒ Hash<Symbol, Array<EventListener>> (readonly)
Returns 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 |
#id ⇒ Integer (readonly)
Returns 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.
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_online ⇒ nil
Disable group online status
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_online ⇒ nil
Enable group online status
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.
112 113 114 115 116 |
# File 'lib/vk_longpoll_bot/bot.rb', line 112 def on(, &block) raise ArgumentError.new("Got subtype #{[:subtype]} of class #{[:subtype].class}") unless String === [:subtype] && Events.valid_subtype?([:subtype]) @event_listeners[[:subtype]] << Events::EventListener.new(, &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 |
#run ⇒ void
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.
60 61 62 63 64 65 66 67 68 |
# File 'lib/vk_longpoll_bot/bot.rb', line 60 def (target, content, = {}) target_id = target.to_i = { user_id: target_id, message: content, random_id: Utility.random_id(target_id) } api("messages.send", .merge()) end |
#stop ⇒ void
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 |