main();
function main() {
const token = configs.get("Token");
const channel = configs.get("ChannelName");
const file = engine.findDataByNumber(configs.get("File"));
// const comment = configs.get("Comment");
//check whether files exist or not
if (file == null){
engine.log ("No File to upload");
return;
}
//check whether number of files is under the limit
const numOfFiles = file.length;
if (numOfFiles > httpClient.getRequestingLimit()){
throw "Number of Files is over the limit.";
}
for (let i = 0; i < numOfFiles; i++){
upload(token,file[i],channel);
}
}
//Upload and output log process
function upload (token,file,channel){
let response = httpClient.begin()
.bearer(token)
.multipart('file',file)
.multipart('channels',channel)
.post("https://slack.com/api/files.upload");
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
const responseJson = JSON.parse(responseTxt);
if (responseJson.ok == false ) {
const error = file.getName() + "\nFailed to upload \n" + responseTxt;
throw error;
}
engine.log(file.getName());
engine.log("status:" + status);
engine.log(responseTxt);
}