Skip to content

Commit

Permalink
[lib] sendMessage.multicast supports batching when >= 500 ids are given
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed May 18, 2021
1 parent e3aa81c commit ec23199
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/lib/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,41 @@ const notify = async (token, message) => {
lineNotify(token, { message: message });
};

/**
* Split large array into list of batches, with max size being batchSize for each batch
* @param {Array<*>} array
* @param {number} batchSize
* @returns {Array<Array<*>>}
*/
function batch(array, batchSize) {
return array.reduce(
(batches, item) => {
if (batches[batches.length - 1].length >= batchSize) {
batches.push([]);
}
batches[batches.length - 1].push(item);
return batches;
},
[[]]
);
}

/**
* https://developers.line.biz/en/reference/messaging-api/#send-multicast-message
* @param {string[]} userIds
* @param {object[]} messages - Array of line message objects, max size:5
*/
const multicast = (userIds, messages) =>
lineClient.post('/message/multicast', {
to: userIds,
messages: messages,
});
const multicast = async (userIds, messages) => {
for (const userIdBatch of batch(
userIds,
500 /* Multicast can send to 500 ppl in max each time */
)) {
await lineClient.post('/message/multicast', {
to: userIdBatch,
messages: messages,
});
}
};

/**
* https://developers.line.biz/en/reference/messaging-api/#send-push-message
Expand Down

0 comments on commit ec23199

Please sign in to comment.