This commit is contained in:
syuilo 2017-10-07 04:48:56 +09:00
parent fffea98462
commit 0b20c3caa6

View File

@ -2,13 +2,13 @@ import * as EventEmitter from 'events';
import * as express from 'express'; import * as express from 'express';
import * as request from 'request'; import * as request from 'request';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
//import User from '../../models/user'; import User from '../../models/user';
import config from '../../../conf'; import config from '../../../conf';
import BotCore from '../core'; import BotCore from '../core';
const sessions: Array<{ const sessions: Array<{
sourceId: string; sourceId: string;
session: BotCore; core: BotCore;
}> = []; }> = [];
module.exports = async (app: express.Application) => { module.exports = async (app: express.Application) => {
@ -21,22 +21,43 @@ module.exports = async (app: express.Application) => {
if (ev.message.type !== 'text') return; if (ev.message.type !== 'text') return;
const sourceId = ev.source.userId; const sourceId = ev.source.userId;
let session = sessions.find(s => { let session = sessions.find(s => s.sourceId === sourceId);
return s.sourceId === sourceId;
});
if (!session) { if (!session) {
const user = await User.findOne({
line: {
user_id: sourceId
}
});
let core: BotCore;
if (user) {
core = new BotCore(user);
} else {
core = new BotCore();
core.on('set-user', user => {
User.update(user._id, {
$set: {
line: {
user_id: sourceId
}
}
});
});
}
session = { session = {
sourceId: sourceId, sourceId: sourceId,
session: new BotCore() core: core
}; };
sessions.push(session); sessions.push(session);
} }
const res = await session.session.q(ev.message.text); const res = await session.core.q(ev.message.text);
request({ request.post({
url: 'https://api.line.me/v2/bot/message/reply', url: 'https://api.line.me/v2/bot/message/reply',
headers: { headers: {
'Authorization': `Bearer ${config.line_bot.channel_access_token}` 'Authorization': `Bearer ${config.line_bot.channel_access_token}`