Class: BotMob::Bot

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(**options)
  @connections = {}
  @options = options
  options[:slack] ? connect!(:slack, options[:slack]) : connect!(:roaming)
end

Instance Attribute Details

#networkObject (readonly)

Returns the value of attribute network.



7
8
9
# File 'lib/bot_mob/bot.rb', line 7

def network
  @network
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/bot_mob/bot.rb', line 7

def options
  @options
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

.commandsObject



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, 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?(network = nil) ⇒ Boolean

## connected? Simple assessment to determine if a given network has been connected

Returns:

  • (Boolean)


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

#nameObject

## 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(message, options = {})
  inbound_message = BotMob::InboundMessage.prepare(message, options)

  warrant_response = respond?(inbound_message)
  log_message(inbound_message) 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(inbound_message)
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.

Returns:

  • (Boolean)


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