class Lita::Adapters::Slack::MessageHandler

@api private

Attributes

data[R]
robot[R]
robot_id[R]
type[R]

Public Class Methods

new(robot, robot_id, data) click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 6
def initialize(robot, robot_id, data)
  @robot = robot
  @robot_id = robot_id
  @data = data
  @type = data["type"]
end

Public Instance Methods

handle() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 13
def handle
  case type
  when "hello"
    handle_hello
  when "message"
    handle_message
  when "reaction_added", "reaction_removed"
    handle_reaction
  when "user_change", "team_join"
    handle_user_change
  when "bot_added", "bot_changed"
    handle_bot_change
  when "channel_created", "channel_rename", "group_rename"
    handle_channel_change
  when "error"
    handle_error
  else
    handle_unknown
  end
end

Private Instance Methods

body() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 41
def body
  normalized_message = if data["text"]
    data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}")
  end

 normalized_message = remove_formatting(normalized_message) unless normalized_message.nil?

  attachment_text = Array(data["attachments"]).map do |attachment|
    attachment["text"] || attachment["fallback"]
  end

  ([normalized_message] + attachment_text).compact.join("\n")
end
channel() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 111
def channel
  data["channel"]
end
dispatch_message(user) click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 115
def dispatch_message(user)
  source = Source.new(user: user, room: channel)
  source.private_message! if channel && channel[0] == "D"
  message = Message.new(robot, body, source)
  message.command! if source.private_message?
  message.extensions[:slack] = { timestamp: data["ts"] }
  log.debug("Dispatching message to Lita from #{user.id}.")
  robot.receive(message)
end
from_self?(user) click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 125
def from_self?(user)
  user.id == robot_id
end
handle_bot_change() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 129
def handle_bot_change
  log.debug("Updating user data for bot.")
  UserCreator.create_user(SlackUser.from_data(data["bot"]), robot, robot_id)
end
handle_channel_change() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 134
def handle_channel_change
  log.debug("Updating channel data.")
  RoomCreator.create_room(SlackChannel.from_data(data["channel"]), robot)
end
handle_error() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 139
def handle_error
  error = data["error"]
  code = error["code"]
  message = error["msg"]
  log.error("Error with code #{code} received from Slack: #{message}")
end
handle_hello() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 146
def handle_hello
  log.info("Connected to Slack.")
  robot.trigger(:connected)
end
handle_message() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 151
def handle_message
  return unless supported_subtype?
  return if data["user"] == 'USLACKBOT'

  user = User.find_by_id(data["user"]) || User.create(data["user"])

  return if from_self?(user)

  dispatch_message(user)
end
handle_reaction() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 162
def handle_reaction
  log.debug "#{type} event received from Slack"

  # find or create user
  user = User.find_by_id(data["user"]) || User.create(data["user"])

  # avoid processing reactions added/removed by self
  return if from_self?(user)

  # build a payload following slack convention for reactions
  payload = { user: user, name: data["reaction"], item: data["item"], event_ts: data["event_ts"] }

  # trigger the appropriate slack reaction event
  robot.trigger("slack_#{type}".to_sym, payload)
end
handle_unknown() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 178
def handle_unknown
  unless data["reply_to"]
    log.debug("#{type} event received from Slack and will be ignored.")
  end
end
handle_user_change() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 184
def handle_user_change
  log.debug("Updating user data.")
  UserCreator.create_user(SlackUser.from_data(data["user"]), robot, robot_id)
end
log() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 189
def log
  Lita.logger
end
remove_formatting(message) click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 55
def remove_formatting(message)
  # https://api.slack.com/docs/formatting
  message = message.gsub(/
      <                    # opening angle bracket
      (?<type>[@#!])?      # link type
      (?<link>[^>|]+)      # link
      (?:\|                # start of |label (optional)
          (?<label>[^>]+)  # label
      )?                   # end of label
      >                    # closing angle bracket
      /ix) do
    link  = Regexp.last_match[:link]
    label = Regexp.last_match[:label]

    case Regexp.last_match[:type]
      when '@'
        if label
          label
        else
          user = User.find_by_id(link)
          if user
            "@#{user.mention_name}"
          else
            "@#{link}"
          end
        end

      when '#'
        if label
          label
        else
          channel = Lita::Room.find_by_id(link)
          if channel
            "\##{channel.name}"
          else
            "\##{link}"
          end
        end

      when '!'
        "@#{link}" if ['channel', 'group', 'everyone'].include? link
      else
        link = link.gsub /^mailto:/, ''
        if label && !(link.include? label)
          "#{label} (#{link})"
        else
          label == nil ? link : label
        end
    end
  end
  message.gsub('&lt;', '<')
         .gsub('&gt;', '>')
         .gsub('&amp;', '&')

end
supported_message_subtypes() click to toggle source

Types of messages Lita should dispatch to handlers.

# File lib/lita/adapters/slack/message_handler.rb, line 194
def supported_message_subtypes
  %w(me_message)
end
supported_subtype?() click to toggle source
# File lib/lita/adapters/slack/message_handler.rb, line 198
def supported_subtype?
  subtype = data["subtype"]

  if subtype
    supported_message_subtypes.include?(subtype)
  else
    true
  end
end