Class: Slacky::Bot
- Inherits:
-
Object
- Object
- Slacky::Bot
- Defined in:
- lib/slacky/bot.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#slack_id ⇒ Object
readonly
Returns the value of attribute slack_id.
Instance Method Summary collapse
- #at(cron, &block) ⇒ Object
- #blowup(user, data, args, &respond) ⇒ Object
- #handle_channel(message) ⇒ Object
- #handle_im(message) ⇒ Object
-
#initialize(config) ⇒ Bot
constructor
A new instance of Bot.
- #known_commands ⇒ Object
- #name ⇒ Object
- #on(type, &block) ⇒ Object
- #on_command(command, &block) ⇒ Object
- #on_im(attrs, &block) ⇒ Object
- #on_message(attrs, &block) ⇒ Object
- #populate_channels ⇒ Object
- #populate_users ⇒ Object
- #run ⇒ Object
- #stay_alive ⇒ Object
- #web_client ⇒ Object
Constructor Details
#initialize(config) ⇒ Bot
Returns a new instance of Bot.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/slacky/bot.rb', line 10 def initialize(config) config.log "#{config.name} is starting up..." puts "#{config.name} is starting up..." @config = config @command_handlers = [] @channel_handlers = [] @im_handlers = [] @raw_handlers = [] @cron_handlers = [] unless @config.slack_api_token @config.log "No Slack API token found. Use environment variable SLACK_API_TOKEN." return end Slack.configure do |slack_cfg| slack_cfg.token = @config.slack_api_token end @client = Slack::RealTime::Client.new auth = web_client.auth_test if auth.ok @slack_id = auth.user_id @config.log "Slackbot is active!" else @config.log "Slackbot is doomed :-(" return end @bookkeeper = Bookkeeper.new @client Channel.bot = self Message.bot = self populate_users populate_channels stay_alive end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/slacky/bot.rb', line 8 def client @client end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/slacky/bot.rb', line 8 def config @config end |
#slack_id ⇒ Object (readonly)
Returns the value of attribute slack_id.
8 9 10 |
# File 'lib/slacky/bot.rb', line 8 def slack_id @slack_id end |
Instance Method Details
#at(cron, &block) ⇒ Object
82 83 84 |
# File 'lib/slacky/bot.rb', line 82 def at(cron, &block) @cron_handlers << { cron: cron, handler: block } end |
#blowup(user, data, args, &respond) ⇒ Object
233 234 235 236 237 238 |
# File 'lib/slacky/bot.rb', line 233 def blowup(user, data, args, &respond) respond.call "Tick... tick... tick... BOOM! Goodbye." EM.next_tick do raise "kablammo!" end end |
#handle_channel(message) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/slacky/bot.rb', line 86 def handle_channel() handled = false if .command? @command_handlers.each do |h| command, handler = h.values_at :command, :handler next unless command == .command handler.call handled = true end end return if handled @channel_handlers.each do |h| match, channels, handler = h.values_at :match, :channels, :handler accept = Channel.find channels next if accept && ! accept.include?(.channel) next if match && ! match === handler.call end end |
#handle_im(message) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/slacky/bot.rb', line 109 def handle_im() unless .user.slack_im_id == .channel.slack_id .user.slack_im_id = .channel.slack_id .user.save end handled = false @command_handlers.each do |h| command, handler = h.values_at :command, :handler next unless command == .command handler.call handled = true end return if handled @im_handlers.each do |h| match, handler = h.values_at :match, :handler next if match && ! match === handler.call end end |
#known_commands ⇒ Object
60 61 62 |
# File 'lib/slacky/bot.rb', line 60 def known_commands @command_handlers.map { |ch| ch[:command] } end |
#name ⇒ Object
56 57 58 |
# File 'lib/slacky/bot.rb', line 56 def name @config.down_name end |
#on(type, &block) ⇒ Object
64 65 66 |
# File 'lib/slacky/bot.rb', line 64 def on(type, &block) @raw_handlers << { type: type, handler: block } end |
#on_command(command, &block) ⇒ Object
68 69 70 |
# File 'lib/slacky/bot.rb', line 68 def on_command(command, &block) @command_handlers << { command: command.downcase, handler: block } end |
#on_im(attrs, &block) ⇒ Object
77 78 79 80 |
# File 'lib/slacky/bot.rb', line 77 def on_im(attrs, &block) attrs ||= {} @im_handlers << { match: attrs[:match], handler: block } end |
#on_message(attrs, &block) ⇒ Object
72 73 74 75 |
# File 'lib/slacky/bot.rb', line 72 def (attrs, &block) attrs ||= {} @channel_handlers << { match: attrs[:match], channels: attrs[:channels], handler: block } end |
#populate_channels ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/slacky/bot.rb', line 201 def populate_channels print "Getting channels from Slack..." resp = web_client.channels_list throw resp unless resp.ok resp.channels.map do |channel| Channel.channel channel end resp = web_client.groups_list throw resp unless resp.ok resp.groups.map do |group| Channel.group group end puts " done!" rescue => e puts " error: #{e.}" raise e end |
#populate_users ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/slacky/bot.rb', line 179 def populate_users print "Getting users from Slack..." resp = web_client.users_list throw resp unless resp.ok User.invalidate_all_users whitelist = @config.whitelist_users || [] resp.members.map do |member| next unless member.profile.email # no bots next if member.deleted # no ghosts unless whitelist.include?(member.id) || whitelist.include?(member.name) || whitelist.include?("@#{member.name}") next if member.is_ultra_restricted # no single channel guests next if member.is_restricted # no multi channel guests either end user = User.find(member.id) || User.new(slack_id: member.id) user.populate(member).validate.save end puts " done!" rescue => e puts " error: #{e.}" raise e end |
#run ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/slacky/bot.rb', line 133 def run @bookkeeper.keep_the_books @client.on :message do |data| next unless ( user = User.find data.user ) next unless user.valid? channel = Channel.find data.channel channel = Channel.im data.channel, user if data.channel =~ /^D/ && ! channel next unless channel reject = Channel.find @config.slack_reject_channels next if reject && reject.find { |c| c.slack_id == data.channel } accept = Channel.find @config.slack_accept_channels next if accept && ! accept.find { |c| c.slack_id == data.channel } = Message.new(user, channel, data) handle_channel if [ :group, :channel ].include? channel.type handle_im if [ :im ].include? channel.type end @raw_handlers.each do |h| type, handler = h.values_at :type, :handler @client.on type do |data| handler.call data end end Thread.abort_on_exception = true @cron_thread ||= Thread.new do EM.run do @cron_handlers.each do |h| cron, handler = h.values_at :cron, :handler EM::Cron.schedule cron do |time| handler.call end end end end puts "#{@config.name} is listening to: #{@config.slack_accept_channels}" @client.start! end |
#stay_alive ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/slacky/bot.rb', line 220 def stay_alive at '* * * * *' do @client.ping stamp: Time.now.to_f end on :pong do |data| now = Time.now.to_f stamp = data.stamp delta = now - stamp @config.log "Slow ping pong response: #{delta}s" if delta > 5 end end |
#web_client ⇒ Object
52 53 54 |
# File 'lib/slacky/bot.rb', line 52 def web_client @client.web_client end |