백승욱

Add functions(ClassifyMessage / GetYesterday)

...@@ -23,6 +23,23 @@ app.use(bodyParser.json()); ...@@ -23,6 +23,23 @@ app.use(bodyParser.json());
23 // =============================================== 23 // ===============================================
24 24
25 25
26 +// [MAIN] CLASSIFY THE MESSAGE
27 +function ClassifyMessage(replyToken, imessage){
28 +
29 + var message = String(imessage);
30 +
31 + if(message.includes('최신') || message.includes('순위') || message.includes('오늘')) {
32 + showYesterdayRank(replyToken);
33 + } else if (message.includes('줄거리')) {
34 + // (예시) 영화 줄거리 출력
35 + }
36 + else if (message.includes('목록')) {
37 + // (예시) 영화 목록 출력
38 + }
39 +}
40 +
41 +
42 +// 사용자의 LINE message 수신
26 app.post('/hook', function (req, res) { 43 app.post('/hook', function (req, res) {
27 44
28 var eventObj = req.body.events[0]; 45 var eventObj = req.body.events[0];
...@@ -33,27 +50,29 @@ app.post('/hook', function (req, res) { ...@@ -33,27 +50,29 @@ app.post('/hook', function (req, res) {
33 console.log('[request source] ', eventObj.source); 50 console.log('[request source] ', eventObj.source);
34 console.log('[request message]', eventObj.message); 51 console.log('[request message]', eventObj.message);
35 52
36 - showYesterdayRank(eventObj.replyToken, eventObj.message.text); 53 + ClassifyMessage(eventObj.replyToken, eventObj.message.text);
54 +
37 res.sendStatus(200); 55 res.sendStatus(200);
38 }); 56 });
39 57
40 -// 해당 날짜 영화 순위 출력 함수
41 -// 입력 인자 중 message 에는 어제 날짜 들어감(ex: 20220528)
42 -function showYesterdayRank(replyToken, message) {
43 58
44 - // 한국영화진흥위원회 API는 get 형태로 호출 59 +// 어제 영화 순위 1위 ~ 5위 출력
60 +function showYesterdayRank(replyToken) {
61 +
62 + var yesterday = GetYesterday();
45 request.get( 63 request.get(
46 { 64 {
47 - url: BOXOFFICE_URL+`?key=${KOFIC_KEY}&targetDt=${message}`, 65 + url: BOXOFFICE_URL+`?key=${KOFIC_KEY}&targetDt=${yesterday}`,
48 json:true 66 json:true
49 },(error, response, body) => { 67 },(error, response, body) => {
50 if(!error && response.statusCode == 200) { 68 if(!error && response.statusCode == 200) {
51 console.log(body.boxOfficeResult); 69 console.log(body.boxOfficeResult);
52 70
53 - var transMessage_1 = body.boxOfficeResult.dailyBoxOfficeList[0].movieNm; 71 + var movie_1st = body.boxOfficeResult.dailyBoxOfficeList[0].movieNm;
54 - var transMessage_2 = body.boxOfficeResult.dailyBoxOfficeList[1].movieNm; 72 + var movie_2nd = body.boxOfficeResult.dailyBoxOfficeList[1].movieNm;
55 - var transMessage_3 = body.boxOfficeResult.dailyBoxOfficeList[2].movieNm; 73 + var movie_3rd = body.boxOfficeResult.dailyBoxOfficeList[2].movieNm;
56 - var transMessage_4 = body.boxOfficeResult.dailyBoxOfficeList[3].movieNm; 74 + var movie_4th = body.boxOfficeResult.dailyBoxOfficeList[3].movieNm;
75 + var movie_5th = body.boxOfficeResult.dailyBoxOfficeList[4].movieNm;
57 76
58 request.post( 77 request.post(
59 { 78 {
...@@ -66,7 +85,12 @@ function showYesterdayRank(replyToken, message) { ...@@ -66,7 +85,12 @@ function showYesterdayRank(replyToken, message) {
66 "messages":[ 85 "messages":[
67 { 86 {
68 "type":"text", 87 "type":"text",
69 - "text": `1위 : ${transMessage_1}\n2위 : ${transMessage_2}\n3위 : ${transMessage_3}\n4위 : ${transMessage_4}` 88 + "text":
89 + `1위 : ${movie_1st}\n`+
90 + `2위 : ${movie_2nd}\n`+
91 + `3위 : ${movie_3rd}\n`+
92 + `4위 : ${movie_4th}\n`+
93 + `5위 : ${movie_5th}\n`
70 } 94 }
71 ] 95 ]
72 } 96 }
...@@ -77,6 +101,22 @@ function showYesterdayRank(replyToken, message) { ...@@ -77,6 +101,22 @@ function showYesterdayRank(replyToken, message) {
77 }); 101 });
78 } 102 }
79 103
104 +
105 +// 어제 날짜를 YYYYMMDD 형식(type: string)으로 반환하는 함수
106 +function GetYesterday() {
107 +
108 + var today = new Date();
109 + var yesterday = new Date(today.setDate(today.getDate() - 1));
110 +
111 + var year = yesterday.getFullYear();
112 + var month = ('0' + (yesterday.getMonth() + 1)).slice(-2);
113 + var day = ('0' + yesterday.getDate()).slice(-2);
114 +
115 + return (year + month + day);
116 +}
117 +
118 +
119 +// ※ WARNING: DO NOT TOUCH THIS CODE SECTION
80 try { 120 try {
81 const option = { 121 const option = {
82 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 122 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......