diff --git a/lib/lita/adapters/slack/message_handler.rb b/lib/lita/adapters/slack/message_handler.rb index b1572b2..d6c07d4 100644 --- a/lib/lita/adapters/slack/message_handler.rb +++ b/lib/lita/adapters/slack/message_handler.rb @@ -39,17 +39,29 @@ def handle attr_reader :type def body - normalized_message = if data["text"] - data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") + normalized_text = nil + if data["text"] + normalized_text = data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") end - normalized_message = remove_formatting(normalized_message) unless normalized_message.nil? + normalized_text = remove_formatting(normalized_text) unless normalized_text.nil? - attachment_text = Array(data["attachments"]).map do |attachment| - attachment["text"] || attachment["fallback"] - end + lines = Array(data["attachments"]).inject([normalized_text]){ + |total, att| total.concat parse_attachment(att) + } + lines.compact.join("\n") + end - ([normalized_message] + attachment_text).compact.join("\n") + def parse_attachment(attachment) + lines = [] + lines << attachment["pretext"] + lines << attachment["title"] + lines << attachment["text"] || attachment["fallback"] + Array(attachment["fields"]).map do |field| + lines << field["title"] + lines << field["value"] + end + lines.compact.map(&:strip).reject(&:empty?) end def remove_formatting(message) @@ -118,7 +130,7 @@ def dispatch_message(user) 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"] } + message.extensions[:slack] = { timestamp: data["ts"], attachments: data["attachments"] } log.debug("Dispatching message to Lita from #{user.id}.") robot.receive(message) end @@ -173,7 +185,10 @@ def handle_reaction item_user = User.find_by_id(data["item_user"]) || User.create(data["item_user"]) # build a payload following slack convention for reactions - payload = { user: user, name: data["reaction"], item_user: item_user, item: data["item"], event_ts: data["event_ts"] } + payload = { + user: user, name: data["reaction"], item_user: item_user, + item: data["item"], event_ts: data["event_ts"] + } # trigger the appropriate slack reaction event robot.trigger("slack_#{type}".to_sym, payload) @@ -195,15 +210,13 @@ def log end # Types of messages Lita should dispatch to handlers. - def supported_message_subtypes - %w(me_message) - end + SUPPORTED_MESSAGE_SUBTYPES = %w(me_message) def supported_subtype? subtype = data["subtype"] if subtype - supported_message_subtypes.include?(subtype) + SUPPORTED_MESSAGE_SUBTYPES.include?(subtype) else true end diff --git a/spec/lita/adapters/slack/message_handler_spec.rb b/spec/lita/adapters/slack/message_handler_spec.rb index c449b0c..e529c36 100644 --- a/spec/lita/adapters/slack/message_handler_spec.rb +++ b/spec/lita/adapters/slack/message_handler_spec.rb @@ -117,22 +117,30 @@ context "when the message has attach" do let(:data) do { - "type" => "message", + "type" =>"message", "channel" => "C2147483705", "user" => "U023BECGF", - "text" => "Hello", - "attachments" => [{"text" => "attached hello"}] + "text" => "Text", + "attachments" => + [{ + "fallback" => "attached fallback", + "text" => "attached text", + "pretext" => "attached pretext", + "title" =>"attached title", + "fields" => [{ "title" => "attached title", "value" => "attached value" }] + }] } end - it "recives attachment text" do + it "receives both serialized and raw attachments" do expect(Lita::Message).to receive(:new).with( robot, - "Hello\nattached hello", + "Text\nattached pretext\nattached title\nattached text\nattached title\nattached value", source ).and_return(message) subject.handle + expect(message.extensions[:slack][:attachments]).to eq(data["attachments"]) end end