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

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#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



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

def active?
  @state != :inactive
end

#api_post(args = {}) ⇒ Object



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

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

#closed?Boolean



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

def closed?
  @state == :closed
end

#connectObject



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

def connect
  logger.info "State[#{user_id}] - connecting"
  establish_connection
end

#connected?Boolean



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

def connected?
  @state == :connected
end

#disconnected?Boolean



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

def disconnected?
  @state != :connected
end

#establish_connectionObject



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

def establish_connection
  client.start_async
rescue Slack::Web::Api::Error => e
  logger.info e.message
  logger.info "State[#{user_id}] - disabled"
  @state = :inactive
end

#handle(message) ⇒ Object



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

def handle(message)
  # noop
end

#realtime_post(args = {}) ⇒ Object



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

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

#reconnectObject



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

def reconnect
  logger.info "State[#{user_id}] - reconnecting"
  @client = Slack::RealTime::Client.new(token: @token)
  setup
  establish_connection
end

#refreshObject



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

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



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

def respond?(message)
  false
end

#waiting?Boolean



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

def waiting?
  @state == :waiting
end