-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.ts
171 lines (159 loc) · 4.82 KB
/
app.ts
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
// service
import logService from './logService';
import lineService from './lineService';
import sheetService from './sheetService';
// config
import CONFIG from './config';
import WORDING from './wording';
// interface
interface ReceiveMessage {
replyToken: string;
userId: string;
userMessage: string;
};
interface ReplyMessage {
type: string;
to: string;
messages: Array<Message>;
};
interface Message {
type: string;
text: string;
template?: object;
};
const addTextMessage = (msg, content) => {
if (content) {
msg.push({
'type': 'text',
'text': content
});
}
}
const getConfig = {
singleReply: (id: string, msgs: Array<string> = []):ReplyMessage => {
const messages = msgs.map((msg: string) => {
const resultMsg: Message = {
type: 'text',
text: msg
};
return resultMsg;
})
return {
type: 'push',
to: id,
messages: messages
};
},
normalReply: function(to, userMessage, link, detail) {
const messages = [];
addTextMessage(messages, userMessage);
addTextMessage(messages, detail);
addTextMessage(messages, link);
return {
type: 'push',
to,
messages
};
},
// return button message to let user feedback
buttonReply: function(target, nameList, userId, userMessage) {
const replyMessages = [];
const recommandation = target.split(',');
// create replyMessages
for (let i = 0; i < recommandation.length; i++) {
const index = parseInt(recommandation[i]);
replyMessages.push({
"type": "message",
"label": nameList[index],
"text": nameList[index]
});
}
const displayText = WORDING.recommandation_head + userMessage + WORDING.recommandation_tail;
return {
type: 'push',
to: userId,
messages: [{
"type": "template",
"altText": displayText,
"template": {
"type": "buttons",
"title": displayText,
"text": WORDING.see_more,
"actions": replyMessages
}
}]
};
}
};
const parseLineMessage = (e: any): ReceiveMessage => {
if (e && e.postData && e.postData.contents) {
// convet message to JSON format
const msg = JSON.parse(e.postData.contents);
const event = msg.events[0];
if (event) {
const {
replyToken,
message: { text: userMessage },
source: { userId }
} = event;
const receiveMessage: ReceiveMessage = {
replyToken,
userId,
userMessage
};
return receiveMessage;
}
}
return null;
};
// default apps script post method
export default function doPost(e) {
logService.log('[doPost]');
const { replyToken, userMessage, userId } = parseLineMessage(e);
// save user action
sheetService.save({
search: userMessage,
user: userId
});
// SELECT link, detail FROM DRINK_LIST WHERE name = name OR nameen = name
const searchResult = sheetService.query({
select: ['link', 'detail'],
from: 'DRINK_LIST',
where: {
name: userMessage,
nameen: userMessage
}
});
let config = {};
const { link, detail } = searchResult;
if (link == null) {
// if can't find cocktail, try to recommands
// SELECT recommandation FROM ELEMENT_MAPPING WHERE name = name OR nameen = name
logService.log('[doPost] find recommands');
const recommands = sheetService.query({
select: ['recommandation'],
from: 'ELEMENT_MAPPING',
where: {
name: userMessage,
nameen: userMessage
}
});
// if there are nothing to recommand, return default not found wording
if (recommands.recommandation == null) {
config = getConfig.normalReply(userId, userMessage, CONFIG.OVERPARTYLAB.IG, WORDING.not_found);
} else {
// retrun to ask type
const nameList = sheetService.query({
select: ['name'],
from: 'DRINK_LIST',
where: {}
});
config = getConfig.buttonReply(recommands.recommandation, nameList.name, userId, userMessage);
}
} else {
// create normal reply
config = getConfig.normalReply(userId, userMessage, link, detail);
}
logService.log([config]);
lineService.pushMsg(config);
}