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



100
101
102
# File 'lib/bot_mob/bot.rb', line 100

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
# 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(command, params = {}) ⇒ 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



77
78
79
80
81
82
83
84
85
86
87
88
89
# 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?(name) ⇒ Boolean

## valid_command? A bot must respond to the given name explicitly declared in the class definition.

Returns:

  • (Boolean)


94
95
96
# 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