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 ⇒ Object
readonly
Every bot stores id of group it operates and hash of event listeners.
-
#id ⇒ Object
readonly
Every bot stores id of group it operates and hash of event listeners.
Instance Method Summary collapse
-
#api(method_name, parameters = {}) ⇒ Object
Call for api method with given parameters.
-
#disable_online(gid = @id) ⇒ Object
Disable group online status.
-
#enable_online(gid = @id) ⇒ Object
Enable group online status.
-
#initialize(access_token, id, options = {}) ⇒ Bot
constructor
Initialize bot.
-
#on(attributes, &block) ⇒ Object
Add 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.
-
#run ⇒ Object
Start bot.
-
#send_message(target, content) ⇒ Object
Send message to
targetwith providedcontent. -
#stop ⇒ Object
Stop bot.
Constructor Details
#initialize(access_token, id, options = {}) ⇒ Bot
Initialize bot. This method don’t run longpoll.
options hash can contain following keys:
-
:api_version- version of api to use -
:longpoll_wait- longpoll requests timeout
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vk_longpoll_bot/bot.rb', line 14 def initialize(access_token, id, = {}) @event_listeners = Hash.new { |hash, key| hash[key] = Array.new } @on_start = [] @on_finish = [] @access_token = access_token @id = id @api_version = [:api_version] || VK_API_CURRENT_VERSION @longpoll_wait = [:longpoll_wait] || LONGPOLL_STANDART_WAIT @longpoll = {} # TODO end |
Instance Attribute Details
#event_listeners ⇒ Object (readonly)
Every bot stores id of group it operates and hash of event listeners.
7 8 9 |
# File 'lib/vk_longpoll_bot/bot.rb', line 7 def event_listeners @event_listeners end |
#id ⇒ Object (readonly)
Every bot stores id of group it operates and hash of event listeners.
7 8 9 |
# File 'lib/vk_longpoll_bot/bot.rb', line 7 def id @id end |
Instance Method Details
#api(method_name, parameters = {}) ⇒ Object
Call for api method with given parameters.
35 36 37 |
# File 'lib/vk_longpoll_bot/bot.rb', line 35 def api(method_name, parameters = {}) Request.api(method_name, parameters, @access_token, @api_version) end |
#disable_online(gid = @id) ⇒ Object
Disable group online status
56 57 58 59 60 61 62 |
# File 'lib/vk_longpoll_bot/bot.rb', line 56 def disable_online(gid = @id) begin api("groups.disableOnline", group_id: gid) rescue # Online is already disabled end end |
#enable_online(gid = @id) ⇒ Object
Enable group online status
47 48 49 50 51 52 53 |
# File 'lib/vk_longpoll_bot/bot.rb', line 47 def enable_online(gid = @id) begin api("groups.enableOnline", group_id: gid) rescue # Online is already enabled end end |
#on(attributes, &block) ⇒ Object
Add event listener.
attributes hash can contain following keys:
-
:subtype- event subtype. All of event types and subtypes are stated in Events::TYPES
74 75 76 77 |
# File 'lib/vk_longpoll_bot/bot.rb', line 74 def on(attributes, &block) raise ArgumentError.new("Got subtype #{attributes[:subtype]} of class #{attributes[:subtype].class}") unless String === attributes[:subtype] && Events.valid_subtype?(attributes[:subtype]) @event_listeners[attributes[:subtype]] << Events::EventListener.new(attributes, &block) end |
#on_finish(&block) ⇒ Object
Add code to be executed right after bot finishes.
85 86 87 |
# File 'lib/vk_longpoll_bot/bot.rb', line 85 def on_finish(&block) @on_finish << block end |
#on_start(&block) ⇒ Object
Add code to be executed right after bot starts.
80 81 82 |
# File 'lib/vk_longpoll_bot/bot.rb', line 80 def on_start(&block) @on_start << block end |
#run ⇒ Object
Start bot. This methods freeze current thread until stop called.
94 95 96 97 98 99 100 101 |
# File 'lib/vk_longpoll_bot/bot.rb', line 94 def run @on_start.each(&:call) init_longpoll run_longpoll @on_finish.each(&:call) end |
#send_message(target, content) ⇒ Object
Send message to target with provided content.
40 41 42 43 44 |
# File 'lib/vk_longpoll_bot/bot.rb', line 40 def (target, content) target_id = target.to_i random_id = Utility.random_id(target_id) api("messages.send", user_id: target_id, message: content, random_id: random_id) end |
#stop ⇒ Object
Stop bot.
104 105 106 |
# File 'lib/vk_longpoll_bot/bot.rb', line 104 def stop @finish_flag = true end |