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.



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

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.



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

def api
  @api
end

Instance Method Details

#onObject



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

def on
  return @labeler
end

#on_startupObject



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

def on_startup
  puts 'Run polling'
end

#run_polling(startup = nil) ⇒ Object



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

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
        event_raw = parse_event(event_raw)
        if @labeler.filter(event_raw)
          next
        end
      rescue Exception => e 
        raise e
      end
    end
  end
end