Class: VkCozy::Bot

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

Constant Summary collapse

CLASS_BY_EVENT_TYPE =
{
  BotEventType::MESSAGE_NEW => BotMessageEvent,
  BotEventType::MESSAGE_REPLY => BotMessageEvent,
  BotEventType::MESSAGE_EDIT => BotMessageEvent
}
DEFAULT_EVENT_CLASS =
BotEvent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, version = 5.92) ⇒ Bot

Returns a new instance of Bot.



18
19
20
21
22
23
24
# File 'lib/vk_cozy/bot.rb', line 18

def initialize(access_token, version=5.92)
  @access_token = access_token
  @api = Api.new(access_token, version)
  
  @polling = VkCozy::BotPolling.new(@api)
  @labeler = VkCozy::BotLabeler.new(@api)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



8
9
10
# File 'lib/vk_cozy/bot.rb', line 8

def api
  @api
end

Instance Method Details

#onObject



26
27
28
# File 'lib/vk_cozy/bot.rb', line 26

def on
  return @labeler
end

#on_startupObject



30
31
32
# File 'lib/vk_cozy/bot.rb', line 30

def on_startup
  puts 'Run polling'
end

#run_polling(startup = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vk_cozy/bot.rb', line 34

def run_polling(startup=nil)
  if startup.nil?
    on_startup
  elsif startup.is_a?(Proc)
    startup.call
  else
    startup
  end
  @polling.listen do |event|
    for event_raw in event['updates']
      begin
        if @labeler.filter(parse_event(event_raw))
          next
        end
      rescue Exception => e 
        raise e
      end
    end
  end
end