Skip to content

Commit

Permalink
Merge 774294d into 9f71d65
Browse files Browse the repository at this point in the history
  • Loading branch information
nonumpa committed Feb 26, 2020
2 parents 9f71d65 + 774294d commit 5464062
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,29 @@ const singleUserHandler = async (
req,
type,
replyToken,
timeout,
userId,
otherFields
) => {
// reply before timeout
// the reply token becomes invalid after a certain period of time
// https://developers.line.biz/en/reference/messaging-api/#send-reply-message
let isReplied = false;
setTimeout(function() {
if (isReplied) return;

isReplied = true;
lineClient('/message/reply', {
replyToken,
messages: [
{
type: 'text',
text: t`Line bot is busy, or we cannot handle this message. Maybe you can try again a few minutes later.`,
},
],
});
}, timeout);

if (userIdBlacklist.indexOf(userId) !== -1) {
// User blacklist
console.log(
Expand Down Expand Up @@ -184,6 +204,8 @@ const singleUserHandler = async (
// Send replies. Does not need to wait for lineClient's callbacks.
// lineClient's callback does error handling by itself.
//
if (isReplied) return;
isReplied = true;
lineClient('/message/reply', {
replyToken,
messages: result.replies,
Expand All @@ -208,9 +230,18 @@ router.post('/callback', ctx => {

ctx.request.body.events.forEach(
async ({ type, replyToken, source, ...otherFields }) => {
// set 28s timeout
const timeout = 28000;
let { userId } = source;
if (source.type === 'user') {
singleUserHandler(ctx.request, type, replyToken, userId, otherFields);
singleUserHandler(
ctx.request,
type,
replyToken,
timeout,
userId,
otherFields
);
} else if (source.type === 'group') {
groupHandler(ctx.request, type, replyToken, userId, otherFields);
}
Expand Down

0 comments on commit 5464062

Please sign in to comment.