-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
373 lines (348 loc) · 9.07 KB
/
utils.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const get = require("lodash/get");
const map = require("lodash/map");
const uniq = require("lodash/uniqBy");
const R = require("ramda");
const {
PAYLOADS: P,
RANDOM_REPLIES,
PROMO_IMG_URLS,
MIN_TIME_INTERVAL,
} = require("./constants");
const { path } = R;
/**
* get location {lat, long} from Context object
* @param {Context} context
* @return {Object} coordinates
* @return {Number} coordinates.lat
* @return {Number} coordinates.long
*/
function getLocation(context) {
const attachments = get(context, "event.attachments");
if (attachments) {
for (let i = 0; i < attachments.length; i = i + 1) {
const type = get(attachments, `[${i}].type`);
if (type === "location") {
const coordinates = get(attachments, `[${i}].payload.coordinates`);
if (coordinates) {
return coordinates;
}
}
}
}
return null;
}
/**
* get timestamp from Context object
* @param {Context} context
* @return {Number} timestamp - miniseconds
*/
function getTimeStamp(context) {
return get(context, "event._rawEvent.timestamp");
}
/**
* Get url of first image in attachments from Context object
* @param {Context} context
* @return {String} url
*/
function getImageUrl(context) {
const attachments = get(context, "event.attachments");
if (attachments) {
for (let i = 0; i < attachments.length; i = i + 1) {
const type = get(attachments, `[${i}].type`);
if (type === "image") {
const url = get(attachments, `[${i}].payload.url`);
if (url) {
return url;
}
}
}
}
return null;
}
/**
* Convert miniseconds to { hrs, mins, secs } format, hrs can be more than 24.
* e.g. 3661000 ms -> { hrs: 1, mins: 1, secs: 1 }
* @param {Number} ms
* @return {Object} time
* @return {Number} time.hrs
* @return {Number} time.mins
* @return {Number} time secs
*/
function calcTime(ms) {
let secs = Math.round(ms / 1000);
let mins = Math.floor(secs / 60);
secs = secs % 60;
let hrs = Math.floor(mins / 60);
mins = mins % 60;
return { hrs, mins, secs };
}
/**
* Calculate how many unique days within an object list
* @param {Array} checkIns
* @return {Number} nUniqDays
*/
function calcCheckInDayCount(checkIns) {
const timestamps = map(checkIns, "startTime");
const days = map(timestamps, ts => {
const date = new Date(ts);
date.setUTCHours(18, 0, 0, 0);
return date.getTime();
});
const uniqDays = uniq(days);
const nUniqDays = uniqDays.length;
return nUniqDays;
}
/**
* Calculate the encouragement sentence based on
* the current days the user uses this bot
* @param {Number} currentDays
* @return {String} encouragement
*/
function getEncouragement(currentDays) {
if (currentDays < 3) {
return "再努力幾天,試著打滿3天吧!";
}
if (currentDays < 5) {
return "再努力幾天,往5天邁進!";
}
if (currentDays < 7) {
return "再撐一下,至少完成一週!";
}
return "台灣的勞工真的 hen 棒~";
}
/**
*
* @param {*} n number to be padded
* @param {*} width total width of final string
* @param {*} z padding sign
*/
function pad(n, width, z) {
z = z || "0";
n = n + "";
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
/**
* Format { hrs, mins, secs } object into readable string
* e.g. { hrs: 1, mins: 1, secs: 1} => 1小時 1分鐘
* @param {Object} timeObj
* @return {String} str
*/
function formatTime(timeObj) {
let str = "";
const { hrs, mins, secs } = timeObj;
if (hrs === 0 && mins === 0) {
return "不到一分鐘";
}
if (hrs > 0) {
str = `${hrs}小時`;
}
if (mins > 0) {
str = `${str} ${mins}分鐘`;
}
return str;
}
/**
* function to calculate local time in given UTC offset
* reference: https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript
*/
function convertTimeZone(date, offset) {
// convert to msec
// add local time zone offset
// get UTC time in msec
var utc = date.getTime() + date.getTimezoneOffset() * 60000;
// create new Date object for different city
// using supplied offset
var nd = new Date(utc + 3600000 * offset);
return nd;
}
function getYearMonthDay(date) {
return {
year: date.getFullYear(),
month: date.getMonth(),
day: date.getDate(),
};
}
/**
* Generate random string from English characters and numbers at given length
* @param {Number} length
* @return {String} randomStr
*/
function genRandomStr(length) {
var text = "";
var possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
/**
* Generate quick reply payload
* @param {*} payloads array of payload to be replied (e.g. CHECK_IN, CHECK_OUT)
*/
function genQuickReply(payloads) {
const qrs = [];
payloads.forEach(p => {
if (p.type) {
switch (p.type) {
case P.GET_STARTED:
qrs.push({
content_type: "text",
title: p.text || "開始使用",
payload: P.GET_STARTED,
});
break;
case P.CHECK_IN:
qrs.push({
content_type: "text",
title: p.text || "開始上班做功德",
payload: P.CHECK_IN,
});
break;
case P.CHECK_OUT:
qrs.push({
content_type: "text",
title: p.text || "下班了,不做了",
payload: P.CHECK_OUT,
});
break;
case P.SEND_LOCATION:
qrs.push({
content_type: "location",
title: p.text || "傳送位置",
payload: P.SEND_LOCATION,
});
break;
case P.VIEW_MY_WORKING_TIME:
qrs.push({
content_type: "text",
title: p.text || "查看我的打卡記錄",
payload: P.VIEW_MY_WORKING_TIME,
});
break;
case P.VIEW_MY_REMINDERS:
qrs.push({
content_type: "text",
title: p.text || "管理我的打卡提醒",
payload: P.VIEW_MY_REMINDERS,
});
break;
case P.VIEW_TOTAL_WORKING_TIME:
qrs.push({
content_type: "text",
title: p.text || "看看全台灣功德量",
payload: P.VIEW_TOTAL_WORKING_TIME,
});
break;
case P.VIEW_WORKING_USER_COUNT:
qrs.push({
content_type: "text",
title: p.text || "查看多少人在上班",
payload: P.VIEW_WORKING_USER_COUNT,
});
break;
case P.SHOW_QUICK_REPLY_MENU:
qrs.push({
content_type: "text",
title: p.text || "顯示功能表",
payload: P.SHOW_QUICK_REPLY_MENU,
});
break;
case P.SET_REMINDER:
qrs.push({
content_type: "text",
title: p.text || "設定打卡提醒",
payload: P.SET_REMINDER,
});
break;
default:
break;
}
} else if (p.text) {
qrs.push({
content_type: "text",
title: p.text,
payload: "",
});
}
});
return { quick_replies: qrs };
}
function genRandomReply() {
return RANDOM_REPLIES[Math.floor(Math.random() * RANDOM_REPLIES.length)];
}
// 這個是暫時用宣傳用的,就先設定 2018-01-19 ~ 2018-01-31
function getTodayPromoteImage() {
const now = convertTimeZone(new Date(), 8);
const { year, month, day } = getYearMonthDay(now);
return get(PROMO_IMG_URLS, `[${year}][${month + 1}][${day}]`);
}
function resetCheckInState(context) {
context.setState({
isWorking: false,
startTime: null,
endTime: null,
location: null,
locationTimestamp: null,
imgUrls: [],
});
}
const getPostbackPayload = path(["event", "postback", "payload"]);
/**
* parse time string to { hour, min } object if possible
* available format: "1:20" "01:20" "01:2" "01:20" " 01:20 "
* @param {String} timeStr
* @return {Object}
*/
function parseTime(timeStr) {
const regex = /\s*(\d{1,2})[:|:](\d{1,2})\s*/;
const m = timeStr.match(regex);
if (m === null) {
return null;
}
const hour = parseInt(m[1], 10);
const min = parseInt(m[2], 10);
if (hour < 0 || hour >= 24 || min < 0 || min >= 60) {
return null;
}
return { hour, min };
}
/**
* get closest time interval by given time
* e.g. interval = 10, time = { hour: 23, min: 54 } => { hour: 23, min: 50 }
* @param {*} time
* @param {*} interval
*/
function getClosestTime(time, interval) {
if (!time) {
return null;
}
let { hour, min } = time;
// get closest interval
min = Math.round(min / interval) * interval;
if (min >= 60) {
min = 0;
hour = hour + 1 >= 24 ? 0 : hour + 1;
}
return { hour, min };
}
module.exports = {
getLocation,
getTimeStamp,
getImageUrl,
calcTime,
calcCheckInDayCount,
getEncouragement,
pad,
formatTime,
convertTimeZone,
getYearMonthDay,
genRandomStr,
genQuickReply,
genRandomReply,
getTodayPromoteImage,
resetCheckInState,
getPostbackPayload,
parseTime,
getClosestTime,
};