question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Helper function to download a file

See original GitHub issue

I suggest adding a method on the Web API to download a file from slack, symmetrical to files.upload.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
aoberoicommented, Oct 3, 2017

i think the SDK could provide a helper function to download actual files. what i’m imagining is a function web.helpers.fileDownload(fileId, [opts]) and the opts could allow you to specify if you want the return value to be a readable stream or a Buffer.

4reactions
clavincommented, Feb 9, 2017

First off, this repository is for handling the client that uses the Web API, which is produced by some lovely engineers at Slack. We have no control over what is and isn’t in the API. Nonetheless, such a method (one that downloads files) isn’t exactly possible with the Web API (can’t force a client to download something, you can only send data).

But, there is a way to do what you’re looking for: simply use an http client (such as request) to go to the file’s url (which you can get using the files.list method) and save the file’s response to disk.

Further, there’s a special little bit of authentication you need to do in your request to get the file (using it’s url_private field), as specified by the file type documentation. Simply, there must be an Authorization header present in the request that’s set to Bearer YOUR_TOKEN (which may be the bot’s token or a OAuth token).

Here’s an example of what all of that would look like:

const fs = require('fs');
const request = require('request');
const WebClient = require('@slack/client').WebClient;

const web = new WebClient(your token and options);

web.files.list({}, function(err, fileListResp) {
  if (err)
    return console.log(err);

  // Strategically choose a file to download.
  let filePrivateUrl = fileListResp.files[Math.floor(
    Math.random() * fileListResp.files.length)].url_private;

  // If you don't want to write the returned data to a file then just remove the
  // `pipe` call and supply `request` with a callback.
  request({
    url: filePrivateUrl,
    headers: {
      'Authorization': 'Bearer ' + YOUR_TOKEN // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
  }).pipe(fs.createWriteStream('./thefile'));
});

I didn’t test the code, it could error. I’m also unaware of any situation in which a file doesn’t have a url_private field. You’ll have to do your own testing to make sure the above approach is sound (sorry). It should still be a solid basis for what you’re attempting to achieve. Good luck!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Download file helper - php - Stack Overflow
I wanna download my file that i have upload to my web Im just saving the name of the file to database. My...
Read more >
Download Helper — CodeIgniter 3.1.13 documentation
Generates server headers which force data to be downloaded to your desktop. Useful with file downloads. The first parameter is the name you...
Read more >
download.file function - RDocumentation
The function download.file can be used to download a single file as described by url from the internet and store it in destfile...
Read more >
How to download a file using Node.js? - GeeksforGeeks
Downloading a file using node js can be done using inbuilt packages or with third party libraries. Method 1: Using 'https' and 'fs'...
Read more >
Download a file from the internet using the R functions ...
Then use the function download.file(url, filename) to download the file. Here, url is a string containing the URL of the file, and filename...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found