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

Returns a new instance of SlackConnection.



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

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.



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

def token
  @token
end

Instance Method Details

#channel_id_to_name(channel_id) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 208

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_ids(_force_refresh = false) ⇒ Object



182
183
184
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 182

def channel_ids(_force_refresh = false)
  @user_id_to_name.keys
end

#channel_name_to_id(channel_name) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 216

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

#channel_names(_force_refresh = false) ⇒ Object



186
187
188
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 186

def channel_names(_force_refresh = false)
  @channel_name_to_id.keys
end

#delete_message(channel:, timestamp:) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 128

def delete_message(channel:, timestamp:)
  SlackbotFrd::Log.debug("#{self.class}: Deleting message with timestamp '#{timestamp}' from channel '#{channel}'")

  resp = SlackbotFrd::SlackMethods::ChatDelete.delete(
    token: @token,
    channel: channel_name_to_id(channel),
    timestamp: timestamp
  )

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

#event_idObject



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

def event_id
  @event_id += 1
  @event_id
end

#invite_user(user: user, channel: channel) ⇒ Object



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

def invite_user(user: user, channel: channel)
  SlackbotFrd::Log.debug("#{self.class}: Inviting user '#{user}' to channel '#{channel}'")

  resp = SlackbotFrd::SlackMethods::ChannelsInvite.invite(
    token: @token,
    user: user_name_to_id(user),
    channel: channel_name_to_id(channel),
  )

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

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



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

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(user: u, channel: c, callback: block)
  end
end

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



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

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: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
  end
end

#on_close(&block) ⇒ Object



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

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

#on_connected(&block) ⇒ Object



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

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

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



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

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: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
  end
end

#post_reaction(name:, channel: nil, timestamp: nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 140

def post_reaction(name:, channel: nil, timestamp: nil)
  SlackbotFrd::Log.debug("#{self.class}: Posting reaction '#{name}' to channel '#{channel}' with timestamp '#{timestamp}'")

  resp = SlackbotFrd::SlackMethods::ReactionsAdd.add(
    token: @token,
    name: name,
    channel: channel_name_to_id(channel),
    timestamp: timestamp
  )

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

#restrict_actions_to_channels_joined(value = true) ⇒ Object



165
166
167
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 165

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

#send_message(channel:, message:, username: nil, avatar_emoji: nil, avatar_url: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 114

def send_message(channel:, message:, username: nil, avatar_emoji: nil, avatar_url: nil)
  if username && (avatar_emoji || avatar_url)
    send_message_as_bot(
      channel: channel,
      message: message,
      username: username,
      avatar_emoji: avatar_emoji,
      avatar_url: avatar_url
    )
  else
    send_message_as_user(channel: channel, message: message)
  end
end

#startObject



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
79
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 51

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



190
191
192
193
194
195
196
197
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 190

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_ids(_force_refresh = false) ⇒ Object



174
175
176
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 174

def user_ids(_force_refresh = false)
  @user_id_to_name.keys
end

#user_name_to_id(user_name) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 199

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

#user_names(_force_refresh = false) ⇒ Object



178
179
180
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 178

def user_names(_force_refresh = false)
  @user_name_to_id.keys
end

#users_in_channel(channel) ⇒ Object



169
170
171
172
# File 'lib/slackbot_frd/lib/slack_connection.rb', line 169

def users_in_channel(channel)
  a = SlackMethods::ChannelsInfo.members(token: @token, channel: channel_name_to_id(channel))
  a.map{ |id| user_id_to_name(id) }
end