Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/line'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 19, 2023
2 parents 57a29df + cca4dad commit 407382f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 9 additions & 0 deletions adapters/line/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export class LineBot extends Bot<LineBot.Config> {
this.internal = new Internal(this.http)
}

async initialize(callback: (bot: this) => Promise<void>) {
const { userId, pictureUrl, displayName } = await this.internal.getBotInfo()
this.selfId = userId
this.username = displayName
if (pictureUrl) this.avatar = pictureUrl
await callback(this)
this.online()
}

// https://developers.line.biz/en/reference/messaging-api/#get-profile
async getSelf(): Promise<Universal.User> {
const { userId, displayName, pictureUrl } = await this.internal.getBotInfo()
Expand Down
20 changes: 13 additions & 7 deletions adapters/line/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ export class HttpServer extends Adapter.Server<LineBot> {
}

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
Expand All @@ -45,5 +45,11 @@ export class HttpServer extends Adapter.Server<LineBot> {
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)
})
}
}

0 comments on commit 407382f

Please sign in to comment.