sharkey/src/api/bot/interfaces/line.ts

104 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-10-07 03:36:46 +09:00
import * as EventEmitter from 'events';
import * as express from 'express';
2017-10-07 04:30:57 +09:00
import * as request from 'request';
2017-10-07 03:36:46 +09:00
import * as crypto from 'crypto';
2017-10-07 04:48:56 +09:00
import User from '../../models/user';
2017-10-07 03:36:46 +09:00
import config from '../../../conf';
2017-10-07 04:30:57 +09:00
import BotCore from '../core';
2017-10-07 05:50:01 +09:00
import _redis from '../../../db/redis';
import prominence = require('prominence');
2017-10-07 03:36:46 +09:00
2017-10-07 05:50:01 +09:00
const redis = prominence(_redis);
2017-10-07 04:30:57 +09:00
2017-10-07 03:36:46 +09:00
module.exports = async (app: express.Application) => {
if (config.line_bot == null) return;
const handler = new EventEmitter();
2017-10-07 04:30:57 +09:00
handler.on('message', async (ev) => {
// テキスト以外(スタンプなど)は無視
if (ev.message.type !== 'text') return;
const sourceId = ev.source.userId;
2017-10-07 05:50:01 +09:00
const sessionId = `line-bot-sessions:${sourceId}`;
2017-10-07 04:30:57 +09:00
2017-10-07 05:50:01 +09:00
const _session = await redis.get(sessionId);
let session: BotCore;
if (_session == null) {
2017-10-07 04:48:56 +09:00
const user = await User.findOne({
line: {
user_id: sourceId
}
});
if (user) {
2017-10-07 05:50:01 +09:00
session = new BotCore(user);
2017-10-07 04:48:56 +09:00
} else {
2017-10-07 05:50:01 +09:00
session = new BotCore();
session.on('set-user', user => {
2017-10-07 04:48:56 +09:00
User.update(user._id, {
$set: {
line: {
user_id: sourceId
}
}
});
});
}
2017-10-07 05:50:01 +09:00
redis.set(sessionId, JSON.stringify(session.export()));
} else {
session = BotCore.import(JSON.parse(_session));
2017-10-07 04:30:57 +09:00
}
2017-10-07 05:50:01 +09:00
session.on('updated', () => {
redis.set(sessionId, JSON.stringify(session.export()));
});
const res = await session.q(ev.message.text);
2017-10-07 04:30:57 +09:00
2017-10-07 05:50:01 +09:00
// 返信
2017-10-07 04:48:56 +09:00
request.post({
2017-10-07 04:30:57 +09:00
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;
}
});
});
2017-10-07 03:36:46 +09:00
app.post('/hooks/line', (req, res, next) => {
2017-10-07 04:30:57 +09:00
// req.headers['x-line-signature'] は常に string ですが、型定義の都合上
2017-10-07 03:36:46 +09:00
// string | string[] になっているので string を明示しています
2017-10-07 04:30:57 +09:00
const sig1 = req.headers['x-line-signature'] as string;
2017-10-07 03:36:46 +09:00
2017-10-07 04:30:57 +09:00
const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret)
.update((req as any).rawBody);
2017-10-07 03:36:46 +09:00
const sig2 = hash.digest('base64');
// シグネチャ比較
if (sig1 === sig2) {
2017-10-07 04:30:57 +09:00
req.body.events.forEach(ev => {
handler.emit(ev.type, ev);
});
2017-10-07 03:36:46 +09:00
res.sendStatus(200);
} else {
res.sendStatus(400);
}
});
};