Class: SlackBotServer::Server
- Inherits:
-
Object
- Object
- SlackBotServer::Server
- Includes:
- Logging
- Defined in:
- lib/slack_bot_server/server.rb
Overview
Implements a server for running multiple Slack bots. Bots can be dynamically added and removed, and can be interacted with from external services (like your application).
To use this, you should create a script to run along side your application. A simple example:
#!/usr/bin/env ruby
require 'slack_bot_server'
require 'slack_bot_server/redis_queue'
require 'slack_bot_server/simple_bot'
# Use a Redis-based queue to add/remove bots and to trigger
# bot messages to be sent
queue = SlackBotServer::RedisQueue.new
# Create a new server using that queue
server = SlackBotServer::Server.new(queue: queue)
# How your application-specific should be created when the server
# is told about a new slack api token to connect with
server.on_add do |token|
# Return a new bot instance to the server. `SimpleBot` is a provided
# example bot with some very simple behaviour.
SlackBotServer::SimpleBot.new(token: token)
end
# Start the server. This method blocks, and will not return until
# the server is killed.
server.start
The key features are:
-
creating a queue as a conduit for commands from your app
-
creating an instance of Server with that queue
-
defining an #on_add block, which is run whenever you need to start a new bot. This block contains the custom code relevant to your particular service, most typically the instantiation of a bot class that implements the logic you want
-
calling #start, to actually run the server and start listening for commands from the queue and connecting bots to Slack itself
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
-
#add_bot(*args) ⇒ Object
Adds a bot to this server Calls the block given to #on_add with the arguments given.
-
#bot(key) ⇒ Object
Find a bot added to this server.
-
#initialize(queue: SlackBotServer::LocalQueue.new) ⇒ Server
constructor
Creates a new Server.
-
#on_add(&block) ⇒ Object
Define the block which should be called when the #add_bot method is called, or the
add_botmessage is sent via a queue. -
#remove_bot(key) ⇒ Object
Stops and removes a bot from the server.
-
#start ⇒ Object
Starts the server.
-
#start_in_background ⇒ Object
Starts the server in the background, via a Thread.
Methods included from Logging
#debug, #log, #log_error, #log_string
Constructor Details
#initialize(queue: SlackBotServer::LocalQueue.new) ⇒ Server
Creates a new SlackBotServer::Server
59 60 61 62 63 64 |
# File 'lib/slack_bot_server/server.rb', line 59 def initialize(queue: SlackBotServer::LocalQueue.new) @queue = queue @bots = {} @add_proc = -> (token) { SlackBotServer::SimpleBot.new(token: token) } @running = false end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
54 55 56 |
# File 'lib/slack_bot_server/server.rb', line 54 def queue @queue end |
Instance Method Details
#add_bot(*args) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/slack_bot_server/server.rb', line 109 def add_bot(*args) bot = @add_proc.call(*args) if bot.respond_to?(:start) && !bot(bot.key) log "adding bot #{bot}" @bots[bot.key] = bot bot.start if @running end rescue => e log_error(e) end |
#bot(key) ⇒ Object
Find a bot added to this server. Returns nil if no bot was found
101 102 103 |
# File 'lib/slack_bot_server/server.rb', line 101 def bot(key) @bots[key] end |
#on_add(&block) ⇒ Object
Define the block which should be called when the #add_bot method is called, or the add_bot message is sent via a queue. This block should return a bot (which responds to start), in which case it will be added and started. If anything else is returned, it will be ignored.
70 71 72 |
# File 'lib/slack_bot_server/server.rb', line 70 def on_add(&block) @add_proc = block end |
#remove_bot(key) ⇒ Object
Stops and removes a bot from the server
123 124 125 126 127 128 129 130 |
# File 'lib/slack_bot_server/server.rb', line 123 def remove_bot(key) if (bot = bot(key)) bot.stop @bots.delete(key) end rescue => e log_error(e) end |
#start ⇒ Object
Starts the server. This method will not return; call it at the end of your server script. It will start all bots it knows about (i.e. bots added via #add_bot before the server was started), and then listen for new instructions.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/slack_bot_server/server.rb', line 79 def start EM.run do @running = true @bots.each do |key, bot| begin bot.start rescue => e log_error(e) end end listen_for_instructions if queue end end |
#start_in_background ⇒ Object
Starts the server in the background, via a Thread
94 95 96 |
# File 'lib/slack_bot_server/server.rb', line 94 def start_in_background Thread.start { start } end |