-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
392 lines (373 loc) · 13.7 KB
/
main.js
File metadata and controls
392 lines (373 loc) · 13.7 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
const usersDataFunc = require("./Utils/getuserlist.js"); //getuserlist.js에서 start 함수 받아옴
const schedule = require("node-schedule");
const sendMsg = require("./Utils/sendmsg.js");
const report = require("./Report/report.js");
const msgBlocks = require("./Utils/msgBlocks.json");
const getPeriod = require("./Utils/getperiod.js");
const { App } = require("@slack/bolt");
const { signingSecret, token } = require("./db/token.js"); //module.exports = {signingSecret, token}
const moment = require("moment");
const app = new App({ signingSecret, token });
const db = require("./db/dbconnection");
moment.locale("ko");
app.action("action_yes", async ({ body, ack, say, respond }) => {
await ack();
let prevDate = moment(body.message.ts * 1000).format('YYYY-MM-DD HH:mm:ss');
let currDate = moment().format('YYYY-MM-DD HH:mm:ss');
let prevWeek = await getPeriod(prevDate);
let currWeek = await getPeriod(currDate);
if ((prevWeek !== currWeek) || (currWeek === null))
{
const result = await respond({
replace_original: true,
blocks: [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "💥 timeout",
"emoji": true
},
},
],
});
return;
} else {
const result = await respond({
replace_original: true,
blocks: [
{
type: "divider",
},
{
type: "header",
text: {
type: "plain_text",
text: `${moment(body.message.ts * 1000).format("MM/DD (ddd)")}`,
emoji: true,
},
},
msgBlocks.yesUndo,
{
type: "divider",
},
],
});
};
// Report 작성 로그 추가
await report.addReportLog(body.user.id, prevDate, currWeek);
// 이번주 작성 Report 개수 조회
let weekNum = "week" + currWeek;
db.query(
`SELECT ${weekNum} FROM user WHERE user_id = "${body.user.id}"`,
function (error, results, fields) {
if (error) {
console.log(error);
} else {
say(`이번주에 ${results[0][weekNum]} 개의 보고서를 작성하셨습니다.`);
}
}
);
});
app.action("action_no", async ({ body, ack, say, respond }) => {
await ack();
let prevDate = moment(body.message.ts * 1000).format('YYYY-MM-DD HH:mm:ss');
let currDate = moment().format('YYYY-MM-DD HH:mm:ss');
let prevWeek = await getPeriod(prevDate);
let currWeek = await getPeriod(currDate);
if ((prevWeek !== currWeek) || (currWeek === null))
{
const result = await respond({
replace_original: true,
blocks: [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "💥 timeout",
"emoji": true
},
},
],
});
return;
} else {
const result = await respond({
replace_original: true,
blocks: [
msgBlocks.noUndo
],
});
};
let weekNum = "week" + currWeek;
db.query(
`SELECT ${weekNum} FROM user WHERE user_id = "${body.user.id}"`,
function (error, results, fields) {
if (error) {
console.log(error);
} else {
say(`이번주에 ${results[0][weekNum]} 개의 보고서를 작성하셨습니다.`);
}
}
);
});
app.action("action_undo", async ({ body, ack, say, respond }) => {
await ack();
let prevDate = moment(body.message.ts * 1000).format('YYYY-MM-DD HH:mm:ss');
let currDate = moment().format('YYYY-MM-DD HH:mm:ss');
let prevWeek = await getPeriod(prevDate);
let currWeek = await getPeriod(currDate);
if (currWeek === null){
say("보고서 작성 기간이 아닙니다.");
return ;
} else if (prevWeek !== currWeek) {
say("한 주가 지난 후에는 응답을 수정하실 수 없습니다.");
return ;
} else {
const result = await respond({
replace_original: true,
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: "오늘 보고서 작성하셨나요?",
emoji: true,
},
},
msgBlocks.btnYesNo
],
});
}
// 이전에 YES를 누른 후 undo를 눌렀을 경우에만 이번주 작성 Report 개수 1 감소
await report.deleteReportLog(body.user.id, prevDate, currWeek);
});
const addUser = require("./User/saveDB.js");
app.message("!join", async ({ body, say }) => {
if (body.challenge && body.type == "url_verification") {
res.json({ challenge: body.challenge });
}
try {
let usersData = await usersDataFunc();
if (usersData != undefined) {
let usersStore = usersData.usersStore;
let user_id = body.event.user;
let intra_id = usersStore[user_id].name;
//user테이블에 해당 유저 데이터가 있는지 조회 후 처리
db.query(
`SELECT * FROM user WHERE user_id = "${user_id}"`,
(error, results, fields) => {
if (error) {
console.log(error);
} else {
//console.log(results[0] === undefined);
if (results[0] === undefined) {
//유저 정보가 없을 경우 유저 데이터 삽입
addUser(user_id, intra_id);
say(
{
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: `<@${intra_id}> 등록 완료!`,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text:
"`!help` 명령어로 레봇의 기능을 확인할 수 있습니다.",
},
},
],
}
);
console.log("=================new Person=================");
} else {
//유저 데이터가 이미 존재할 경우 메세지 응답
say(`<@${user_id}>은 이미 등록된 유저입니다!`);
console.log("=================already Person=================");
}
}
}
);
}
} catch (error) {
console.error(error);
}
});
app.message("!delete", async ({ body, say }) => {
if (body.challenge && body.type == "url_verification") {
res.json({ challenge: body.challenge });
}
try {
let user_id = body.event.user;
console.log(body.event.user);
db.query(`SELECT * FROM user WHERE user_id = "${user_id}"`, (error, results, fields) => {
if (error) console.error(error);
else {
if (results[0] != undefined) {
db.query(
`DELETE FROM user WHERE user_id = "${user_id}"`,
(error, results, fileds) => {
if (error) console.error(error);
else say(`<@${user_id}> 삭제 완료!\n레봇에서 모든 데이터가 삭제됩니다.`);
}
);
} else {
say(`<@${user_id}>은 등록되지 않은 유저입니다.`);
}
}
});
} catch (error) {
console.error(error);
}
});
app.message("!push", async ({ body, say }) => {
if (body.challenge && body.type == "url_verification") {
res.json({ challenge: body.challenge });
}
try {
let user_id = body.event.user;
let msg = body.event.text.substring(6);
let msg_state = [
{
msg: "off",
num: 0,
text: "항상 off",
},
{
msg: "on",
num: 1,
text: "항상 on",
},
{
msg: "sun",
num: 2,
text: "일요일만 on",
},
{
msg: "state",
text: "알림 상태 확인",
},
];
let value = { num: 404, text: "없는 명령어" };
msg_state.forEach((i) => {
if (i.msg === msg) value = i;
});
db.query(`SELECT * FROM user WHERE user_id = "${user_id}"`, (error, results, fields) => {
if (error) console.error(error);
else {
if (results[0] != undefined) {
if (value.msg === "state") {
let on_off = results[0].on_off;
say(`<@${user_id}>의 알림상태: ${msg_state[on_off].text}`);
} else if (value.num != 404) {
db.query(
`UPDATE user SET on_off = ${value.num} WHERE user_id = "${user_id}"`,
(error, results, fileds) => {
if (error) console.error(error);
else say(`<@${user_id}>: ${value.text} 설정완료 :)`);
}
);
}
console.log(`!push ${msg}: ${body.event.user}`);
} else {
say(`<@${user_id}>은 등록되지 않은 유저입니다.`);
}
}
});
} catch (error) {
console.error(error);
}
});
app.message("!help", async ({ body, say }) => {
try {
await say({
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "`!join`\n: 구독 시작\n`!delete`\n: 구독 취소",
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text:
"`!push on`\n: 모든 알림 받기 (기본)\n`!push sun`\n: 일요일에 마감일 알림만 받기\n" +
"`!push off`\n: 모든 알림 끄기\n" +
"`!push state`\n: 알림 상태 확인\n" +
"`!count`\n: 이번주에 작성한 보고서 개수 확인",
},
},
{
type: "divider",
},
{
type: "section",
text: {
"type": "mrkdwn",
"text": "<https://github.com/42Helper/42Report_helper|레봇 github 구경하기>"
},
},
{
type: "section",
text: {
"type": "plain_text",
"text": "문의: @gkim @hjung @hyejshin"
},
},
],
});
} catch (error) {
console.error(error);
}
});
app.message("!count", async ({ body, say }) => {
try {
let user_id = body.event.user;
let currDate = moment().format('YYYY-MM-DD HH:mm:ss');
let currWeek = await getPeriod(currDate);
if (currWeek === null) say("보고서 작성 기간이 아닙니다");
else {
let weekNum = "week" + currWeek;
db.query(
`SELECT ${weekNum} FROM user WHERE user_id="${user_id}"`,
function (error, results, fields) {
if (error) {
console.log(error);
} else {
say(
`이번주에 ${results[0][weekNum]} 개의 보고서를 작성하셨습니다.`
);
}
}
);
}
} catch (error) {
console.error(error);
}
});
const isresetWeek = require("./Report/resetcount.js");
(async () => {
await app.start(process.env.PORT || 3000);
schedule.scheduleJob("00 21 * * *", function () {
sendMsg.dailyMsg();
});
schedule.scheduleJob(`00 17 * * 7`, function () {
sendMsg.sundayMsg();
});
schedule.scheduleJob(`00 00 * * 1`, isresetWeek);
})();