class BotMob::Bot
## BotMob::Bot
This is the base for all bots that will connect to respond to messages received by your application.
Attributes
Public Class Methods
# File lib/bot_mob/bot.rb, line 14 def command(method_name) @commands ||= [] @commands << method_name.to_sym end
# File lib/bot_mob/bot.rb, line 10 def commands @commands ||= [] end
A bot does not require any params to be initialized
# File lib/bot_mob/bot.rb, line 21 def initialize(**options) @connections = {} @options = options options[:slack] ? connect!(:slack, options[:slack]) : connect!(:roaming) end
Public Instance Methods
## `connect` `connect` allows you to specify a network to which you are intending to connect your bot
# File lib/bot_mob/bot.rb, line 30 def connect!(network, options = {}) if @connections[network.to_sym] BotMob.logger.warn "#{name} is already connected to #{network}" return end connection = BotMob::Connection.setup network, self, @options.merge(options) return false if connection.nil? @connections[network.to_sym] = connection end
## `connected?` Simple assessment to determine if a given network has been connected
# File lib/bot_mob/bot.rb, line 44 def connected?(network = nil) network.nil? ? @connections.keys.empty? : !@connections[network.to_sym].nil? end
## `name` Convenience method to get the class name for the current bot class
# File lib/bot_mob/bot.rb, line 100 def name self.class.to_s end
## `receive` Receive a message from an implementation of this bot. This is the primary entry point for your bot to accept input.
bot = BotMob.new bot.receive('foo')
Specify message as the received data
# File lib/bot_mob/bot.rb, line 56 def receive(message, options = {}) inbound_message = BotMob::InboundMessage.prepare(message, options) BotMob.logger.log_request(self, :message, inbound_message.params) BotMob.logger.info('-> Received message was from a bot') unless inbound_message.human? unless respond?(inbound_message) BotMob.logger.info('-> Does not warrant response') return end process_response(inbound_message) end
## `receive_command` Receive a message from an implementation of this bot. This is the primary entry point for your bot to accept input.
bot = BotMob.new bot.receive('foo')
Specify message as the received data
# File lib/bot_mob/bot.rb, line 77 def receive_command(command, params = {}) BotMob.logger.log_request(self, :command, command: command) return unless valid_command?(command) begin send(command, params) rescue ArgumentError => e BotMob.logger.warn("#{name}##{command} is not accepting parameters") BotMob.logger.warn("ArgumentError - #{e.message}") send(command) end end
## `valid_command?` A bot must respond to the given name explicitly declared in the class definition.
# File lib/bot_mob/bot.rb, line 94 def valid_command?(name) self.class.commands.include?(name.to_sym) && respond_to?(name.to_sym) end
Private Instance Methods
## `deliver`
`deliver` takes the provided outbound message and executes the necessary actions to ensure the message arrives to the bot's network
# File lib/bot_mob/bot.rb, line 147 def deliver(outbound_message) outbound_network = outbound_message.network # Assume they want the only connected network if only one exists outbound_network ||= @connections.values.first if @connections.values.one? raise BotMob::UnspecifiedTargetNetworkError if outbound_network.nil? connection = @connections[outbound_network.to_sym] raise BotMob::UnconnectedTargetNetworkError if connection.nil? connection.deliver(outbound_message) end
## `log_responseless_activity?`
Log all received messages regardless of the result of `respond?`
Defaults to true in development
# File lib/bot_mob/bot.rb, line 165 def log_responseless_activity? BotMob.env.development? end
## `process_response`
Begin the process of responding to a given message
# File lib/bot_mob/bot.rb, line 137 def process_response(message) response = respond(message) outbound_message = BotMob::OutboundMessage.prepare(response, inbound: message) deliver(outbound_message) end
## `respond`
`respond` accepts an inbound message, does whatever processing it needs, generates an outbound message and passes the message to the `deliver` method, which delegates to the currently assigned network.
A respond method that is not overloaded will return the outbound message
# File lib/bot_mob/bot.rb, line 125 def respond(inbound_message) raise BotMob::UndefinedResponseError if BotMob.env.production? BotMob::OutboundMessage.new( body: "ACK - #{inbound_message.body}", network: inbound_message.network ) end
## `respond?`
`respond?` is the determining method of the involvement of a bot. It should be a lightweight check of it's own logic against the contents of the inbound message. Commonly used for checking language used or regex matching of the inbound messages' contents.
Defaults to true for human messages
# File lib/bot_mob/bot.rb, line 114 def respond?(inbound_message) inbound_message.human? end