엄성진

Fix Random Recommend, Fix User Setting(part)

...@@ -6,7 +6,7 @@ const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' ...@@ -6,7 +6,7 @@ const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
6 var { google } = require('googleapis'); 6 var { google } = require('googleapis');
7 var service = google.youtube('v3'); 7 var service = google.youtube('v3');
8 8
9 -//세팅값 9 +//세팅,설정
10 var setting_userId = []; //유저아이디보관 10 var setting_userId = []; //유저아이디보관
11 var setting_Thumbnail = []; // 1:썸네일 출력(기본값) / 0:미출력 11 var setting_Thumbnail = []; // 1:썸네일 출력(기본값) / 0:미출력
12 var setting_SendAmount = []; // 보낼 양 (1이상, 10이하의 정수) (기본값:1) 12 var setting_SendAmount = []; // 보낼 양 (1이상, 10이하의 정수) (기본값:1)
...@@ -23,15 +23,17 @@ function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색 ...@@ -23,15 +23,17 @@ function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색
23 temp_title[i] = title[i]; 23 temp_title[i] = title[i];
24 24
25 temp_url2[i] = url2[i]; 25 temp_url2[i] = url2[i];
26 - temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, "")); 26 + if (sendaddress) //주소 전송 활성화
27 - 27 + temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
28 + else
29 + temp_url[i] = "";
28 sendMessage += temp_title[i] + "\n" + 30 sendMessage += temp_title[i] + "\n" +
29 temp_url[i] + "\n" + "\n"; 31 temp_url[i] + "\n" + "\n";
30 } 32 }
31 33
32 34
33 console.log(sendMessage); 35 console.log(sendMessage);
34 - if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌. 36 + if (thumbnail) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
35 request.post( 37 request.post(
36 { 38 {
37 url: TARGET_URL, 39 url: TARGET_URL,
...@@ -57,58 +59,58 @@ function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색 ...@@ -57,58 +59,58 @@ function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색
57 } 59 }
58 } 60 }
59 ); 61 );
60 -} 62 + else //썸네일 비활성화
61 - 63 + request.post(
62 -function SEND_TEXT(replyToken,sendMessage) //단순 텍스트 전송하는 함수
63 -{
64 - console.log(sendMessage);
65 - request.post(
66 - {
67 - url: TARGET_URL,
68 - headers:
69 - {
70 - 'Authorization': `Bearer ${Info.TOKEN}`
71 - },
72 - json:
73 { 64 {
74 - "replyToken": replyToken, 65 + url: TARGET_URL,
75 - "messages": 66 + headers:
76 - [ 67 + {
77 - { 68 + 'Authorization': `Bearer ${Info.TOKEN}`
78 - "type": "text", 69 + },
79 - "text": sendMessage 70 + json:
80 - } 71 + {
81 - ] 72 + "replyToken": replyToken,
73 + "messages":
74 + [
75 + {
76 + "type": "text",
77 + "text": sendMessage
78 + }
79 + ]
80 + }
82 } 81 }
83 - } 82 + );
84 - );
85 } 83 }
86 84
85 +function shuffle(array) { //플레이리스트에서 랜덤으로 골라서 출력할 수 있도록 shuffle 함수를 추가함
86 + for (let i = array.length - 1; i > 0; i--) {
87 + let j = Math.floor(Math.random() * (i + 1));
88 + [array[i], array[j]] = [array[j], array[i]];
89 + }
90 +}
87 function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)//플레이리스트에서 나온 내용 전송하는 함수 91 function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)//플레이리스트에서 나온 내용 전송하는 함수
88 { 92 {
89 - //sendMessage = JSON.stringify(response.data.items[0].snippet.title); 93 + var random_order=[0,1,2,3,4,5,6,7,8,9];
94 + shuffle(random_order)
90 95
91 sendMessage = ""; 96 sendMessage = "";
92 var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = []; 97 var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
93 for (var i = 0; i < SendAmount; i++) { 98 for (var i = 0; i < SendAmount; i++) {
94 99
95 - temp_title[i] = JSON.stringify(response.data.items[i].snippet.title).replace(/\"/gi, ""); 100 + temp_title[i] = JSON.stringify(response.data.items[random_order[i]].snippet.title).replace(/\"/gi, "");
96 -
97 - temp_url2[i] = JSON.stringify(response.data.items[i].snippet.resourceId.videoId);
98 - //temp_url2[i] = choice;
99 - temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
100 -
101 - //temp_thumpnail[i] = "https://img.youtube.com/vi/" + JSON.stringify(response.data.items[i].snippet.thumbnails.high.url).replace(/\"/gi, "") + "/hqdefault.jpg";
102 - // 썸네일 : 사용자가 플레이리스트로 입력 시 리턴으로 너무 많은 정보들을 줘야함. 그래서 뺌.
103 101
102 + temp_url2[i] = JSON.stringify(response.data.items[random_order[i]].snippet.resourceId.videoId);
103 + if (sendaddress) //주소 활성화
104 + temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
105 + else
106 + temp_url[i] = "";
104 sendMessage += temp_title[i] + "\n" + 107 sendMessage += temp_title[i] + "\n" +
105 temp_url[i] + "\n" + "\n"; 108 temp_url[i] + "\n" + "\n";
106 - // temp_thumpnail[i] + "\n" + "\n";
107 } 109 }
108 110
109 111
110 console.log(sendMessage); 112 console.log(sendMessage);
111 - if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌. 113 + if (thumbnail) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
112 request.post( 114 request.post(
113 { 115 {
114 url: TARGET_URL, 116 url: TARGET_URL,
...@@ -134,27 +136,141 @@ function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)/ ...@@ -134,27 +136,141 @@ function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)/
134 } 136 }
135 } 137 }
136 ); 138 );
139 + else //썸네일 비활성화
140 + request.post(
141 + {
142 + url: TARGET_URL,
143 + headers:
144 + {
145 + 'Authorization': `Bearer ${Info.TOKEN}`
146 + },
147 + json:
148 + {
149 + "replyToken": replyToken,
150 + "messages":
151 + [
152 + {
153 + "type": "text",
154 + "text": sendMessage
155 + }
156 + ]
157 + }
158 + }
159 + );
137 } 160 }
138 161
162 +function SEND_TEXT(replyToken,sendMessage) //단순 텍스트 전송하는 함수
163 +{
164 + console.log(sendMessage);
165 + request.post(
166 + {
167 + url: TARGET_URL,
168 + headers:
169 + {
170 + 'Authorization': `Bearer ${Info.TOKEN}`
171 + },
172 + json:
173 + {
174 + "replyToken": replyToken,
175 + "messages":
176 + [
177 + {
178 + "type": "text",
179 + "text": sendMessage
180 + }
181 + ]
182 + }
183 + }
184 + );
185 +}
139 186
187 +function SETTINGS(replyToken,sendMessage) //설정 함수
188 +{
189 + console.log(sendMessage);
190 + request.post(
191 + {
192 + url: TARGET_URL,
193 + headers:
194 + {
195 + 'Authorization': `Bearer ${Info.TOKEN}`
196 + },
197 + json:
198 + {
199 + "replyToken": replyToken,
200 + "messages":
201 + [
202 + {
203 + "type": "text",
204 + "text": sendMessage
205 + },
206 + {
207 + "type": "template",
208 + "altText": "설정메뉴입니다.",
209 + "template": {
210 + "type": "buttons",
211 + "title": "설정",
212 + "text": "설정하려는 항목을 골라주세요.",
213 + "actions": [
214 + {
215 + "type": "postback",
216 + "label": "썸네일 출력 여부 변경",
217 + "data": "썸네일"
218 + },
219 + {
220 + "type": "postback",
221 + "label": "추천 음악 개수 조정",
222 + "data": "개수"
223 + },
224 + {
225 + "type": "postback",
226 + "label": "유튜브 주소 출력 여부 변경",
227 + "data": "주소"
228 + }
229 + ]
230 + }
231 + }
232 + ]
233 + }
234 + }
235 + );
236 +}
140 237
238 +exports.thumbnail_settings = function (replyToken, userId)
239 +{
240 + console.log('썸네일 출력 설정');
241 + // 여기서 썸네일을 출력할지 말지 물어보고나서, 웹훅의 응답을 받아 그 응답에 따라서
242 + // setting_Thumbnail[setting_userId.indexOf(userId)] 의 값를 수정합니다.
243 + // https://developers.line.biz/en/reference/messaging-api/#template-messages 의 confirm 를 참고해서 응용하시면 될 것 같습니다.
244 + // 이곳에 userid에 맞게 setting_Thumbnail, setting_SendAmount, setting_SendAddress 의 값을 수정해주시면 됩니다.
245 +}
141 246
142 247
248 +exports.amount_settings = function (replyToken, userId)
249 +{
250 + console.log('출력량 설정');
251 + // 여기서 노래 몇개를 출력할지 물어보고나서, 웹훅의 응답을 받아 그 응답에 따라서
252 + // setting_SendAmount[setting_userId.indexOf(userId)] 의 값를 수정합니다.
253 + // 이거는 단순하게 숫자를 입력받으면 될 것 같습니다. 다만 1,2,3,4,5,6,7,8,9,10 의 응답만 받도록 해주셔야 합니다. (예외처리 필수)
254 +}
255 +
256 +exports.address_settings = function (replyToken, userId)
257 +{
258 + console.log('유튜브 주소 출력 설정');
259 + // 여기서 유튜브 주소를 출력할지 말지 물어보고나서, 웹훅의 응답을 받아 그 응답에 따라서
260 + // setting_SendAddress[setting_userId.indexOf(userId)] 의 값를 수정합니다.
261 + // https://developers.line.biz/en/reference/messaging-api/#template-messages 의 confirm 를 참고해서 응용하시면 될 것 같습니다.
262 +}
143 263
144 exports.check = function (message, replyToken, userId) { 264 exports.check = function (message, replyToken, userId) {
145 265
146 266
147 - if (setting_userId.indexOf(userId)==-1) 267 + if (setting_userId.indexOf(userId)==-1) // 새로운 사용자가 이용할때, 설정 초기값 세팅
148 { 268 {
149 setting_userId.push(userId); 269 setting_userId.push(userId);
150 setting_Thumbnail.push(1); 270 setting_Thumbnail.push(1);
151 setting_SendAmount.push(1); 271 setting_SendAmount.push(1);
152 setting_SendAddress.push(1); 272 setting_SendAddress.push(1);
153 - console.log("없당"); 273 + console.log("알림: 새로운 사용자가 발견됨");
154 - }
155 - else
156 - {
157 - console.log("있당");
158 } 274 }
159 275
160 276
...@@ -249,7 +365,7 @@ exports.check = function (message, replyToken, userId) { ...@@ -249,7 +365,7 @@ exports.check = function (message, replyToken, userId) {
249 if (title.length==0) 365 if (title.length==0)
250 console.log('검색된 동영상이 없습니다.'); 366 console.log('검색된 동영상이 없습니다.');
251 else 367 else
252 - SEND(replyToken,title,url2,1,3,1); 368 + SEND(replyToken,title,url2,setting_Thumbnail[setting_userId.indexOf(userId)],setting_SendAmount[setting_userId.indexOf(userId)],setting_SendAddress[setting_userId.indexOf(userId)]);
253 }); 369 });
254 370
255 371
...@@ -283,12 +399,16 @@ exports.check = function (message, replyToken, userId) { ...@@ -283,12 +399,16 @@ exports.check = function (message, replyToken, userId) {
283 } 399 }
284 400
285 else if (choose == 001){ // 도움말 401 else if (choose == 001){ // 도움말
286 - sendMessage = "도움말 예시"; 402 + sendMessage = "- 기본적으로 찾고 싶은 음악을 입력하여 보내면 적절한 음악 영상을 찾아 추천해드립니다."
403 + +"\n"+"예시) 소녀시대 노래"
404 + +"\n"+"- '플레이리스트'를 입력하면 분류에 따라 엄선한 음악을 추천드립니다!"
405 + +"\n"+"- '설정'을 입력하면 썸네일 출력 여부, 추천 음악 개수(1~10), 유튜브 주소 전송 여부를 설정하실 수 있습니다."
406 + +"\n"+"- 이 도움말을 다시 보고 싶으시다면 '도움말'을 입력하시면 됩니다! 음악과 함께 좋은 하루 보내세요.";
287 SEND_TEXT(replyToken,sendMessage); 407 SEND_TEXT(replyToken,sendMessage);
288 } 408 }
289 else if (choose == 002) { // 설정 409 else if (choose == 002) { // 설정
290 sendMessage = "설정 예시"; 410 sendMessage = "설정 예시";
291 - SEND_TEXT(replyToken,sendMessage); 411 + SETTINGS(replyToken,sendMessage);
292 } 412 }
293 413
294 else { //플레이리스트 414 else { //플레이리스트
...@@ -308,7 +428,7 @@ exports.check = function (message, replyToken, userId) { ...@@ -308,7 +428,7 @@ exports.check = function (message, replyToken, userId) {
308 if (video.length == 0) 428 if (video.length == 0)
309 console.log('검색된 동영상이 없습니다.'); 429 console.log('검색된 동영상이 없습니다.');
310 else 430 else
311 - SEND_LIST(replyToken,response,choice,1,10,1); 431 + SEND_LIST(replyToken,response,choice,setting_Thumbnail[setting_userId.indexOf(userId)],setting_SendAmount[setting_userId.indexOf(userId)],setting_SendAddress[setting_userId.indexOf(userId)]);
312 } 432 }
313 ) 433 )
314 } 434 }
......
...@@ -20,11 +20,33 @@ app.post('/hook', function (req, res) { ...@@ -20,11 +20,33 @@ app.post('/hook', function (req, res) {
20 20
21 // request log 21 // request log
22 console.log('======================', new Date(), '======================'); 22 console.log('======================', new Date(), '======================');
23 - 23 + console.log(eventObj);
24 - send(eventObj.replyToken, message.text,eventObj.source.userId); 24 + if (eventObj.type=="message") // 일반 메시지일때
25 + react(eventObj.replyToken, message.text,eventObj.source.userId);
26 + else if (eventObj.postback.data=="썸네일") //설정에서 썸네일 설정버튼을 눌렀을때
27 + songs.thumbnail_settings(eventObj.replyToken,eventObj.source.userId);
28 +
29 + //else if (썸네일 설정 버튼을 누르고 거기서 응답이 들어왔을때)
30 +
31 + else if (eeventObj.postback.data=="개수") //설정에서 출력 개수 설정버튼을 눌렀을때
32 + songs.amount_settings(eventObj.replyToken,eventObj.source.userId);
33 +
34 + //else if (출력 개수 설정 버튼을 누르고 거기서 응답이 들어왔을때)
35 +
36 + else if (eventObj.postback.data=="주소") //설정에서 유튜브 주소 설정버튼을 눌렀을때
37 + songs.address_settings(eventObj.replyToken,eventObj.source.userId);
38 +
39 + //else if (유튜브 주소 설정 버튼을 누르고 거기서 응답이 들어왔을때)
40 +
41 + //★★else if (플레이리스트를 입력했을때 뜬 버튼에서 장르를 눌렀을때)
42 +
43 + //★★else if (플레이리스트를 입력했을때 뜬 버튼에서 무드를 눌렀을때)
44 +
45 + //★★else if (플레이리스트를 입력했을때 뜬 버튼에서 가수를 눌렀을때)
46 +
25 res.sendStatus(200); 47 res.sendStatus(200);
26 }); 48 });
27 -function send(replyToken, message,userId) { 49 +function react(replyToken, message,userId) {
28 50
29 request.post( 51 request.post(
30 { 52 {
......