From 6fd207d23650486805c11f6357ad5d867ace1610 Mon Sep 17 00:00:00 2001 From: LittleC Date: Mon, 17 Jul 2023 01:38:18 +0800 Subject: [PATCH 1/2] feat(line): set webhook endpoint fix(line): handle multiple bots --- adapters/line/src/bot.ts | 7 +++++++ adapters/line/src/http.ts | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/adapters/line/src/bot.ts b/adapters/line/src/bot.ts index be0517fb..fbe55bbf 100644 --- a/adapters/line/src/bot.ts +++ b/adapters/line/src/bot.ts @@ -32,6 +32,13 @@ export class LineBot extends Bot { this.internal = new Internal(this.http) } + async initialize(callback: (bot: this) => Promise) { + const { userId } = await this.internal.getBotInfo() + this.selfId = userId + await callback(this) + this.online() + } + // https://developers.line.biz/en/reference/messaging-api/#get-profile async getSelf(): Promise { const { userId, displayName, pictureUrl } = await this.internal.getBotInfo() diff --git a/adapters/line/src/http.ts b/adapters/line/src/http.ts index abcf01df..ba3129a4 100644 --- a/adapters/line/src/http.ts +++ b/adapters/line/src/http.ts @@ -12,20 +12,20 @@ export class HttpServer extends Adapter.Server { } async start(bot: LineBot) { - const { userId } = await bot.internal.getBotInfo() - bot.selfId = userId - bot.online() bot.ctx.router.post('/line', async (ctx) => { const sign = ctx.headers['x-line-signature']?.toString() - const hash = crypto.createHmac('SHA256', bot.config.secret).update(ctx.request.rawBody || '').digest('base64') + const parsed = ctx.request.body as WebhookRequestBody + const { destination } = parsed + const localBot = this.bots.find(bot => bot.selfId === destination) + if (!localBot) return ctx.status = 403 + const hash = crypto.createHmac('SHA256', localBot?.config?.secret).update(ctx.request.rawBody || '').digest('base64') if (hash !== sign) { return ctx.status = 403 } - const parsed = ctx.request.body as WebhookRequestBody this.logger.debug(require('util').inspect(parsed, false, null, true)) for (const event of parsed.events) { - const sessions = await adaptSessions(bot, event) - if (sessions.length) sessions.forEach(bot.dispatch.bind(bot)) + const sessions = await adaptSessions(localBot, event) + if (sessions.length) sessions.forEach(localBot.dispatch.bind(localBot)) this.logger.debug(require('util').inspect(sessions, false, null, true)) } ctx.status = 200 @@ -45,5 +45,11 @@ export class HttpServer extends Adapter.Server { ctx.response.body = resp.data ctx.status = 200 }) + bot.initialize(async (bot) => { + await bot.internal.setWebhookEndpoint({ + endpoint: bot.ctx.root.config.selfUrl + '/line', + }) + this.logger.debug('listening updates %c', 'line:' + bot.selfId) + }) } } From cca4dadc05111bc8b4144e6c01db53ad4bf12b16 Mon Sep 17 00:00:00 2001 From: LittleC Date: Mon, 17 Jul 2023 03:49:11 +0800 Subject: [PATCH 2/2] feat(line): bot info --- adapters/line/src/bot.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adapters/line/src/bot.ts b/adapters/line/src/bot.ts index fbe55bbf..a712d424 100644 --- a/adapters/line/src/bot.ts +++ b/adapters/line/src/bot.ts @@ -33,8 +33,10 @@ export class LineBot extends Bot { } async initialize(callback: (bot: this) => Promise) { - const { userId } = await this.internal.getBotInfo() + const { userId, pictureUrl, displayName } = await this.internal.getBotInfo() this.selfId = userId + this.username = displayName + if (pictureUrl) this.avatar = pictureUrl await callback(this) this.online() }