Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Ability to send content as a file. #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/lita/adapters/slack/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ def send_messages(channel_id, messages)
)
end

def send_file_content(content, filename, filetype = "text", title = nil,
initial_comment = nil, channels = nil)

call_api(
"files.upload",
content: content,
filetype: filetype,
filename: filename,
title: title,
initial_comment: initial_comment,
channels: channels
)
end

def set_topic(channel, topic)
call_api("channels.setTopic", channel: channel, topic: topic)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/lita/adapters/slack/chat_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def send_attachments(target, attachments)
api.send_attachments(target, Array(attachments))
end
alias_method :send_attachment, :send_attachments

# @param content [String] File contents.
# @param filename [String] File name.
# @param filetype [String] Slack file type. Default "text".
# @param title [String] File title. Default nil.
# @param initial_comment [String] Initial comment for file. Default nil.
# @param channels [String] Comma-separated list of channel ids. Default nil.
# @return [void]
def send_file_content(content, filename, filetype = "text", title = nil,
initial_comment = nil, channels = nil)
api.send_file_content(content, filename, filetype, title,
initial_comment, channels)
end
end
end
end
Expand Down
77 changes: 77 additions & 0 deletions spec/lita/adapters/slack/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,83 @@ def stubs(postMessage_options = {})
end
end

describe "#send_file_content" do
let(:channels) { 'C1234567890' }
let(:comment) { 'First post!' }
let(:content) { 'lorem ipsum' }
let(:filename) { 'xyz.txt' }
let(:filetype) { 'text' }
let(:title) { 'XYZ Text File' }
let(:stubs) do
Faraday::Adapter::Test::Stubs.new do |stub|
stub.post(
'https://slack.com/api/files.upload',
token: token,
content: content,
filetype: filetype,
filename: filename,
title: title,
initial_comment: comment,
channels: channels
) do
[http_status, {}, http_response]
end
end
end

context "with a successful response" do
let(:http_response) do
MultiJson.dump({
ok: true,
file: {
id: "F12345678",
created: 1456523515,
timestamp: 1456523515,
name: "xyz.txt",
title: "XYZ Text File",
mimetype: "text/plain",
filetype: "text"
}
})
end

it "returns a response with the file id" do
response = subject.send_file_content(content, filename, filetype, title,
comment, channels)

expect(response['file']['id']).to eq("F12345678")
end
end

context "with a Slack error" do
let(:http_response) do
MultiJson.dump({
ok: false,
error: 'invalid_auth'
})
end

it "raises a RuntimeError" do
expect { subject.send_file_content(content, filename, filetype, title,
comment, channels) }.to raise_error(
"Slack API call to files.upload returned an error: invalid_auth."
)
end
end

context "with an HTTP error" do
let(:http_status) { 422 }
let(:http_response) { '' }

it "raises a RuntimeError" do
expect { subject.send_file_content(content, filename, filetype, title,
comment, channels) }.to raise_error(
"Slack API call to files.upload failed with status code 422: ''. Headers: {}"
)
end
end
end

describe "#set_topic" do
let(:channel) { 'C1234567890' }
let(:topic) { 'Topic' }
Expand Down
17 changes: 17 additions & 0 deletions spec/lita/adapters/slack/chat_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@
subject.send_attachment(room, attachment)
end
end

describe "#send_file_content" do
let(:channels) { 'C1234567890' }
let(:comment) { 'First post!' }
let(:content) { 'lorem ipsum' }
let(:filename) { 'xyz.txt' }
let(:filetype) { 'text' }
let(:title) { 'XYZ Text File' }

it "can send file content" do
expect(subject.api).to receive(:send_file_content).with(content, filename,
filetype, title, comment, channels)

subject.send_file_content(content, filename, filetype, title, comment,
channels)
end
end
end