Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from Shopify/add-attachments-to-extensions
Browse files Browse the repository at this point in the history
Add attachments to extensions
  • Loading branch information
ridwanmsharif authored Jan 26, 2018
2 parents 0d0569f + b0ffefc commit 84b522e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
39 changes: 26 additions & 13 deletions lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 84b522e

Please sign in to comment.