장소원

chatbot + search + cfr and connect

Showing 1 changed file with 202 additions and 53 deletions
...@@ -3,12 +3,12 @@ var client_id = 'v3M4wjolGLkrvNA3GUIW'; ...@@ -3,12 +3,12 @@ var client_id = 'v3M4wjolGLkrvNA3GUIW';
3 var client_secret = 'fKF6vjkWhE'; 3 var client_secret = 'fKF6vjkWhE';
4 const request = require('request'); 4 const request = require('request');
5 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 5 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
6 -const TOKEN = 'w5i8sURqF5bof6DWeB87n+oCeWrYaFf7a5YZzfzN1jeITIlZ3PcOmZRcdGCo/djTuHhNxybfJ69y7Jex+7tipBNRynngfyWX9CK1L3EupuhnX8rubeCmJda7HvsQWXVo8ZDcwl2aLwXsE3kiYF2qEwdB04t89/1O/w1cDnyilFU=' 6 +const TOKEN = 'hMnfhBQc8nadyn5Ow6aejAVDUoUEp9N8YxOFxfOB2V83TOf0vjquT4cC8ll4Ccq4hkWJ8xHij53FzjMteqLLuUL6bZs+ZONI+f5aawIulRg4Y4FFBGp1O03awvgxGn503iyI7+5iQCEi80Kus6cRZQdB04t89/1O/w1cDnyilFU=' //수정해주세용
7 -const SECRET = 'b0b4501ebc2813a2b0e586293a35b466' 7 +const SECRET = '270103fd4cbd81695ceb6d0ed7f85f4b' //수정해주세용
8 const fs = require('fs'); 8 const fs = require('fs');
9 const path = require('path'); 9 const path = require('path');
10 const HTTPS = require('https'); 10 const HTTPS = require('https');
11 -const domain = "www.osstest237.ml" 11 +const domain = "www.osschatbot.tk" //수정해주세용
12 const sslport = 23023; 12 const sslport = 23023;
13 const line = require('@line/bot-sdk'); 13 const line = require('@line/bot-sdk');
14 14
...@@ -22,6 +22,19 @@ var content_id = '' ...@@ -22,6 +22,19 @@ var content_id = ''
22 var imgDownloaded = false; 22 var imgDownloaded = false;
23 var downloadedImg = '' 23 var downloadedImg = ''
24 24
25 +//search
26 +var search_client_id = 'cnS9zzj0OZ3xPgHqtaLJ';
27 +var search_client_secret = 'oQGaxdr7aq';
28 +
29 +//data parsing
30 +var data='';
31 +var title='';
32 +var link='';
33 +var category='';
34 +var address='';
35 +var roadAddress='';
36 +
37 +
25 app.post('/hook', function (req, res) { 38 app.post('/hook', function (req, res) {
26 39
27 var eventObj = req.body.events[0]; 40 var eventObj = req.body.events[0];
...@@ -38,8 +51,16 @@ app.post('/hook', function (req, res) { ...@@ -38,8 +51,16 @@ app.post('/hook', function (req, res) {
38 if (eventObj.message.type == 'location') { 51 if (eventObj.message.type == 'location') {
39 52
40 //위치 받아서 맛집 추천해주는 함수 53 //위치 받아서 맛집 추천해주는 함수
54 + var chatbotaddress=eventObj.message;
55 + var chatbotdata1=chatbotaddress.address;
56 + var chatbotdata2=chatbotdata1.split(' ');
57 + var place=chatbotdata2[0];
58 +
59 + console.log(place);
60 + var menu='';
61 + var query= place + ' ' + menu + ' 맛집'; //검색 원하는 문자열
62 + RecommendationResult(eventObj.replyToken, query);
41 63
42 - RecommendationResult(eventObj.replyToken);
43 res.sendStatus(200); 64 res.sendStatus(200);
44 } else if (text == 'Cfr:Yes') { 65 } else if (text == 'Cfr:Yes') {
45 QuickReplyCfrYes(eventObj.replyToken); 66 QuickReplyCfrYes(eventObj.replyToken);
...@@ -48,13 +69,14 @@ app.post('/hook', function (req, res) { ...@@ -48,13 +69,14 @@ app.post('/hook', function (req, res) {
48 QuickReplyCfrNo(eventObj.replyToken); 69 QuickReplyCfrNo(eventObj.replyToken);
49 res.sendStatus(200); 70 res.sendStatus(200);
50 } else if (text == '랜덤 추천' || text == '위치 기반 추천') { 71 } else if (text == '랜덤 추천' || text == '위치 기반 추천') {
51 - if (text == '랜덤 추천') { 72 + if (text == '랜덤 추천') {
52 73
53 - //랜덤으로 맛집 추천해주는 함수 74 + //랜덤으로 맛집 추천해주는 함수
75 + var query = '맛집';
76 + RecommendationResult(eventObj.replyToken, query);
54 77
55 - RecommendationResult(eventObj.replyToken); 78 + res.sendStatus(200);
56 - res.sendStatus(200); 79 + }
57 - }
58 else { 80 else {
59 SendingLocation(eventObj.replyToken); 81 SendingLocation(eventObj.replyToken);
60 res.sendStatus(200); 82 res.sendStatus(200);
...@@ -169,7 +191,7 @@ function initReply (replyToken) { ...@@ -169,7 +191,7 @@ function initReply (replyToken) {
169 }, 191 },
170 { 192 {
171 "type": "text", 193 "type": "text",
172 - "text": "안녕하세요.\nCFR을 이용한 경기도 맛집 추천봇입니다." 194 + "text": "안녕하세요.\nCFR을 이용한 얼굴인식 맛집 추천봇입니다."
173 }, 195 },
174 { 196 {
175 "type": "template", 197 "type": "template",
...@@ -215,7 +237,8 @@ function SendingLocation(replyToken) { ...@@ -215,7 +237,8 @@ function SendingLocation(replyToken) {
215 "quickReply": quickReplyLocation 237 "quickReply": quickReplyLocation
216 } 238 }
217 ] 239 ]
218 - } 240 + },
241 + body: request
219 },(error, response, body) => { 242 },(error, response, body) => {
220 console.log(body) 243 console.log(body)
221 }); 244 });
...@@ -289,48 +312,98 @@ function Checking (replyToken) { ...@@ -289,48 +312,98 @@ function Checking (replyToken) {
289 }); 312 });
290 } 313 }
291 314
292 -//추천 결과 315 +
293 -function RecommendationResult(replyToken) { 316 +//추천 결과 + Search
294 - request.post( 317 +function RecommendationResult(replyToken, query) {
318 +
319 + console.log(query);
320 + var display = '1'; //검색 결과 출력 건수. 최대 5개
321 + var start = '1'; //검색 시작 위치. 1만 가능
322 + var sort = 'comment'; //정렬 옵션 (random : 유사도순, comment : 카페/블로그 리뷰 개수 순)
323 +
324 + var api_url = 'https://openapi.naver.com/v1/search/local?query=' + encodeURI(query) + '&display=' + encodeURI(display) + '&start=' + encodeURI(start) + '&sort=' + encodeURI(sort);
325 + var options = {
326 + url: api_url,
327 + headers: { 'X-Naver-Client-Id': search_client_id, 'X-Naver-Client-Secret': search_client_secret }
328 + };
329 + request.get(options, function (error, response, body) {
330 + if (!error && response.statusCode == 200) {
331 + //response.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
332 + console.log(body);
333 +
334 + //데이터 parsing
335 + data = JSON.parse(body);
336 + title = data.items[0].title;
337 + console.log(title);
338 +
339 + link = data.items[0].link;
340 + console.log(link);
341 +
342 + category = data.items[0].category;
343 + console.log(category);
344 +
345 + address = data.items[0].address;
346 + console.log(address);
347 +
348 + roadAddress = data.items[0].roadAddress;
349 + console.log(roadAddress);
350 +
351 + request.post(
295 { 352 {
296 - url: TARGET_URL, 353 + url: TARGET_URL,
297 - headers: { 354 + headers: {
298 - 'Authorization': `Bearer ${TOKEN}` 355 + 'Authorization': `Bearer ${TOKEN}`
299 - }, 356 + },
300 - json: { 357 + json: {
301 - "replyToken": replyToken, 358 + "replyToken": replyToken,
302 - "messages": [ 359 + "messages": [
303 - { 360 + /*{
304 - "type": "imagemap", 361 + "type": "imagemap",
305 - // 이미지 불러올 수 없습니다 뜸 362 + // 이미지 불러올 수 없습니다 뜸
306 - "baseUrl": "https://www.flaticon.com/free-icon/food-store_2934069?term=restaurant&page=1&position=7&related_item_id=2934069", 363 + "baseUrl": "https://www.flaticon.com/free-icon/food-store_2934069?term=restaurant&page=1&position=7&related_item_id=2934069",
307 - "altText": "이미지를 누르시면 해당 가게로 이동합니다.", 364 + "altText": "이미지를 누르시면 해당 가게로 이동합니다.",
308 - "baseSize": { 365 + "baseSize": {
309 - "width": 1040, 366 + "width": 1040,
310 - "height": 1040 367 + "height": 1040
311 - }, 368 + },
312 - "actions": [ 369 + "actions": [
313 - { 370 + {
314 - "type": "uri", 371 + "type": "uri",
315 - "linkUri": `${link}`, // 가게 링크 372 + "linkUri": link, // 가게 링크
316 - "area": { 373 + "area": {
317 - "x":0, 374 + "x": 0,
318 - "y":0, 375 + "y": 0,
319 - "width":1040, 376 + "width": 1040,
320 - "height":1040 377 + "height": 1040
321 - }
322 - }
323 - ]
324 - },
325 - {
326 - "type": "text",
327 - "text": "기타 결과 출력" // 정보 수정
328 } 378 }
379 + }
329 ] 380 ]
330 - } 381 + },*/
331 - },(error, response, body) => { 382 + {
332 - console.log(body) 383 + "type": "text",
384 + "text": "맛집 이름: " + title
385 + },
386 + {
387 + "type":"text",
388 + "text": "맛집 주소: " + address
389 + }
390 + /*{
391 + "type":"location",
392 + "title":"맛집 주소",
393 + "address":address
394 + }*/
395 + ]
396 + }
397 + }, (error, response, body) => {
398 + console.log(body)
333 }); 399 });
400 +
401 + } else {
402 + res.status(response.statusCode).end();
403 + console.log('error = ' + response.statusCode);
404 + }
405 + })
406 +
334 } 407 }
335 408
336 imgtodata = function(dir){ 409 imgtodata = function(dir){
...@@ -349,13 +422,89 @@ imgtodata = function(dir){ ...@@ -349,13 +422,89 @@ imgtodata = function(dir){
349 }, (err,response,body) =>{ 422 }, (err,response,body) =>{
350 console.log(response.statusCode); // 200 423 console.log(response.statusCode); // 200
351 //console.log(response.headers['content-type']) 424 //console.log(response.headers['content-type'])
352 - 425 + console.log(body);
353 - data=JSON.parse(body); 426 + var cfrdata=JSON.parse(body);
354 - gender=data.faces[0].gender.value; 427 +
355 - emotion=data.faces[0].emotion.value 428 + var cfrgender=cfrdata.faces[0].gender.value; //CFR의 성별 데이터 (json)
429 + var cfremotion=cfrdata.faces[0].emotion.value; //CFR의 감정 데이터 (json)
430 +
431 + var gender = cfrgender; //사용자 성별
432 + var emotion = cfremotion; //사용자 감정
433 +
356 434
357 console.log(gender); 435 console.log(gender);
358 console.log(emotion); 436 console.log(emotion);
437 +
438 + if(gender=='male'){
439 + if(emotion=='angry'){
440 + menu='술';
441 + }
442 + else if(emotion=='disgust'){
443 + menu='야식';
444 + }
445 + else if(emotion=='fear'){
446 + menu='한식';
447 + }
448 + else if(emotion=='laugh'){
449 + menu='치킨';
450 + }
451 + else if(emotion=='neutral'){
452 + menu='양식';
453 + }
454 + else if(emotion=='sad'){
455 + menu='중식';
456 + }
457 + else if(emotion=='surprise'){
458 + menu='일식';
459 + }
460 + else if(emotion=='smile'){
461 + menu='고기';
462 + }
463 + else if(emotion=='talking'){
464 + menu='술';
465 + }
466 + else{
467 + menu='';
468 + }
469 + }
470 + else if(gender=='female'){
471 + if(emotion=='angry'){
472 + menu='고기';
473 + }
474 + else if(emotion=='disgust'){
475 + menu='디저트';
476 + }
477 + else if(emotion=='fear'){
478 + menu='한식';
479 + }
480 + else if(emotion=='laugh'){
481 + menu='일식';
482 + }
483 + else if(emotion=='neutral'){
484 + menu='중식';
485 + }
486 + else if(emotion=='sad'){
487 + menu='야식';
488 + }
489 + else if(emotion=='surprise'){
490 + menu='중식';
491 + }
492 + else if(emotion=='smile'){
493 + menu='치킨';
494 + }
495 + else if(emotion=='talking'){
496 + menu='카페';
497 + }
498 + else{
499 + menu='';
500 + }
501 + }
502 + else{
503 + menu='';
504 + }
505 +
506 + console.log(menu);
507 +
359 508
360 //return {gender:gender,emotion:emotion}; 509 //return {gender:gender,emotion:emotion};
361 }); 510 });
......