iceshrimp-legacy/src/server/api/bot/interfaces/line.ts

239 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-10-06 20:36:46 +02:00
import * as EventEmitter from 'events';
2018-04-12 23:06:18 +02:00
import * as Router from 'koa-router';
2017-10-06 21:30:57 +02:00
import * as request from 'request';
2017-10-06 20:36:46 +02:00
import * as crypto from 'crypto';
2018-03-29 13:32:18 +02:00
import User from '../../../../models/user';
2018-04-02 06:15:53 +02:00
import config from '../../../../config';
2017-10-06 21:30:57 +02:00
import BotCore from '../core';
2018-03-28 18:20:40 +02:00
import _redis from '../../../../db/redis';
2017-10-06 22:50:01 +02:00
import prominence = require('prominence');
2018-04-02 06:44:32 +02:00
import getAcct from '../../../../acct/render';
import parseAcct from '../../../../acct/parse';
2018-04-07 19:30:37 +02:00
import getNoteSummary from '../../../../renderers/get-note-summary';
2018-04-05 18:36:34 +02:00
import getUserName from '../../../../renderers/get-user-name';
2017-10-06 20:36:46 +02:00
2017-10-06 22:50:01 +02:00
const redis = prominence(_redis);
2017-10-06 21:30:57 +02:00
2017-10-07 12:09:10 +02:00
// SEE: https://developers.line.me/media/messaging-api/messages/sticker_list.pdf
const stickers = [
'297',
'298',
'299',
'300',
'301',
'302',
'303',
'304',
'305',
'306',
'307'
];
2017-10-07 11:30:04 +02:00
class LineBot extends BotCore {
private replyToken: string;
private reply(messages: any[]) {
request.post({
url: 'https://api.line.me/v2/bot/message/reply',
headers: {
'Authorization': `Bearer ${config.line_bot.channel_access_token}`
},
json: {
replyToken: this.replyToken,
messages: messages
}
}, (err, res, body) => {
if (err) {
console.error(err);
return;
}
});
}
public async react(ev: any): Promise<void> {
2017-10-07 11:31:55 +02:00
this.replyToken = ev.replyToken;
2017-10-07 20:24:10 +02:00
switch (ev.type) {
// メッセージ
case 'message':
switch (ev.message.type) {
// テキスト
case 'text':
const res = await this.q(ev.message.text);
if (res == null) return;
// 返信
this.reply([{
type: 'text',
text: res
}]);
break;
// スタンプ
case 'sticker':
// スタンプで返信
this.reply([{
type: 'sticker',
packageId: '4',
stickerId: stickers[Math.floor(Math.random() * stickers.length)]
}]);
break;
}
break;
2018-04-07 19:30:37 +02:00
// noteback
case 'noteback':
const data = ev.noteback.data;
2017-10-07 20:24:10 +02:00
const cmd = data.split('|')[0];
const arg = data.split('|')[1];
switch (cmd) {
case 'showtl':
2018-04-07 19:30:37 +02:00
this.showUserTimelineNoteback(arg);
2017-10-07 20:24:10 +02:00
break;
}
break;
2017-10-07 12:09:10 +02:00
}
2017-10-07 11:30:04 +02:00
}
public static import(data) {
const bot = new LineBot();
bot._import(data);
return bot;
}
public async showUserCommand(q: string) {
2018-03-27 09:51:12 +02:00
const user = await require('../../endpoints/users/show')(parseAcct(q.substr(1)), this.user);
2017-10-07 11:30:04 +02:00
2018-03-27 09:51:12 +02:00
const acct = getAcct(user);
2017-10-07 20:24:10 +02:00
const actions = [];
actions.push({
2018-04-07 19:30:37 +02:00
type: 'noteback',
2017-10-07 20:24:10 +02:00
label: 'タイムラインを見る',
data: `showtl|${user.id}`
});
2018-04-07 20:58:11 +02:00
if (user.twitter) {
2017-10-07 20:24:10 +02:00
actions.push({
type: 'uri',
label: 'Twitterアカウントを見る',
2018-04-07 20:58:11 +02:00
uri: `https://twitter.com/${user.twitter.screenName}`
2017-10-07 20:24:10 +02:00
});
}
actions.push({
type: 'uri',
label: 'Webで見る',
2018-03-27 09:51:12 +02:00
uri: `${config.url}/@${acct}`
2017-10-07 20:24:10 +02:00
});
2017-10-07 11:30:04 +02:00
this.reply([{
type: 'template',
altText: await super.showUserCommand(q),
template: {
type: 'buttons',
2018-03-29 07:48:47 +02:00
thumbnailImageUrl: `${user.avatarUrl}?thumbnail&size=1024`,
2018-04-05 18:36:34 +02:00
title: `${getUserName(user)} (@${acct})`,
2017-10-07 11:30:04 +02:00
text: user.description || '(no description)',
2017-10-07 20:24:10 +02:00
actions: actions
2017-10-07 11:30:04 +02:00
}
}]);
2017-11-11 20:48:16 +01:00
return null;
2017-10-07 11:30:04 +02:00
}
2017-10-07 12:09:10 +02:00
2018-04-07 19:30:37 +02:00
public async showUserTimelineNoteback(userId: string) {
const tl = await require('../../endpoints/users/notes')({
2018-03-29 07:48:47 +02:00
userId: userId,
2017-10-07 12:09:10 +02:00
limit: 5
}, this.user);
2018-04-05 18:36:34 +02:00
const text = `${getUserName(tl[0].user)}さんのタイムラインはこちらです:\n\n` + tl
2018-04-07 19:30:37 +02:00
.map(note => getNoteSummary(note))
2017-10-07 12:09:10 +02:00
.join('\n-----\n');
this.reply([{
type: 'text',
text: text
}]);
}
2017-10-07 11:30:04 +02:00
}
2018-04-12 23:06:18 +02:00
const handler = new EventEmitter();
2017-10-06 20:36:46 +02:00
2018-04-12 23:06:18 +02:00
handler.on('event', async (ev) => {
2017-10-06 20:36:46 +02:00
2018-04-12 23:06:18 +02:00
const sourceId = ev.source.userId;
const sessionId = `line-bot-sessions:${sourceId}`;
2017-10-06 21:30:57 +02:00
2018-04-12 23:06:18 +02:00
const session = await redis.get(sessionId);
let bot: LineBot;
2017-10-06 21:30:57 +02:00
2018-04-12 23:06:18 +02:00
if (session == null) {
const user = await User.findOne({
host: null,
'line': {
userId: sourceId
}
});
2017-10-06 21:48:56 +02:00
2018-04-12 23:06:18 +02:00
bot = new LineBot(user);
2017-10-06 23:43:36 +02:00
2018-04-12 23:06:18 +02:00
bot.on('signin', user => {
User.update(user._id, {
$set: {
'line': {
userId: sourceId
2017-10-06 23:03:16 +02:00
}
2018-04-12 23:06:18 +02:00
}
2017-10-06 23:03:16 +02:00
});
2018-04-12 23:06:18 +02:00
});
2017-10-06 21:48:56 +02:00
2018-04-12 23:06:18 +02:00
bot.on('signout', user => {
User.update(user._id, {
$set: {
'line': {
userId: null
2017-10-06 23:43:36 +02:00
}
2018-04-12 23:06:18 +02:00
}
2017-10-06 23:43:36 +02:00
});
2017-10-06 22:50:01 +02:00
});
2018-04-12 23:06:18 +02:00
redis.set(sessionId, JSON.stringify(bot.export()));
} else {
bot = LineBot.import(JSON.parse(session));
}
2017-10-07 20:24:10 +02:00
2018-04-12 23:06:18 +02:00
bot.on('updated', () => {
redis.set(sessionId, JSON.stringify(bot.export()));
2017-10-06 21:30:57 +02:00
});
2018-04-12 23:06:18 +02:00
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'];
2017-10-06 20:36:46 +02:00
2017-10-06 21:30:57 +02:00
const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret)
2018-04-12 23:06:18 +02:00
.update(ctx.request.rawBody);
2017-10-06 20:36:46 +02:00
const sig2 = hash.digest('base64');
// シグネチャ比較
if (sig1 === sig2) {
2018-04-13 02:44:00 +02:00
ctx.request.body.events.forEach(ev => {
2017-10-07 12:29:32 +02:00
handler.emit('event', ev);
2017-10-06 21:30:57 +02:00
});
2017-10-06 20:36:46 +02:00
} else {
2018-04-12 23:06:18 +02:00
ctx.status = 400;
2017-10-06 20:36:46 +02:00
}
});
2018-04-12 23:06:18 +02:00
}
module.exports = router;