Class: SlackbotFrd::SlackConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/slackbot_frd/lib/slack_connection.rb

Constant Summary collapse

FILE_PATH =
File.expand_path(__FILE__)
APP_ROOT =
File.expand_path(File.dirname(File.dirname(FILE_PATH)))
FILE_DIR =
File.dirname(FILE_PATH)
LOG_FILE =
"#{APP_ROOT}/bp-slackbot.log"
PID_FILE_NAME =
"#{APP_ROOT}/bp-slackbot.pid"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, errors_file) ⇒ SlackConnection



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 26

def initialize(token, errors_file)
  unless token
    log_and_add_to_error_file("No token passed to #{self.class}")
  end

  @token = token
  @errors_file = errors_file
  @event_id = 0
  @on_connected_callbacks = []
  @on_disconnected_callbacks = []
  @on_message_callbacks = UserChannelCallbacks.new
  @on_channel_left_callbacks = UserChannelCallbacks.new
  @on_channel_joined_callbacks = UserChannelCallbacks.new

  # These hashes are used to map ids to names efficiently
  @user_id_to_name = {}
  @user_name_to_id = {}
  @channel_id_to_name = {}
  @channel_name_to_id = {}

  restrict_actions_to_channels_joined
  SlackbotFrd::Log::debug("Done initializing #{self.class}")
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



24
25
26
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 24

def token
  @token
end

Instance Method Details

#channel_id_to_name(channel_id) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 171

def channel_id_to_name(channel_id)
  unless @channel_id_to_name && @channel_id_to_name.has_key?(channel_id)
    refresh_channel_info
  end
  SlackbotFrd::Log::warn("#{self.class}: Channel id '#{channel_id}' not found") unless @channel_id_to_name.include?(channel_id)
  @channel_id_to_name[channel_id]
end

#channel_name_to_id(channel_name) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 179

def channel_name_to_id(channel_name)
  return channel_name if channel_name == :any
  nc = normalize_channel_name(channel_name)
  unless @channel_name_to_id && @channel_name_to_id.has_key?(nc)
    refresh_channel_info
  end
  SlackbotFrd::Log::warn("#{self.class}: Channel name '#{nc}' not found") unless @channel_name_to_id.include?(nc)
  @channel_name_to_id[nc]
end

#event_idObject



80
81
82
83
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 80

def event_id
  @event_id += 1
  @event_id
end

#on_channel_joined(user = :any, channel = :any, &block) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 105

def on_channel_joined(user = :any, channel = :any, &block)
  wrap_user_or_channel_lookup_on_callback('on_message_channel_joined', user, channel) do
    u = user_name_to_id(user)
    c = channel_name_to_id(channel)
    @on_channel_joined_callbacks.add(u, c, block)
  end
end

#on_channel_left(user = :any, channel = :any, &block) ⇒ Object



99
100
101
102
103
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 99

def on_channel_left(user = :any, channel = :any, &block)
  wrap_user_or_channel_lookup_on_callback('on_message_channel_left', user, channel) do
    @on_channel_left_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
  end
end

#on_close(&block) ⇒ Object



89
90
91
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 89

def on_close(&block)
  @on_disconnected_callbacks.push(block)
end

#on_connected(&block) ⇒ Object



85
86
87
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 85

def on_connected(&block)
  @on_connected_callbacks.push(block)
end

#on_message(user = :any, channel = :any, &block) ⇒ Object



93
94
95
96
97
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 93

def on_message(user = :any, channel = :any, &block)
  wrap_user_or_channel_lookup_on_callback('on_message', user, channel) do
    @on_message_callbacks.add(user_name_to_id(user), channel_name_to_id(channel), block)
  end
end

#restrict_actions_to_channels_joined(value = true) ⇒ Object



149
150
151
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 149

def restrict_actions_to_channels_joined(value = true)
  @restrict_actions_to_channels_joined = value
end

#send_message(channel, message, username, avatar, avatar_is_emoji) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 134

def send_message(channel, message, username, avatar, avatar_is_emoji)
  SlackbotFrd::Log::debug("#{self.class}: Sending message '#{message}' as user '#{username}' to channel '#{channel}'")

  resp = SlackbotFrd::SlackMethods::ChatPostMessage.postMessage(
    @token,
    channel_name_to_id(channel),
    message,
    username,
    avatar,
    avatar_is_emoji
  )

  SlackbotFrd::Log::debug("#{self.class}: Received response:  #{resp}")
end

#send_message_as_user(channel, message) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 113

def send_message_as_user(channel, message)
  unless @ws
    log_and_add_to_error_file("Cannot send message '#{message}' as user to channel '#{channel}' because not connected to wss stream")
  end

  SlackbotFrd::Log::debug("#{self.class}: Sending message '#{message}' as user to channel '#{channel}'")

  begin
    resp = @ws.send({
      id: event_id,
      type: "message",
      channel: channel_name_to_id(channel),
      text: message
    }.to_json)

    SlackbotFrd::Log::debug("#{self.class}: Received response:  #{resp}")
  rescue SocketError => e
    log_and_add_to_error_file(socket_error_message(e))
  end
end

#startObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 50

def start
  # Write pid file
  File.write(PID_FILE_NAME, "#{Process.pid}")

  SlackbotFrd::Log::info("#{self.class}: starting event machine")

  EM.run do
    begin
      wss_url = SlackbotFrd::SlackMethods::RtmStart.wss_url(@token)
    rescue SocketError => e
      log_and_add_to_error_file(socket_error_message(e))
    end

    unless wss_url
      log_and_add_to_error_file("No Real Time stream opened by slack.  Check for network connection and correct authentication token")
      return
    end
    @ws = Faye::WebSocket::Client.new(wss_url)

    @on_connected_callbacks.each    { |callback| @ws.on(:open,  &callback) }
    @on_disconnected_callbacks.each { |callback| @ws.on(:close, &callback) }
    @ws.on(:message) { |event| process_message_received(event) }

    # Clean up our pid file
    @ws.on(:close) { |event| File.delete(PID_FILE_NAME) }
  end

  SlackbotFrd::Log::debug("#{self.class}: event machine started")
end

#user_id_to_name(user_id) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 153

def user_id_to_name(user_id)
  return user_id if user_id == :any || user_id == :bot
  unless @user_id_to_name && @user_id_to_name.has_key?(user_id)
    
  end
  SlackbotFrd::Log::warn("#{self.class}: User id '#{user_id}' not found") unless @user_id_to_name.include?(user_id)
  @user_id_to_name[user_id]
end

#user_name_to_id(user_name) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 162

def user_name_to_id(user_name)
  return user_name if user_name == :any || user_name == :bot
  unless @user_name_to_id && @user_name_to_id.has_key?(user_name)
    
  end
  SlackbotFrd::Log::warn("#{self.class}: User name '#{user_name}' not found") unless @user_name_to_id.include?(user_name)
  @user_name_to_id[user_name]
end