Class: BotMob::Bot
- Inherits:
-
Object
- Object
- BotMob::Bot
- Defined in:
- lib/bot_mob/bot.rb
Overview
## BotMob::Bot
This is the base for all bots that will connect to respond to messages received by your application.
Instance Attribute Summary collapse
-
#network ⇒ Object
readonly
Returns the value of attribute network.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#connect!(network, options = {}) ⇒ Object
##
connectconnectallows you to specify a network to which you are intending to connect your bot. -
#connected?(network = nil) ⇒ Boolean
##
connected?Simple assessment to determine if a given network has been connected. -
#initialize(**options) ⇒ Bot
constructor
A bot does not require any params to be initialized.
-
#name ⇒ Object
##
nameConvenience method to get the class name for the current bot class. -
#receive(message, options = {}) ⇒ Object
##
receiveReceive a message from an implementation of this bot. -
#receive_command(command) ⇒ Object
##
receive_commandReceive a message from an implementation of this bot. -
#valid_command?(name) ⇒ Boolean
##
valid_command?A bot must respond to the given name explicitly declared in the class definition.
Constructor Details
#initialize(**options) ⇒ Bot
A bot does not require any params to be initialized
21 22 23 24 25 |
# File 'lib/bot_mob/bot.rb', line 21 def initialize(**) @connections = {} = [:slack] ? connect!(:slack, [:slack]) : connect!(:roaming) end |
Instance Attribute Details
#network ⇒ Object (readonly)
Returns the value of attribute network.
7 8 9 |
# File 'lib/bot_mob/bot.rb', line 7 def network @network end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/bot_mob/bot.rb', line 7 def end |
Class Method Details
.command(method_name) ⇒ Object
14 15 16 17 |
# File 'lib/bot_mob/bot.rb', line 14 def command(method_name) @commands ||= [] @commands << method_name.to_sym end |
.commands ⇒ Object
10 11 12 |
# File 'lib/bot_mob/bot.rb', line 10 def commands @commands ||= [] end |
Instance Method Details
#connect!(network, options = {}) ⇒ Object
## connect connect allows you to specify a network to which you are intending to connect your bot
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bot_mob/bot.rb', line 30 def connect!(network, = {}) if @connections[network.to_sym] BotMob.logger.warn "#{name} is already connected to #{network}" return end connection = BotMob::Connection.setup network, self, .merge() return false if connection.nil? @connections[network.to_sym] = connection end |
#connected?(network = nil) ⇒ Boolean
## connected? Simple assessment to determine if a given network has been connected
44 45 46 |
# File 'lib/bot_mob/bot.rb', line 44 def connected?(network = nil) network.nil? ? @connections.keys.empty? : !@connections[network.to_sym].nil? end |
#name ⇒ Object
## name Convenience method to get the class name for the current bot class
96 97 98 |
# File 'lib/bot_mob/bot.rb', line 96 def name self.class.to_s end |
#receive(message, options = {}) ⇒ Object
## 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bot_mob/bot.rb', line 56 def receive(, = {}) = BotMob::InboundMessage.prepare(, ) warrant_response = respond?() () if warrant_response || log_responseless_activity? unless warrant_response if log_responseless_activity? BotMob.logger.info(' > Does not warrant response') end return end process_response() end |
#receive_command(command) ⇒ Object
## 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
80 81 82 83 84 85 |
# File 'lib/bot_mob/bot.rb', line 80 def receive_command(command) BotMob.logger.info("#{name} received command at #{Time.now}") BotMob.logger.info(" Parameters: {:text => \"#{command}\"}") send(command) if valid_command?(command) end |
#valid_command?(name) ⇒ Boolean
## valid_command? A bot must respond to the given name explicitly declared in the class definition.
90 91 92 |
# File 'lib/bot_mob/bot.rb', line 90 def valid_command?(name) self.class.commands.include?(name.to_sym) && respond_to?(name.to_sym) end |