summaryrefslogtreecommitdiff
path: root/src/server/api/bot
diff options
context:
space:
mode:
authortamaina <tamaina@hotmail.co.jp>2018-04-11 20:59:30 +0900
committerGitHub <noreply@github.com>2018-04-11 20:59:30 +0900
commit185805363d244c43e9e91c67fd94dbb7473c283a (patch)
tree838914e262c0fca5737588a7bba64e2b9f3d8e5f /src/server/api/bot
parentv2380 (diff)
parentMerge pull request #1 from syuilo/master (diff)
downloadmisskey-185805363d244c43e9e91c67fd94dbb7473c283a.tar.gz
misskey-185805363d244c43e9e91c67fd94dbb7473c283a.tar.bz2
misskey-185805363d244c43e9e91c67fd94dbb7473c283a.zip
Merge pull request #2 from tamaina/master
追従
Diffstat (limited to 'src/server/api/bot')
-rw-r--r--src/server/api/bot/core.ts439
-rw-r--r--src/server/api/bot/interfaces/line.ts239
2 files changed, 678 insertions, 0 deletions
diff --git a/src/server/api/bot/core.ts b/src/server/api/bot/core.ts
new file mode 100644
index 0000000000..d41af48057
--- /dev/null
+++ b/src/server/api/bot/core.ts
@@ -0,0 +1,439 @@
+import * as EventEmitter from 'events';
+import * as bcrypt from 'bcryptjs';
+
+import User, { IUser, init as initUser, ILocalUser } from '../../../models/user';
+
+import getNoteSummary from '../../../renderers/get-note-summary';
+import getUserName from '../../../renderers/get-user-name';
+import getUserSummary from '../../../renderers/get-user-summary';
+import parseAcct from '../../../acct/parse';
+import getNotificationSummary from '../../../renderers/get-notification-summary';
+
+const hmm = [
+ '?',
+ 'ふぅ~む...?',
+ 'ちょっと何言ってるかわからないです',
+ '「ヘルプ」と言うと利用可能な操作が確認できますよ'
+];
+
+/**
+ * Botの頭脳
+ */
+export default class BotCore extends EventEmitter {
+ public user: IUser = null;
+
+ private context: Context = null;
+
+ constructor(user?: IUser) {
+ super();
+
+ this.user = user;
+ }
+
+ public clearContext() {
+ this.setContext(null);
+ }
+
+ public setContext(context: Context) {
+ this.context = context;
+ this.emit('updated');
+
+ if (context) {
+ context.on('updated', () => {
+ this.emit('updated');
+ });
+ }
+ }
+
+ public export() {
+ return {
+ user: this.user,
+ context: this.context ? this.context.export() : null
+ };
+ }
+
+ protected _import(data) {
+ this.user = data.user ? initUser(data.user) : null;
+ this.setContext(data.context ? Context.import(this, data.context) : null);
+ }
+
+ public static import(data) {
+ const bot = new BotCore();
+ bot._import(data);
+ return bot;
+ }
+
+ public async q(query: string): Promise<string> {
+ if (this.context != null) {
+ return await this.context.q(query);
+ }
+
+ if (/^@[a-zA-Z0-9_]+$/.test(query)) {
+ return await this.showUserCommand(query);
+ }
+
+ switch (query) {
+ case 'ping':
+ return 'PONG';
+
+ case 'help':
+ case 'ヘルプ':
+ return '利用可能なコマンド一覧です:\n' +
+ 'help: これです\n' +
+ 'me: アカウント情報を見ます\n' +
+ 'login, signin: サインインします\n' +
+ 'logout, signout: サインアウトします\n' +
+ 'note: 投稿します\n' +
+ 'tl: タイムラインを見ます\n' +
+ 'no: 通知を見ます\n' +
+ '@<ユーザー名>: ユーザーを表示します\n' +
+ '\n' +
+ 'タイムラインや通知を見た後、「次」というとさらに遡ることができます。';
+
+ case 'me':
+ return this.user ? `${getUserName(this.user)}としてサインインしています。\n\n${getUserSummary(this.user)}` : 'サインインしていません';
+
+ case 'login':
+ case 'signin':
+ case 'ログイン':
+ case 'サインイン':
+ if (this.user != null) return '既にサインインしていますよ!';
+ this.setContext(new SigninContext(this));
+ return await this.context.greet();
+
+ case 'logout':
+ case 'signout':
+ case 'ログアウト':
+ case 'サインアウト':
+ if (this.user == null) return '今はサインインしてないですよ!';
+ this.signout();
+ return 'ご利用ありがとうございました <3';
+
+ case 'note':
+ case '投稿':
+ if (this.user == null) return 'まずサインインしてください。';
+ this.setContext(new NoteContext(this));
+ return await this.context.greet();
+
+ case 'tl':
+ case 'タイムライン':
+ if (this.user == null) return 'まずサインインしてください。';
+ this.setContext(new TlContext(this));
+ return await this.context.greet();
+
+ case 'no':
+ case 'notifications':
+ case '通知':
+ if (this.user == null) return 'まずサインインしてください。';
+ this.setContext(new NotificationsContext(this));
+ return await this.context.greet();
+
+ case 'guessing-game':
+ case '数当てゲーム':
+ this.setContext(new GuessingGameContext(this));
+ return await this.context.greet();
+
+ default:
+ return hmm[Math.floor(Math.random() * hmm.length)];
+ }
+ }
+
+ public signin(user: IUser) {
+ this.user = user;
+ this.emit('signin', user);
+ this.emit('updated');
+ }
+
+ public signout() {
+ const user = this.user;
+ this.user = null;
+ this.emit('signout', user);
+ this.emit('updated');
+ }
+
+ public async refreshUser() {
+ this.user = await User.findOne({
+ _id: this.user._id
+ }, {
+ fields: {
+ data: false
+ }
+ });
+
+ this.emit('updated');
+ }
+
+ public async showUserCommand(q: string): Promise<string> {
+ try {
+ const user = await require('../endpoints/users/show')(parseAcct(q.substr(1)), this.user);
+
+ const text = getUserSummary(user);
+
+ return text;
+ } catch (e) {
+ return `問題が発生したようです...: ${e}`;
+ }
+ }
+}
+
+abstract class Context extends EventEmitter {
+ protected bot: BotCore;
+
+ public abstract async greet(): Promise<string>;
+ public abstract async q(query: string): Promise<string>;
+ public abstract export(): any;
+
+ constructor(bot: BotCore) {
+ super();
+ this.bot = bot;
+ }
+
+ public static import(bot: BotCore, data: any) {
+ if (data.type == 'guessing-game') return GuessingGameContext.import(bot, data.content);
+ if (data.type == 'note') return NoteContext.import(bot, data.content);
+ if (data.type == 'tl') return TlContext.import(bot, data.content);
+ if (data.type == 'notifications') return NotificationsContext.import(bot, data.content);
+ if (data.type == 'signin') return SigninContext.import(bot, data.content);
+ return null;
+ }
+}
+
+class SigninContext extends Context {
+ private temporaryUser: ILocalUser = null;
+
+ public async greet(): Promise<string> {
+ return 'まずユーザー名を教えてください:';
+ }
+
+ public async q(query: string): Promise<string> {
+ if (this.temporaryUser == null) {
+ // Fetch user
+ const user = await User.findOne({
+ usernameLower: query.toLowerCase(),
+ host: null
+ }, {
+ fields: {
+ data: false
+ }
+ }) as ILocalUser;
+
+ if (user === null) {
+ return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
+ } else {
+ this.temporaryUser = user;
+ this.emit('updated');
+ return `パスワードを教えてください:`;
+ }
+ } else {
+ // Compare password
+ const same = await bcrypt.compare(query, this.temporaryUser.password);
+
+ if (same) {
+ this.bot.signin(this.temporaryUser);
+ this.bot.clearContext();
+ return `${getUserName(this.temporaryUser)}さん、おかえりなさい!`;
+ } else {
+ return `パスワードが違います... もう一度教えてください:`;
+ }
+ }
+ }
+
+ public export() {
+ return {
+ type: 'signin',
+ content: {
+ temporaryUser: this.temporaryUser
+ }
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new SigninContext(bot);
+ context.temporaryUser = data.temporaryUser;
+ return context;
+ }
+}
+
+class NoteContext extends Context {
+ public async greet(): Promise<string> {
+ return '内容:';
+ }
+
+ public async q(query: string): Promise<string> {
+ await require('../endpoints/notes/create')({
+ text: query
+ }, this.bot.user);
+ this.bot.clearContext();
+ return '投稿しましたよ!';
+ }
+
+ public export() {
+ return {
+ type: 'note'
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new NoteContext(bot);
+ return context;
+ }
+}
+
+class TlContext extends Context {
+ private next: string = null;
+
+ public async greet(): Promise<string> {
+ return await this.getTl();
+ }
+
+ public async q(query: string): Promise<string> {
+ if (query == '次') {
+ return await this.getTl();
+ } else {
+ this.bot.clearContext();
+ return await this.bot.q(query);
+ }
+ }
+
+ private async getTl() {
+ const tl = await require('../endpoints/notes/timeline')({
+ limit: 5,
+ untilId: this.next ? this.next : undefined
+ }, this.bot.user);
+
+ if (tl.length > 0) {
+ this.next = tl[tl.length - 1].id;
+ this.emit('updated');
+
+ const text = tl
+ .map(note => `${getUserName(note.user)}\n「${getNoteSummary(note)}」`)
+ .join('\n-----\n');
+
+ return text;
+ } else {
+ return 'タイムラインに表示するものがありません...';
+ }
+ }
+
+ public export() {
+ return {
+ type: 'tl',
+ content: {
+ next: this.next,
+ }
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new TlContext(bot);
+ context.next = data.next;
+ return context;
+ }
+}
+
+class NotificationsContext extends Context {
+ private next: string = null;
+
+ public async greet(): Promise<string> {
+ return await this.getNotifications();
+ }
+
+ public async q(query: string): Promise<string> {
+ if (query == '次') {
+ return await this.getNotifications();
+ } else {
+ this.bot.clearContext();
+ return await this.bot.q(query);
+ }
+ }
+
+ private async getNotifications() {
+ const notifications = await require('../endpoints/i/notifications')({
+ limit: 5,
+ untilId: this.next ? this.next : undefined
+ }, this.bot.user);
+
+ if (notifications.length > 0) {
+ this.next = notifications[notifications.length - 1].id;
+ this.emit('updated');
+
+ const text = notifications
+ .map(notification => getNotificationSummary(notification))
+ .join('\n-----\n');
+
+ return text;
+ } else {
+ return '通知はありません';
+ }
+ }
+
+ public export() {
+ return {
+ type: 'notifications',
+ content: {
+ next: this.next,
+ }
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new NotificationsContext(bot);
+ context.next = data.next;
+ return context;
+ }
+}
+
+class GuessingGameContext extends Context {
+ private secret: number;
+ private history: number[] = [];
+
+ public async greet(): Promise<string> {
+ this.secret = Math.floor(Math.random() * 100);
+ this.emit('updated');
+ return '0~100の秘密の数を当ててみてください:';
+ }
+
+ public async q(query: string): Promise<string> {
+ if (query == 'やめる') {
+ this.bot.clearContext();
+ return 'やめました。';
+ }
+
+ const guess = parseInt(query, 10);
+
+ if (isNaN(guess)) {
+ return '整数で推測してください。「やめる」と言うとゲームをやめます。';
+ }
+
+ const firsttime = this.history.indexOf(guess) === -1;
+
+ this.history.push(guess);
+ this.emit('updated');
+
+ if (this.secret < guess) {
+ return firsttime ? `${guess}よりも小さいですね` : `もう一度言いますが${guess}より小さいですよ`;
+ } else if (this.secret > guess) {
+ return firsttime ? `${guess}よりも大きいですね` : `もう一度言いますが${guess}より大きいですよ`;
+ } else {
+ this.bot.clearContext();
+ return `正解です🎉 (${this.history.length}回目で当てました)`;
+ }
+ }
+
+ public export() {
+ return {
+ type: 'guessing-game',
+ content: {
+ secret: this.secret,
+ history: this.history
+ }
+ };
+ }
+
+ public static import(bot: BotCore, data: any) {
+ const context = new GuessingGameContext(bot);
+ context.secret = data.secret;
+ context.history = data.history;
+ return context;
+ }
+}
diff --git a/src/server/api/bot/interfaces/line.ts b/src/server/api/bot/interfaces/line.ts
new file mode 100644
index 0000000000..be3bfe33d3
--- /dev/null
+++ b/src/server/api/bot/interfaces/line.ts
@@ -0,0 +1,239 @@
+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 '../../../../config';
+import BotCore from '../core';
+import _redis from '../../../../db/redis';
+import prominence = require('prominence');
+import getAcct from '../../../../acct/render';
+import parseAcct from '../../../../acct/parse';
+import getNoteSummary from '../../../../renderers/get-note-summary';
+import getUserName from '../../../../renderers/get-user-name';
+
+const redis = prominence(_redis);
+
+// 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'
+];
+
+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> {
+ this.replyToken = ev.replyToken;
+
+ 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;
+
+ // noteback
+ case 'noteback':
+ const data = ev.noteback.data;
+ const cmd = data.split('|')[0];
+ const arg = data.split('|')[1];
+ switch (cmd) {
+ case 'showtl':
+ this.showUserTimelineNoteback(arg);
+ break;
+ }
+ break;
+ }
+ }
+
+ public static import(data) {
+ const bot = new LineBot();
+ bot._import(data);
+ return bot;
+ }
+
+ public async showUserCommand(q: string) {
+ const user = await require('../../endpoints/users/show')(parseAcct(q.substr(1)), this.user);
+
+ const acct = getAcct(user);
+ const actions = [];
+
+ actions.push({
+ type: 'noteback',
+ label: 'タイムラインを見る',
+ data: `showtl|${user.id}`
+ });
+
+ if (user.twitter) {
+ actions.push({
+ type: 'uri',
+ label: 'Twitterアカウントを見る',
+ uri: `https://twitter.com/${user.twitter.screenName}`
+ });
+ }
+
+ actions.push({
+ type: 'uri',
+ label: 'Webで見る',
+ uri: `${config.url}/@${acct}`
+ });
+
+ this.reply([{
+ type: 'template',
+ altText: await super.showUserCommand(q),
+ template: {
+ type: 'buttons',
+ thumbnailImageUrl: `${user.avatarUrl}?thumbnail&size=1024`,
+ title: `${getUserName(user)} (@${acct})`,
+ text: user.description || '(no description)',
+ actions: actions
+ }
+ }]);
+
+ return null;
+ }
+
+ public async showUserTimelineNoteback(userId: string) {
+ const tl = await require('../../endpoints/users/notes')({
+ userId: userId,
+ limit: 5
+ }, this.user);
+
+ const text = `${getUserName(tl[0].user)}さんのタイムラインはこちらです:\n\n` + tl
+ .map(note => getNoteSummary(note))
+ .join('\n-----\n');
+
+ this.reply([{
+ type: 'text',
+ text: text
+ }]);
+ }
+}
+
+module.exports = async (app: express.Application) => {
+ if (config.line_bot == null) return;
+
+ const handler = new EventEmitter();
+
+ handler.on('event', async (ev) => {
+
+ const sourceId = ev.source.userId;
+ const sessionId = `line-bot-sessions:${sourceId}`;
+
+ const session = await redis.get(sessionId);
+ let bot: LineBot;
+
+ if (session == null) {
+ const user = await User.findOne({
+ host: null,
+ 'line': {
+ userId: sourceId
+ }
+ });
+
+ bot = new LineBot(user);
+
+ bot.on('signin', user => {
+ User.update(user._id, {
+ $set: {
+ 'line': {
+ userId: sourceId
+ }
+ }
+ });
+ });
+
+ 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();
+
+ bot.react(ev);
+ });
+
+ 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;
+
+ const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret)
+ .update((req as any).rawBody);
+
+ const sig2 = hash.digest('base64');
+
+ // シグネチャ比較
+ if (sig1 === sig2) {
+ req.body.events.forEach(ev => {
+ handler.emit('event', ev);
+ });
+
+ res.sendStatus(200);
+ } else {
+ res.sendStatus(400);
+ }
+ });
+};