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?