summaryrefslogtreecommitdiff
path: root/src/server/api/bot/interfaces/line.ts
blob: 733315391d561801f6876a86931eb921ca05079f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as EventEmitter from 'events';
import * as Router from 'koa-router';
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
		}]);
	}
}

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);
});

// Init router
const router = new Router();

if (config.line_bot) {
	router.post('/hooks/line', ctx => {
		const sig1 = ctx.headers['x-line-signature'];

		const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret)
			.update(ctx.request.rawBody);

		const sig2 = hash.digest('base64');

		// シグネチャ比較
		if (sig1 === sig2) {
			ctx.request.body.events.forEach(ev => {
				handler.emit('event', ev);
			});
		} else {
			ctx.status = 400;
		}
	});
}

module.exports = router;