I use Node Cisco Spark Client (https://github.com/marchfederico/node-sparkclient) for sending message with any attachments. I can send 1 attachment, but not many.
This is my code : Can you help me please ?
function postMessage(roomId,message,nbfichiers) {
return new Promise(function (fulfill, reject){
var messageParams = {}
var sanitizedMessage = '>'+message.replace(/\n/g,"<br>")
if (nbfichiers.length > 0){
console.log("Nombres de pièces jointes : " + nbfichiers.length);
for(var b=0; b < nbfichiers.length; b++){
messageParams.file = fs.readFileSync('uploads/'+nbfichiers[b]+'');
messageParams.filename = nbfichiers[b];
messageParams.markdown = true
sparkClient.createMessage(roomId,sanitizedMessage,messageParams,function(err,message){
if (err) {
reject(err)
}
else {
fulfill(message)
}
});
fs.unlinkSync('uploads/'+nbfichiers[b]);
}
}
})
}
By using a loop inside a new Promise(...)
you will only get the result of the first iteration when you consume the promise, is it really the behaviour that you are expecting ?
According to the node-sparkclient documentation you can only send one attachment at a time. You could send multiple attachments by using Promise.all
the following way :
function postAttachment(roomId, message, file) {
return new Promise(function (fullfill, reject){
var messageParams = {}
var sanitizedMessage = '>'+message.replace(/\n/g,"<br>")
messageParams.file = fs.readFileSync('uploads/'+file);
messageParams.filename = file;
messageParams.markdown = true
sparkClient.createMessage(roomId, sanitizedMessage, messageParams, function(err,message){
if (err) {
reject(err)
}
else {
fs.unlinkSync('uploads/'+ file);
fullfill(message)
}
});
})
}
function postAttachments(roomId, message, files) {
return Promise.all(files.map(file => postAttachment(roomId, message, file));
}
If you want to send only one attachment with all the files, you could merge your files before sending them.