Skip to content

Commit

Permalink
Merge pull request #255 from cofacts/batch-multicast
Browse files Browse the repository at this point in the history
Batch multicast
  • Loading branch information
MrOrz committed May 19, 2021
2 parents e7de113 + 47942f3 commit 55d7fb7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
34 changes: 31 additions & 3 deletions src/lib/__tests__/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rollbar from 'src/lib/rollbar';
import sendMessage from '../sendMessage';

beforeEach(() => {
fetch.mockClear();
fetch.mockReset();
rollbar.error.mockClear();
});

Expand All @@ -29,8 +29,12 @@ it('send message using LINE Notify', () => {
`);
});

it('send message using multicast api', () => {
sendMessage.multicast(
it('send message using multicast api', async () => {
fetch.mockImplementation(() =>
Promise.resolve({ json: () => Promise.resolve({ status: 200 }) })
);

await sendMessage.multicast(
['userId1', 'userId2', 'userId3'],
[{ type: 'text', text: 'message' }]
);
Expand All @@ -49,6 +53,30 @@ it('send message using multicast api', () => {
],
]
`);

// Test batching
fetch.mockClear();
await sendMessage.multicast(
Array.from(Array(501)).map((_, id) => `user${id}`),
[{ type: 'text', text: 'message' }]
);

expect(fetch.mock.calls).toHaveLength(2);

// The snapshot of the "second batch", which should only contain user500
expect(fetch.mock.calls[1]).toMatchInlineSnapshot(`
Array [
"https://api.line.me/v2/bot/message/multicast",
Object {
"body": "{\\"to\\":[\\"user500\\"],\\"messages\\":[{\\"type\\":\\"text\\",\\"text\\":\\"message\\"}]}",
"headers": Object {
"Authorization": "Bearer ",
"Content-Type": "application/json",
},
"method": "POST",
},
]
`);
});

it('send message using push api', () => {
Expand Down
35 changes: 29 additions & 6 deletions src/lib/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,52 @@ 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 = async (userIds, messages) => {
lineClient.post('/message/multicast', {
to: userIds,
messages: 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
* @param {string} userId
* @param {object[]} messages - Array of line message objects, max size:5
*/
const push = async (userId, messages) => {
const push = (userId, messages) =>
lineClient.post('/message/push', {
to: userId,
messages: messages,
});
};

export default {
notify,
Expand Down

0 comments on commit 55d7fb7

Please sign in to comment.