diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-10-07 04:30:57 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-10-07 04:30:57 +0900 |
| commit | fffea98462b7ba3250118b4bdf4e5678cf6e4ba7 (patch) | |
| tree | 29d9eb9f5ddbe674eb0c2e27009ddd5726421334 /src/api/bot | |
| parent | :v: (diff) | |
| download | sharkey-fffea98462b7ba3250118b4bdf4e5678cf6e4ba7.tar.gz sharkey-fffea98462b7ba3250118b4bdf4e5678cf6e4ba7.tar.bz2 sharkey-fffea98462b7ba3250118b4bdf4e5678cf6e4ba7.zip | |
:v:
Diffstat (limited to 'src/api/bot')
| -rw-r--r-- | src/api/bot/core.ts | 4 | ||||
| -rw-r--r-- | src/api/bot/interfaces/line.ts | 65 |
2 files changed, 56 insertions, 13 deletions
diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 002ac1b06e..47989dbaa2 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -4,11 +4,11 @@ import * as bcrypt from 'bcryptjs'; import User, { IUser } from '../models/user'; export default class BotCore extends EventEmitter { - public user: IUser; + public user: IUser = null; private context: Context = null; - constructor(user: IUser) { + constructor(user?: IUser) { super(); this.user = user; diff --git a/src/api/bot/interfaces/line.ts b/src/api/bot/interfaces/line.ts index 4bee844c12..8c7d6acfd5 100644 --- a/src/api/bot/interfaces/line.ts +++ b/src/api/bot/interfaces/line.ts @@ -1,34 +1,77 @@ import * as EventEmitter from 'events'; import * as express from 'express'; +import * as request from 'request'; import * as crypto from 'crypto'; //import User from '../../models/user'; import config from '../../../conf'; -/*import BotCore from '../core'; +import BotCore from '../core'; -const sessions: { - userId: string; +const sessions: Array<{ + sourceId: string; session: BotCore; -}[] = []; -*/ +}> = []; + module.exports = async (app: express.Application) => { if (config.line_bot == null) return; const handler = new EventEmitter(); + handler.on('message', async (ev) => { + // テキスト以外(スタンプなど)は無視 + if (ev.message.type !== 'text') return; + + const sourceId = ev.source.userId; + let session = sessions.find(s => { + return s.sourceId === sourceId; + }); + + if (!session) { + session = { + sourceId: sourceId, + session: new BotCore() + }; + + sessions.push(session); + } + + const res = await session.session.q(ev.message.text); + + request({ + url: 'https://api.line.me/v2/bot/message/reply', + headers: { + 'Authorization': `Bearer ${config.line_bot.channel_access_token}` + }, + json: { + replyToken: ev.replyToken, + messages: [{ + type: 'text', + text: res + }] + } + }, (err, res, body) => { + if (err) { + console.error(err); + return; + } + }); + }); + app.post('/hooks/line', (req, res, next) => { - // req.headers['X-Line-Signature'] は常に string ですが、型定義の都合上 + // req.headers['x-line-signature'] は常に string ですが、型定義の都合上 // string | string[] になっているので string を明示しています - const sig1 = req.headers['X-Line-Signature'] as string; + const sig1 = req.headers['x-line-signature'] as string; - const hash = crypto.createHmac('sha256', config.line_bot.channel_secret) - .update(JSON.stringify(req.body)); + const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret) + .update((req as any).rawBody); const sig2 = hash.digest('base64'); // シグネチャ比較 if (sig1 === sig2) { - console.log(req.body); - handler.emit(req.body.type); + req.body.events.forEach(ev => { + handler.emit(ev.type, ev); + }); + res.sendStatus(200); } else { res.sendStatus(400); |