-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
334 lines (319 loc) · 12.4 KB
/
app.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const line = require('@line/bot-sdk');
const express = require('express');
const config = require('./config.json');
const contents = require('./contents.json');
// create LINE SDK client
const client = new line.Client(config);
const app = express();
app.get('/webhook', (req, res) => res.end(`I'm listening. Please access with POST.`));
// webhook callback
app.post('/webhook', line.middleware(config), (req, res) => {
// req.body.events should be an array of events
if (!Array.isArray(req.body.events)) {
return res.status(500).end();
}
// handle events separately
Promise.all(req.body.events.map(handleEvent))
.then(() => res.end())
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
// simple reply function
const replyText = (token, texts) => {
texts = Array.isArray(texts) ? texts : [texts];
return client.replyMessage(
token,
texts.map((text) => ({ type: 'text', text }))
);
};
// callback function to handle a single event
function handleEvent(event) {
if (event.replyToken && event.replyToken.match(/^(.)\1*$/)) {
return console.log("Hook recieved: " + JSON.stringify(event.message));
}
switch (event.type) {
case 'message':
const message = event.message;
switch (message.type) {
case 'text':
return handleText(message, event.replyToken);
case 'sticker':
return handleSticker(message, event.replyToken);
default:
throw new Error(`Unknown message: ${JSON.stringify(message)}`);
}
case 'follow': // greeting event
return client.replyMessage(
event.replyToken, contents["welcome"]
);
case 'unfollow':
return console.log(`Unfollowed this bot: ${JSON.stringify(event)}`);
default:
throw new Error(`Unknown event: ${JSON.stringify(event)}`);
}
}
function handleText(message, replyToken) {
switch (message.text) {
case 'เมนู':
return client.replyMessage(
replyToken, contents["menu"]
);
case 'นับลูกดิ้น':
return client.replyMessage(
replyToken, contents["menu-count"]
);
// let t = getHours()
// // check time must not exceed 4-8 o'clock
// if (inRange(t, 4, 8)) {
// return client.replyMessage(
// replyToken, contents["menu-count"]
// );
// } else {
// return client.replyMessage(
// replyToken, contents["menu-count-ctt"]
// );
// }
case 'นับลูกดิ้นแบบ count to ten':
return client.replyMessage(
replyToken, contents["menu-count-ctt-start"]
);
case 'นับลูกดิ้นแบบ sadovsky':
// return client.replyMessage(
// replyToken, contents["menu-count-sadovsky-start"]
// );
let h = getHours()
// check time must not exceed 4-8 o'clock
if (inRange(h, 4, 10)) {
return client.replyMessage(
replyToken, contents["menu-count-sadovsky-start"]
);
} else {
return replyText(replyToken, ["ในเวลานี้ไม่สามารถทำรายการเริ่มนับแบบ sadovsky ได้ค่ะ😅 คุณแม่จะต้องขอทำรายการก่อน 10.00 น.", "หากคุณแม่ทำรายการเริ่มนับแล้ว ⏳กรุณารอการแจ้งเตือนแต่ละมื้อนะคะ\n🌞มื้อเที่ยง 11.30 น.\n🌙มื้อเย็น 17.00 น.", "กรณีทำรายการขอนับในช่วงเช้าไม่ทันหรือมีข้อผิดพลาดเกิดขึ้นระหว่างการนับ 💡เราขอแนะนำให้คุณแม่นับลูกดิ้นแบบ count to ten แทนค่ะ👍🏻"])
}
case 'คู่มือคุณแม่':
return client.replyMessage(
replyToken, contents["menu-manual"]
);
case 'พัฒนาการลูกน้อย': // manual 1
return client.replyMessage(
replyToken, contents["menu-manual-1"]
)
case 'พัฒนาการลูกน้อยในไตรมาสที่ 1':
return client.replyMessage(
replyToken, contents["menu-manual-1-1"]
)
case 'พัฒนาการลูกน้อยในไตรมาสที่ 2':
return client.replyMessage(
replyToken, contents["menu-manual-1-2"]
)
case 'พัฒนาการลูกน้อยในไตรมาสที่ 3':
return client.replyMessage(
replyToken, contents["menu-manual-1-3"]
)
case 'การเปลี่ยนแปลงด้านร่างกาย': // manual 2
return client.replyMessage(
replyToken, contents["menu-manual-2"]
)
case 'การเปลี่ยนแปลงด้านร่างกายในไตรมาสที่ 1':
return client.replyMessage(
replyToken, contents["menu-manual-2-1"]
)
case 'การเปลี่ยนแปลงด้านร่างกายในไตรมาสที่ 2':
return client.replyMessage(
replyToken, contents["menu-manual-2-2"]
)
case 'การเปลี่ยนแปลงด้านร่างกายในไตรมาสที่ 3':
return client.replyMessage(
replyToken, contents["menu-manual-2-3"]
)
case 'โภชนาการสำหรับคุณแม่': // manual 3
return client.replyMessage(
replyToken, contents["menu-manual-3"]
)
case 'การออกกำลังกายสำหรับคุณแม่': // manual 4
return client.replyMessage(
replyToken, contents["menu-manual-4"]
)
case 'ท่านั่งจับปลายเท้า':
return client.replyMessage(
replyToken, contents["menu-manual-4-1"]
)
case 'ท่านั่งยกมือดันอากาศ':
return client.replyMessage(
replyToken, contents["menu-manual-4-2"]
)
case 'ท่าโก่งหลัง':
return client.replyMessage(
replyToken, contents["menu-manual-4-3"]
)
case 'การนอนหลับพักผ่อนในหญิงตั้งครรภ์': // manual 5
return client.replyMessage(
replyToken, contents["menu-manual-5"]
)
case 'การมีเพศสัมพันธ์ในระยะตั้งครรภ์':
return client.replyMessage(
replyToken, contents["menu-manual-6"]
)
case 'อาการไม่สุขสบาย': // manual 7
return client.replyMessage(
replyToken, contents["menu-manual-7"]
)
case 'ระบบทางเดินอาหาร': // manual 7-1
return client.replyMessage(
replyToken, contents["menu-manual-7-1"]
)
case 'คลื่นไส้ อาเจียน':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-1"]
)
case 'มีน้ำลายมาก':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-2"]
)
case 'เหงือกอักเสบ':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-3"]
)
case 'ร้อนในอก':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-4"]
)
case 'ท้องผูก':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-5"]
)
case 'ริดสีดวงทวาร':
return client.replyMessage(
replyToken, contents["menu-manual-7-1-6"]
)
case 'ระบบหัวใจและหลอดเลือด': // manual 7-2
return client.replyMessage(
replyToken, contents["menu-manual-7-2"]
)
case 'ใจสั่น เป็นลม':
return client.replyMessage(
replyToken, contents["menu-manual-7-2-1"]
)
case 'เส้นเลือดขอด':
return client.replyMessage(
replyToken, contents["menu-manual-7-2-2"]
)
case 'ระบบหายใจ': // manual 7-3
return client.replyMessage(
replyToken, contents["menu-manual-7-3"]
)
case 'หายใจลำบาก':
return client.replyMessage(
replyToken, contents["menu-manual-7-3-1"]
)
case 'ระบบกระดูกและกล้ามเนื้อ': // manual 7-4
return client.replyMessage(
replyToken, contents["menu-manual-7-4"]
)
case 'ตะคริว':
return client.replyMessage(
replyToken, contents["menu-manual-7-4-1"]
)
case 'ปวดหลัง':
return client.replyMessage(
replyToken, contents["menu-manual-7-4-2"]
)
case 'ระบบขับถ่ายปัสสาวะ': // manual 7-5
return client.replyMessage(
replyToken, contents["menu-manual-7-5"]
)
case 'ปัสสาวะบ่อย':
return client.replyMessage(
replyToken, contents["menu-manual-7-5-1"]
)
case 'ระบบผิวหนัง': // manual 7-6
return client.replyMessage(
replyToken, contents["menu-manual-7-6"]
)
case 'ผิวหนังเปลี่ยนแปลง':
return client.replyMessage(
replyToken, contents["menu-manual-7-6-1"]
)
case 'ระบบประสาท': // manual 7-7
return client.replyMessage(
replyToken, contents["menu-manual-7-7"]
)
case 'ปวดศีรษะ':
return client.replyMessage(
replyToken, contents["menu-manual-7-7-1"]
)
case 'อารมณ์แปรปรวน':
return client.replyMessage(
replyToken, contents["menu-manual-7-7-2"]
)
case 'ปวด ชาฝ่ามือและนิ้วมือ':
return client.replyMessage(
replyToken, contents["menu-manual-7-7-3"]
)
case 'อาการอื่นๆ': // manual 7-8
return client.replyMessage(
replyToken, contents["menu-manual-7-8"]
)
case 'เท้าและข้อบวม':
return client.replyMessage(
replyToken, contents["menu-manual-7-8-1"]
)
case 'นอนไม่หลับ':
return client.replyMessage(
replyToken, contents["menu-manual-7-8-2"]
)
case 'อ่อนเพลีย':
return client.replyMessage(
replyToken, contents["menu-manual-7-8-3"]
)
case 'สัญญาณอันตรายที่อาจเกิดขึ้นได้ในระยะตั้งครรภ์': // manual 8
return client.replyMessage(
replyToken, contents["menu-manual-8"]
)
case 'สัญญาณอันตรายในไตรมาสที่ 1':
return client.replyMessage(
replyToken, contents["menu-manual-8-1"]
)
case 'สัญญาณอันตรายในไตรมาสที่ 2':
return client.replyMessage(
replyToken, contents["menu-manual-8-2"]
)
case 'สัญญาณอันตรายในไตรมาสที่ 3':
return client.replyMessage(
replyToken, contents["menu-manual-8-3"]
)
case 'การเตรียมตัวคลอด': // manual 9
return replyText(replyToken, contents["menu-manual-9"]["msg"]);
case 'time':
let x = new Date().toLocaleString("en-US", { timeZone: "Asia/Bangkok" });
x = new Date(x).toString();
return replyText(replyToken, x);
case '1669':
return client.replyMessage(
replyToken, contents["1669"]
)
default:
console.log(`Echo message to ${replyToken}: ${message.text}`);
}
}
function handleSticker(message, replyToken) {
return client.replyMessage(
replyToken, contents["default-sticker"]
);
}
function getHours() {
let time = new Date().toLocaleString("en-US", { timeZone: "Asia/Bangkok" });
time = new Date(time).getHours();
return time
}
function inRange(value, min, max) {
return ((value - min) * (value - max) <= 0);
}
// listen on port
const port = process.env.PORT || config.port;
app.listen(port, () => {
console.log(`listening on ${port}`);
});