Class: Igni::Bot

Inherits:
Object
  • 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
30
# File 'lib/igni/bot.rb', line 13

def initialize(hash)
  puts "Igni version " + Igni::VERSION

  abort 'No token specified' if hash[:token] == nil
  abort 'No domain specified' if hash[:domain] == nil

  @token = hash[:token]
  @hook = hash[:domain]

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

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

Instance Attribute Details

#hookObject (readonly)

Returns the value of attribute hook.



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

def hook
  @hook
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#on(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/igni/bot.rb', line 32

def on(&block)
  @app = Sinatra.new do
    post "/webhook/#{ENV['IGNI_TOKEN']}" do
      raw = request.env["rack.input"].read

      data = JSON.parse(raw)
      data["raw"] = raw

      puts raw

      block.call(data)
    end
  end

  @app.run!
end

#register_webhookObject



49
50
51
# File 'lib/igni/bot.rb', line 49

def register_webhook
  self.setWebhook(@hook)
end