Class: Igni::Bot

Inherits:
Sinatra::Base
  • Object
show all
Includes:
API
Defined in:
lib/igni/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

#getMe, #getUpdates, #request, #sendMessage, #setWebhook

Constructor Details

#initialize(hash) ⇒ Bot

Returns a new instance of Bot.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/igni/bot.rb', line 13

def initialize(hash)
  abort 'No token specified' if hash[:token] == nil
  abort 'No domain specified' if hash[:domain] == nil

  @token = hash[:token]
  @hook = hash[:domain]
  @handler = proc { || }

  puts 'Registering webhook'
  if self.register_webhook
    puts 'Done!'
  else
    abort 'Error while registering webhook!'
  end

  abort 'Wrong token' if @API.getMe['ok'] != true
end

Instance Attribute Details

#APIObject (readonly)

Returns the value of attribute API.



11
12
13
# File 'lib/igni/bot.rb', line 11

def API
  @API
end

#hookObject (readonly)

Returns the value of attribute hook.



10
11
12
# File 'lib/igni/bot.rb', line 10

def hook
  @hook
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#handle(&block) ⇒ Object



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

def handle(&block)
  @handler = proc { |message| block.call(message) }
end

#handlerObject



35
36
37
# File 'lib/igni/bot.rb', line 35

def handler
  @handler
end

#register_webhookObject



52
53
54
55
# File 'lib/igni/bot.rb', line 52

def register_webhook
  @API = Igni::API.new(@token)
  @API.setWebhook(@hook)
end