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 Slack.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id, token) ⇒ Bot



9
10
11
12
13
14
15
16
17
18
# File 'lib/bot_mob/bot.rb', line 9

def initialize(user_id, token)
  @user_id = user_id

  if token.nil?
    @state = :inactive
  else
    @token = token
    setup
  end
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#user_idObject (readonly)

Returns the value of attribute user_id.



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

def user_id
  @user_id
end

Instance Method Details

#active?Boolean



70
71
72
# File 'lib/bot_mob/bot.rb', line 70

def active?
  @state != :inactive
end

#api_post(args = {}) ⇒ Object



39
40
41
42
# File 'lib/bot_mob/bot.rb', line 39

def api_post(args = {})
  logger.info("  Sending \"#{args.inspect}\"\n")
  client.web_client.chat_postMessage(args)
end

#closed?Boolean



86
87
88
# File 'lib/bot_mob/bot.rb', line 86

def closed?
  @state == :closed
end

#connectObject



27
28
29
30
# File 'lib/bot_mob/bot.rb', line 27

def connect
  set_state :connecting
  establish_connection
end

#connected?Boolean



82
83
84
# File 'lib/bot_mob/bot.rb', line 82

def connected?
  @state == :connected
end

#disconnected?Boolean



78
79
80
# File 'lib/bot_mob/bot.rb', line 78

def disconnected?
  @state != :connected
end

#establish_connectionObject



20
21
22
23
24
25
# File 'lib/bot_mob/bot.rb', line 20

def establish_connection
  client.start_async
rescue Slack::Web::Api::Error => e
  logger.info e.message
  set_state :inactive
end

#handle(message) ⇒ Object



62
63
64
# File 'lib/bot_mob/bot.rb', line 62

def handle(message)
  # noop
end

#realtime_post(args = {}) ⇒ Object



44
45
46
47
# File 'lib/bot_mob/bot.rb', line 44

def realtime_post(args = {})
  logger.info("  Sending \"#{args[:text]}\"\n")
  client.message(args)
end

#reconnectObject



32
33
34
35
36
37
# File 'lib/bot_mob/bot.rb', line 32

def reconnect
  set_state :reconnecting
  @client = Slack::RealTime::Client.new(token: @token)
  setup
  establish_connection
end

#refreshObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bot_mob/bot.rb', line 49

def refresh
  return unless active?

  case true
  when waiting?
    connect
  when disconnected?
    reconnect
  else
    logger.info "State[#{user_id}] - No Change"
  end
end

#respond?(message) ⇒ Boolean



66
67
68
# File 'lib/bot_mob/bot.rb', line 66

def respond?(message)
  false
end

#waiting?Boolean



74
75
76
# File 'lib/bot_mob/bot.rb', line 74

def waiting?
  @state == :waiting
end