Skip to content

Commit 2b3810a

Browse files
committed
add a Slack::Web::Api::Helpers:Files module that has files_upload_v2
1 parent 6669770 commit 2b3810a

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

lib/slack-ruby-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
require_relative 'slack/web/faraday/request'
3838
require_relative 'slack/web/api/mixins'
3939
require_relative 'slack/web/api/endpoints'
40+
require_relative 'slack/web/api/helpers'
4041
require_relative 'slack/web/pagination/cursor'
4142
require_relative 'slack/web/client'
4243

lib/slack/web/api/helpers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'helpers/files'
4+
5+
module Slack
6+
module Web
7+
module Api
8+
module Helpers
9+
include Files
10+
end
11+
end
12+
end
13+
end

lib/slack/web/api/helpers/files.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
module Slack
3+
module Web
4+
module Api
5+
module Helpers
6+
module Files
7+
#
8+
# Uses Slack APIs new sequential file upload flow. This replaces the files.upload method
9+
# @see https://api.slack.com/changelog/2024-04-a-better-way-to-upload-files-is-here-to-stay
10+
# @see https://api.slack.com/messaging/files#uploading_files
11+
#
12+
# @option options [string] :filename
13+
# Name of the file being uploaded.
14+
# @option options [string] :content
15+
# File contents via a POST variable.
16+
# @option options [string] :alt_txt
17+
# Description of image for screen-reader.
18+
# @option options [string] :snippet_type
19+
# Syntax type of the snippet being uploaded.
20+
# @option options [string] :title
21+
# Title of file.
22+
# @option options [string] :channels
23+
# Comma-separated string of channel IDs where the file will be shared. If not specified the file will be private.
24+
# @option options [string] :initial_comment
25+
# The message text introducing the file in specified channels.
26+
# @option options [string] :thread_ts
27+
# Provide another message's ts value to upload this file as a reply. Never use a reply's ts value; use its parent instead. Also make sure to provide only one channel when using 'thread_ts'.
28+
def files_upload_v2(options = {})
29+
%i[filename content channels].each do |param|
30+
raise ArgumentError, "Required argument :#{param} missing" if options[param].nil?
31+
end
32+
33+
content = options[:content]
34+
title = options[:title] || options[:filename]
35+
36+
upload_url_request_params = options.slice(:filename, :alt_txt, :snippet_type)
37+
upload_url_request_params[:length] = content.bytesize
38+
39+
# 1. get the upload url
40+
get_upload_url_response = files_getUploadURLExternal(upload_url_request_params)
41+
upload_url = get_upload_url_response[:upload_url]
42+
file_id = get_upload_url_response[:file_id]
43+
44+
# 2. upload the file
45+
post(upload_url, content)
46+
47+
# 3. complete the upload
48+
complete_upload_request_params = options.slice(:channels, :initial_comment, :thread_ts)
49+
complete_upload_request_params[:files] = [{ id: file_id, title: title }].to_json
50+
51+
files_completeUploadExternal(complete_upload_request_params)
52+
end
53+
end
54+
end
55+
end
56+
end
57+
end

lib/slack/web/client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Client
55
include Faraday::Connection
66
include Faraday::Request
77
include Api::Endpoints
8+
include Api::Helpers
89
include Api::Options
910

1011
attr_accessor(*Config::ATTRIBUTES)

0 commit comments

Comments
 (0)