|
| 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. |
| 28 | + # Never use a reply's ts value; use its parent instead. |
| 29 | + # Also make sure to provide only one channel when using 'thread_ts'. |
| 30 | + def files_upload_external(options = {}) |
| 31 | + %i[filename content channels].each do |param| |
| 32 | + raise ArgumentError, "Required argument :#{param} missing" if options[param].nil? |
| 33 | + end |
| 34 | + |
| 35 | + content = options[:content] |
| 36 | + title = options[:title] || options[:filename] |
| 37 | + |
| 38 | + upload_url_request_params = options.slice(:filename, :alt_txt, :snippet_type) |
| 39 | + upload_url_request_params[:length] = content.bytesize |
| 40 | + |
| 41 | + # Get the upload url. |
| 42 | + get_upload_url_response = files_getUploadURLExternal(upload_url_request_params) |
| 43 | + upload_url = get_upload_url_response[:upload_url] |
| 44 | + file_id = get_upload_url_response[:file_id] |
| 45 | + |
| 46 | + # Upload the file. |
| 47 | + plain_text_connection.post do |request| |
| 48 | + request.url upload_url |
| 49 | + request.body = content |
| 50 | + end |
| 51 | + |
| 52 | + # Complete the upload. |
| 53 | + complete_upload_request_params = options.slice(:channels, :initial_comment, :thread_ts) |
| 54 | + complete_upload_request_params[:files] = [{ id: file_id, title: title }].to_json |
| 55 | + |
| 56 | + files_completeUploadExternal(complete_upload_request_params) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments