summaryrefslogtreecommitdiff
path: root/src/api/bot
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-10-07 19:09:10 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-10-07 19:09:10 +0900
commit041499814a955b514970bbd3cb2d89480e6802a3 (patch)
treecc29a54176e424f696c2949bfde12c5afa9f8303 /src/api/bot
parent:v: (diff)
downloadsharkey-041499814a955b514970bbd3cb2d89480e6802a3.tar.gz
sharkey-041499814a955b514970bbd3cb2d89480e6802a3.tar.bz2
sharkey-041499814a955b514970bbd3cb2d89480e6802a3.zip
:v:
Diffstat (limited to 'src/api/bot')
-rw-r--r--src/api/bot/interfaces/line.ts74
1 files changed, 64 insertions, 10 deletions
diff --git a/src/api/bot/interfaces/line.ts b/src/api/bot/interfaces/line.ts
index c5e8293b92..cc634ca89c 100644
--- a/src/api/bot/interfaces/line.ts
+++ b/src/api/bot/interfaces/line.ts
@@ -7,9 +7,25 @@ import config from '../../../conf';
import BotCore from '../core';
import _redis from '../../../db/redis';
import prominence = require('prominence');
+import getPostSummary from '../../../common/get-post-summary';
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;
@@ -34,18 +50,36 @@ class LineBot extends BotCore {
public async react(ev: any): Promise<void> {
this.replyToken = ev.replyToken;
- // テキスト以外(スタンプなど)は無視
- if (ev.message.type !== 'text') return;
-
- const res = await this.q(ev.message.text);
+ // テキスト
+ if (ev.message.type == 'text') {
+ const res = await this.q(ev.message.text);
- if (res == null) return;
+ if (res == null) return;
- // 返信
- this.reply([{
- type: 'text',
- text: res
- }]);
+ // 返信
+ this.reply([{
+ type: 'text',
+ text: res
+ }]);
+ // スタンプ
+ } else if (ev.message.type == 'sticker') {
+ // スタンプで返信
+ this.reply([{
+ type: 'sticker',
+ packageId: '4',
+ stickerId: stickers[Math.floor(Math.random() * stickers.length)]
+ }]);
+ // postback
+ } else if (ev.message.type == 'postback') {
+ const data = ev.message.postback.data;
+ const cmd = data.split('|')[0];
+ const arg = data.split('|')[1];
+ switch (cmd) {
+ case 'showtl':
+ this.showUserTimelinePostback(arg);
+ break;
+ }
+ }
}
public static import(data) {
@@ -68,6 +102,10 @@ class LineBot extends BotCore {
title: `${user.name} (@${user.username})`,
text: user.description || '(no description)',
actions: [{
+ type: 'postback',
+ label: 'タイムラインを見る',
+ data: `showtl|${user._id}`
+ }, {
type: 'uri',
label: 'Webで見る',
uri: `${config.url}/${user.username}`
@@ -75,6 +113,22 @@ class LineBot extends BotCore {
}
}]);
}
+
+ public async showUserTimelinePostback(userId: string) {
+ const tl = await require('../../endpoints/users/posts')({
+ user_id: userId,
+ limit: 5
+ }, this.user);
+
+ const text = tl
+ .map(post => getPostSummary(post))
+ .join('\n-----\n');
+
+ this.reply([{
+ type: 'text',
+ text: text
+ }]);
+ }
}
module.exports = async (app: express.Application) => {