0

So I have written a code to download a file from a slack bot

the code is

var https = require('https');
var fs = require('fs');

var pDownload= function (url, dest){
  var slug = url.split('.com').pop();
  console.log(slug);
  var options = {
  "method": "GET",
  "hostname": "files.slack.com",
  "path": slug,
  "rejectUnauthorized": "false",
  "headers": {
      "Authorization": "Bearer MYTOKEN"
    }
  }
  var file = fs.createWriteStream(dest);
  return new Promise((resolve, reject) => {
    var responseSent = false; // flag to make sure that response is sent only once.

    https.get(options, response => {
      response.pipe(file);
      file.on('finish', () =>{
        file.close(() => {
          if(responseSent)  return;
          responseSent = true;
          resolve();
        });
      });
    }).on('error', err => {
        if(responseSent)  return;
        responseSent = true;
        reject(err);
    });
  });
}

module.exports.pDownload=pDownload;
example
pDownload('https://files.slack.com/files-pri/T7JRF8TCN-F7WDP24LV/analysis.r', './test/res.new')
   .then( ()=> console.log('downloaded file no issues...'))
   .catch( e => console.error('error while downloading', e));

however, if a user talks to the bot on direct message, the file that is downloaded only contains 'https://files-origin.slack.com/files-pri/T7JRF8TCN-F7WDP24LV/analysis.r found' and not the contents

But if we are talking to the bot on a channel and upload a file, it correctly downloads the file and stores all the content to the file.

Why is this issue caused? and why is it redirecting to files-origin.slack.com when i have set the scope to files:read and files:write:user

Can the bot download the file when sent a Direct Message?

Rohil
  • 177
  • 1
  • 3
  • 13
  • changing the hostname to `files-origin.slack.com` stores 'Requested file could not be found' in res.new – Rohil Nov 07 '17 at 18:02
  • Can you post some more of your code? Where are you getting that url from? Are you using the private_download url that comes in on the direct message event? You can certainly access files that are sent to your bot via DM, something else is amiss here – Jon Church Nov 15 '17 at 06:47

1 Answers1

0

1) Have you tried sharing the file? Files belonging to other users need to be shared in a channel with your bot to give your bot access to it.

2) It looks to me that you are using a fixed hostname in your code. I would advise against it. Instead use the URL from the url_private property of the file object for downloading. This will work together with your token as authentication.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114