diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-04-13 06:06:18 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-04-13 06:06:18 +0900 |
| commit | 3368fe855249f45bdf1e4c1e509d325d44e80fbe (patch) | |
| tree | 63c8bc61fb645b1d730b05120ab5117c0fdeee29 /src/server/api/bot/interfaces | |
| parent | wip (diff) | |
| download | sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.tar.gz sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.tar.bz2 sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.zip | |
wip
Diffstat (limited to 'src/server/api/bot/interfaces')
| -rw-r--r-- | src/server/api/bot/interfaces/line.ts | 103 |
1 files changed, 51 insertions, 52 deletions
diff --git a/src/server/api/bot/interfaces/line.ts b/src/server/api/bot/interfaces/line.ts index be3bfe33d3..454630161a 100644 --- a/src/server/api/bot/interfaces/line.ts +++ b/src/server/api/bot/interfaces/line.ts @@ -1,5 +1,5 @@ import * as EventEmitter from 'events'; -import * as express from 'express'; +import * as Router from 'koa-router'; import * as request from 'request'; import * as crypto from 'crypto'; import User from '../../../../models/user'; @@ -158,82 +158,81 @@ class LineBot extends BotCore { } } -module.exports = async (app: express.Application) => { - if (config.line_bot == null) return; +const handler = new EventEmitter(); - const handler = new EventEmitter(); +handler.on('event', async (ev) => { - handler.on('event', async (ev) => { + const sourceId = ev.source.userId; + const sessionId = `line-bot-sessions:${sourceId}`; - const sourceId = ev.source.userId; - const sessionId = `line-bot-sessions:${sourceId}`; + const session = await redis.get(sessionId); + let bot: LineBot; - const session = await redis.get(sessionId); - let bot: LineBot; - - if (session == null) { - const user = await User.findOne({ - host: null, - 'line': { - userId: sourceId - } - }); + if (session == null) { + const user = await User.findOne({ + host: null, + 'line': { + userId: sourceId + } + }); - bot = new LineBot(user); + bot = new LineBot(user); - bot.on('signin', user => { - User.update(user._id, { - $set: { - 'line': { - userId: sourceId - } + bot.on('signin', user => { + User.update(user._id, { + $set: { + 'line': { + userId: sourceId } - }); + } }); + }); - bot.on('signout', user => { - User.update(user._id, { - $set: { - 'line': { - userId: null - } + bot.on('signout', user => { + User.update(user._id, { + $set: { + 'line': { + userId: null } - }); + } }); - - redis.set(sessionId, JSON.stringify(bot.export())); - } else { - bot = LineBot.import(JSON.parse(session)); - } - - bot.on('updated', () => { - redis.set(sessionId, JSON.stringify(bot.export())); }); - if (session != null) bot.refreshUser(); + redis.set(sessionId, JSON.stringify(bot.export())); + } else { + bot = LineBot.import(JSON.parse(session)); + } - bot.react(ev); + bot.on('updated', () => { + redis.set(sessionId, JSON.stringify(bot.export())); }); - app.post('/hooks/line', (req, res, next) => { - // req.headers['x-line-signature'] は常に string ですが、型定義の都合上 - // string | string[] になっているので string を明示しています - const sig1 = req.headers['x-line-signature'] as string; + if (session != null) bot.refreshUser(); + + bot.react(ev); +}); + +// Init router +const router = new Router(); + +if (config.line_bot) { + router.post('/hooks/line', ctx => { + const sig1 = ctx.headers['x-line-signature']; const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret) - .update((req as any).rawBody); + .update(ctx.request.rawBody); const sig2 = hash.digest('base64'); // シグネチャ比較 if (sig1 === sig2) { - req.body.events.forEach(ev => { + ctx.body.events.forEach(ev => { handler.emit('event', ev); }); - - res.sendStatus(200); } else { - res.sendStatus(400); + ctx.status = 400; } }); -}; +} + +module.exports = router; |