Class: RSlack::Bot

Inherits:
Object
  • Object
show all
Includes:
CronApplicable
Defined in:
lib/rslack/bot.rb,
lib/rslack/actions/cron.rb

Defined Under Namespace

Modules: CronApplicable

Constant Summary collapse

STORAGE_KEYNAME =
'rslack'
STORAGE_INTERVAL =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, log: nil) ⇒ Bot



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rslack/bot.rb', line 36

def initialize(token, log: nil)
  @client  = APIClient.new(token)
  @logger  = Logger.new(log || STDOUT)
  @memory  = {}

  @routes  = self.class.instance_variable_get(:@routes)
  @storage = LevelDB::DB.new(File.join(Dir.pwd, "#{STORAGE_KEYNAME}.db"))

  @auth = @client.auth
  unless @auth['ok']
    @logger.fatal("#{self.class} Failed to access slack web api.")
    exit 1
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



28
29
30
# File 'lib/rslack/bot.rb', line 28

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



29
30
31
# File 'lib/rslack/bot.rb', line 29

def logger
  @logger
end

#memoryObject (readonly)

Returns the value of attribute memory.



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

def memory
  @memory
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Class Method Details

.register(route) ⇒ Object



17
18
19
# File 'lib/rslack/bot.rb', line 17

def register(route)
  routes.push(route)
end

Instance Method Details

#force_stopObject



99
100
101
# File 'lib/rslack/bot.rb', line 99

def force_stop
  EM.stop
end

#linked_nameObject



55
56
57
# File 'lib/rslack/bot.rb', line 55

def linked_name
  "<@#{@auth['user_id']}>"
end

#nameObject



51
52
53
# File 'lib/rslack/bot.rb', line 51

def name
  "@#{@auth['user']}"
end

#react(msg) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rslack/bot.rb', line 63

def react(msg)
  count = 0
  @routes.each do |route|
    matched = route.regexp.match(msg.text)
    next unless matched
    next unless route.matchall || msg.prefix == linked_name

    count += 1
    message = msg.dup
    message.var = OpenStruct.new(matched.named_captures)
    logger.info("#{self.class} passed msg to: '#{route.action}'.")

    th = Thread.start do
      begin
        action = route.action.new(self)
        action.instance_exec(message.freeze, &route.callback)
      rescue => e
        reply(msg, text: "_#{e}._")
      end
    end
    th.abort_on_exception = true
  end

  reply(msg, text: 'I have no idea what to do...') if msg.prefix == linked_name && count.zero?
end

#reply(msg, **opts) ⇒ Object



59
60
61
# File 'lib/rslack/bot.rb', line 59

def reply(msg, **opts)
  @client.post(**msg.to_h.merge(**opts))
end

#restartObject



103
104
105
106
# File 'lib/rslack/bot.rb', line 103

def restart
  stop
  start
end

#startObject



89
90
91
92
# File 'lib/rslack/bot.rb', line 89

def start
  load_storage
  EM.run { wait_for_message }
end

#stopObject



94
95
96
97
# File 'lib/rslack/bot.rb', line 94

def stop
  EM.stop
  dump_storage
end