Module: Slack::Web::Api::Helpers::Files

Included in:
Slack::Web::Api::Helpers
Defined in:
lib/slack/web/api/helpers/files.rb

Instance Method Summary collapse

Instance Method Details

#files_upload_v2(params = {}) ⇒ Object

Uses Slack APIs new sequential file upload flow. This replaces the files.upload method

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :filename (string)

    Name of the file being uploaded.

  • :content (string)

    File contents via a POST variable.

  • :files (Array<Hash>)

    Array of file objects with :filename, :content, and optionally :title, :alt_txt, :snippet_type.

  • :alt_txt (string)

    Description of image for screen-reader.

  • :snippet_type (string)

    Syntax type of the snippet being uploaded.

  • :title (string)

    Title of file.

  • :channel_id (string)

    Channel ID where the file will be shared. If not specified the file will be private.

  • :channel (string)

    Channel where the file will be shared, alias of channel_id. If not specified the file will be private.

  • :channels (string)

    Comma-separated string of channel IDs where the file will be shared. If not specified the file will be private.

  • :initial_comment (string)

    The message text introducing the file in specified channels.

  • :blocks (string)

    A JSON-based array of structured rich text blocks, presented as a URL-encoded string. If the initial_comment field is provided, the blocks field is ignored.

  • :thread_ts (string)

    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’.

Raises:

  • (ArgumentError)

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/slack/web/api/helpers/files.rb', line 39

def files_upload_v2(params = {})
  files_to_upload = if params[:files] && params[:files].is_a?(Array)
                      params[:files]
                    else
                      [params.slice(:filename, :content, :title, :alt_txt, :snippet_type)]
                    end

  files_to_upload.each_with_index do |file, index|
    %i[filename content].each do |param|
      raise ArgumentError, "Required argument :#{param} missing in file (#{index})" if file[param].nil?
    end
  end

  channel_params = %i[channel channels channel_id].map { |param| params[param] }.compact
  raise ArgumentError, 'Only one of :channel, :channels, or :channel_id is required' if channel_params.size > 1

  complete_upload_request_params = params.slice(:initial_comment, :blocks, :thread_ts)

  if params[:channels]
    complete_upload_request_params[:channels] = Array(params[:channels]).map do |channel|
      conversations_id(channel: channel)['channel']['id']
    end.uniq.join(',')
  elsif params[:channel]
    complete_upload_request_params[:channel_id] = conversations_id(
      channel: params[:channel]
    )['channel']['id']
  elsif params[:channel_id]
    complete_upload_request_params[:channel_id] = params[:channel_id]
  end

  uploaded_files = files_to_upload.map do |file|
    content = file[:content]
    title = file[:title] || file[:filename]

    upload_url_request_params = file.slice(:filename, :alt_txt, :snippet_type)
    upload_url_request_params[:length] = content.bytesize

    get_upload_url_response = files_getUploadURLExternal(upload_url_request_params)
    upload_url = get_upload_url_response[:upload_url]
    file_id = get_upload_url_response[:file_id]

    ::Faraday::Connection.new(upload_url, options) do |connection|
      connection.request :multipart
      connection.request :url_encoded
      connection.use ::Slack::Web::Faraday::Response::WrapError
      connection.response :logger, logger if logger
      connection.adapter adapter
    end.post do |request|
      request.body = content
    end

    { id: file_id, title: title }
  end

  complete_upload_request_params[:files] = uploaded_files.to_json
  files_completeUploadExternal(complete_upload_request_params)
end