chatbot.js 16.4 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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
var express = require('express');
var client_id = 'v3M4wjolGLkrvNA3GUIW';
var client_secret = 'fKF6vjkWhE';
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'w5i8sURqF5bof6DWeB87n+oCeWrYaFf7a5YZzfzN1jeITIlZ3PcOmZRcdGCo/djTuHhNxybfJ69y7Jex+7tipBNRynngfyWX9CK1L3EupuhnX8rubeCmJda7HvsQWXVo8ZDcwl2aLwXsE3kiYF2qEwdB04t89/1O/w1cDnyilFU=' //수정해주세용
const SECRET = 'b0b4501ebc2813a2b0e586293a35b466' //수정해주세용
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "www.osstest237.ml" //수정해주세용
const sslport = 23023;
const line = require('@line/bot-sdk');

const bodyParser = require('body-parser');
const { assert } = require('console');
var app = express();
app.use(bodyParser.json());

var usingMessage = ''
var content_id = ''
var imgDownloaded = false;
var downloadedImg = ''

//search
var search_client_id = 'cnS9zzj0OZ3xPgHqtaLJ';
var search_client_secret = 'oQGaxdr7aq';

//data parsing
var data='';
var title='';
var link='';
var category='';
var address='';
var roadAddress='';
var menu='';

var arrmenu=new Array('술','야식','한식','치킨','양식','중식','일식','고기','디저트','카페');
  
app.post('/hook', function (req, res) {

    var eventObj = req.body.events[0];
    var text = eventObj.message.text;

    // request log
    if (!(eventObj.message.type == 'image')) {
    console.log('======================', new Date() ,'======================');
    console.log('[request]', req.body);
    console.log('[request source] ', eventObj.source);
    console.log('[request message]', eventObj.message);
    }

    if (eventObj.message.type == 'location') {
        
        //위치 받아서 맛집 추천해주는 함수
        var chatbotaddress=eventObj.message;
        var chatbotdata1=chatbotaddress.address; 
        var chatbotdata2=chatbotdata1.split(' ');
        var place=chatbotdata2[3];
        
        console.log(place);
       
        var query= place + ' ' + menu + ' 맛집'; //검색 원하는 문자열 
        RecommendationResult(eventObj.replyToken, query);

        res.sendStatus(200);
    } else if (text == 'Cfr:Yes') {
        QuickReplyCfrYes(eventObj.replyToken);
        res.sendStatus(200);
    } else if (text == 'Cfr:No') {
        QuickReplyCfrNo(eventObj.replyToken);
        res.sendStatus(200);
    } else if (text == '랜덤 추천' || text == '위치 기반 추천') {
      if (text == '랜덤 추천') {

        //랜덤으로 맛집 추천해주는 함수
        var randomMenu=Math.floor(Math.random()*10);
        var query = randomMenu+' 맛집';
        RecommendationResult(eventObj.replyToken, query);

        res.sendStatus(200);
      }
        else { 
            SendingLocation(eventObj.replyToken);
            res.sendStatus(200);
        }
    } else if (eventObj.message.type == 'image') {
        content_id = eventObj.message.id;
        const downloadPath = path.join(__dirname, 'sample.jpg');
        downloadContent(content_id, downloadPath);
        //downloadedImg = content_id;
        //imgDownloaded = true;
        Checking(eventObj.replyToken);
        res.sendStatus(200);
    } else if (text == 'ㅇ') {
        //사진으로 얼굴 인식해주는 함수
        imgtodata('sample.jpg');
        SendingLocation(eventObj.replyToken);
        res.sendStatus(200);
    }
    else { 
        usingMessage = text;
        initReply(eventObj.replyToken);
        res.sendStatus(200);
    }

});

const quickReplyLocation = {
    "items": [
        {
            "type": "action",
            "action": {
              "type": "location",
              "label": "위치 입력"
            }
        }
    ]
};

const quickReplyCfrYes = {
    "items": [
      {
        "type": "action",
        "action": {
          "type": "cameraRoll",
          "label": "사진 가져오기"
        }
      },
      {
        "type": "action",
        "action": {
          "type": "camera",
          "label": "사진 찍기"
        }
      },
    ]
  };

const quickReplyCfrNo = {
    items: [
    /*
      {
        "type": "action",
        "action": {
            "type": "message",
            "label": "weather",
            "text": "날씨"
        }
      },
      {
        "type": "action",
        "action": {
            "type": "message",
            "label": "menu",
            "text": "메뉴"
        }
      },
    */
      {
        "type": "action",
        "action": {
            "type": "message",
            "label": "random",
            "text": "랜덤 추천"
        }
      },
      {
        "type": "action",
        "action": {
            "type": "message",
            "label": "location",
            "text": "위치 기반 추천"
        }
      }
    ]
  };

//기본 reply
function initReply (replyToken) {
    request.post(
        {
            url: TARGET_URL,
            headers: {
                'Authorization': `Bearer ${TOKEN}`
            },
            json: {
                "replyToken":replyToken,
                "messages":[
                    {
                        "type": "sticker",
                        "packageId": "11537",
                        "stickerId": "52002738"
                    },
                    {
                        "type": "text",
                        "text": "안녕하세요.\nCFR을 이용한 얼굴인식 맛집 추천봇입니다."
                    },
                    {
                        "type": "template",
                        "altText": "얼굴 인식을 위해 사진을 가져오시겠습니까?",
                        "template": {
                            "type": "confirm",
                            "text": "얼굴 인식을 위해 사진을 가져오시겠습니까?",
                            "actions": [
                                {
                                    "type": "message",
                                    "label": "Cfr:Yes",
                                    "text": "Cfr:Yes"
                                },
                                {
                                    "type": "message",
                                    "label": "Cfr:No",
                                    "text": "Cfr:No"
                                }
                            ]
                        }
                    }
                ]
            }
        },(error, response, body) => {
            console.log(body)
        });
}

//위치 입력 reply
function SendingLocation(replyToken) {
    request.post(
        {
            url: TARGET_URL,
            headers: {
                'Authorization': `Bearer ${TOKEN}`
            },
            json: {
                "replyToken": replyToken,
                "messages": [
                    {
                        "type": "text",
                        "text": "위치를 입력해주세요.",
                        "quickReply": quickReplyLocation
                    }
                ]
            },
            body: request
        },(error, response, body) => {
            console.log(body)
    });
}

//cfr 이용할 때 reply
function QuickReplyCfrYes(replyToken) {
    request.post(
        {
            url: TARGET_URL,
            headers: {
                'Authorization': `Bearer ${TOKEN}`
            },
            json: {
                "replyToken": replyToken,
                "messages": [
                    {
                        "type": "text",
                        "text": "사진을 입력해주세요. 얼굴 인식이 되지 않을 경우 위치 기반으로 추천됩니다.",
                        "quickReply": quickReplyCfrYes
                    }
                ]
            }
        },(error, response, body) => {
            console.log(body)
        });
}

//cfr 이용하지 않을 때 reply
function QuickReplyCfrNo (replyToken) {
    request.post(
        {
            url: TARGET_URL,
            headers: {
                'Authorization': `Bearer ${TOKEN}`
            },
            json: {
                "replyToken": replyToken,
                "messages": [
                    {
                        "type": "text",
                        "text": "랜덤 추천 옵션을 선택해주세요.",
                        "quickReply": quickReplyCfrNo
                    }
                ]
            }
        },(error, response, body) => {
            console.log(body)
        });
}

function Checking (replyToken) {
    request.post(
        {
            url: TARGET_URL,
            headers: {
                'Authorization': `Bearer ${TOKEN}`
            },
            json: {
                "replyToken": replyToken,
                "messages": [
                    {
                        "type": "text",
                        "label": "계속 진행",
                        "text": "계속 진행하시려면 'ㅇ'을 입력해주세요."
                    },
                ],
            }
        },(error, response, body) => {
            console.log(body)
        });
}


//추천 결과 + Search
function RecommendationResult(replyToken, query) {
  
  console.log(query);
  var display = '1'; //검색 결과 출력 건수. 최대 5개
  var start = '1'; //검색 시작 위치. 1만 가능
  var sort = 'comment'; //정렬 옵션 (random : 유사도순, comment : 카페/블로그 리뷰 개수 순)

  var api_url = 'https://openapi.naver.com/v1/search/local?query=' + encodeURI(query) + '&display=' + encodeURI(display) + '&start=' + encodeURI(start) + '&sort=' + encodeURI(sort);
  var options = {
    url: api_url,
    headers: { 'X-Naver-Client-Id': search_client_id, 'X-Naver-Client-Secret': search_client_secret }
  };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      //response.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
      console.log(body);
      
      //데이터 parsing
      data = JSON.parse(body);
      title = data.items[0].title;
      console.log(title);

      link = data.items[0].link;
      console.log(link);

      category = data.items[0].category;
      console.log(category);

      address = data.items[0].address;
      console.log(address);

      roadAddress = data.items[0].roadAddress;
      console.log(roadAddress);
    
      var x = Number(data.items[0].mapx);
      var y = Number(data.items[0].mapy);
        
      var longitude = TMtoWGS(x, y)[0];
      var latitude = TMtoWGS(x, y)[1];

      request.post(
        {
          url: TARGET_URL,
          headers: {
            'Authorization': `Bearer ${TOKEN}`
          },
          json: {
            "replyToken": replyToken,
            "messages": [
                {
                    "type":"location",
                    "title": title,
                    "address": address,
                    "latitude": latitude,
                    "longitude": longitude
                },
                {
                    "type": "text",
                    "text": "맛집 이름: " + title
                },
                {
                    "type":"text",
                    "text": "맛집 주소: " + address
                }
            ]
          }
        }, (error, response, body) => {
          console.log(body)
        });

    } else {
      res.status(response.statusCode).end();
      console.log('error = ' + response.statusCode);
    }
  })

}


imgtodata = function(dir) {
    var api_url = 'https://openapi.naver.com/v1/vision/face'; // 얼굴 감지

    var _formData = {
        image: 'image',
        image: fs.createReadStream(path.join(__dirname, dir)) // FILE 이름
    };

    request.post({
        url: api_url,
        formData: _formData,
        headers: {
            'X-Naver-Client-Id': client_id,
            'X-Naver-Client-Secret': client_secret
        }
    }, (err, response, body) => {
        console.log(response.statusCode); // 200
        //console.log(response.headers['content-type'])
        console.log(body);
        var cfrdata = JSON.parse(body);

        var facenum = cfrdata.info.faceCount;
        //인식된 얼굴이 없을 때
        if (facenum == 0) {
            randomMenu=Math.floor(Math.random()*10);
            menu = arrmenu[randomMenu];
        } 
        //얼굴 1개 이상
        else {
            //제일 잘 인식되는 얼굴 기준
            var cfrgender = cfrdata.faces[0].gender.value; //CFR의 성별 데이터 (json)
            var cfremotion = cfrdata.faces[0].emotion.value; //CFR의 감정 데이터 (json)

            var gender = cfrgender; //사용자 성별
            var emotion = cfremotion; //사용자 감정

            console.log(facenum);
            console.log(gender);
            console.log(emotion);

            if (gender == 'male') {
                if (emotion == 'angry') {
                    menu = arrmenu[0];
                } else if (emotion == 'disgust') {
                    menu = arrmenu[1];
                } else if (emotion == 'fear') {
                    menu = arrmenu[2];
                } else if (emotion == 'laugh') {
                    menu = arrmenu[3];
                } else if (emotion == 'neutral') {
                    menu = arrmenu[4];
                } else if (emotion == 'sad') {
                    menu = arrmenu[5];
                } else if (emotion == 'surprise') {
                    menu = arrmenu[6];
                } else if (emotion == 'smile') {
                    menu = arrmenu[7];
                } else if (emotion == 'talking') {
                    menu = arrmenu[0];
                } else {
                    randomMenu=Math.floor(Math.random()*10);
                    menu = arrmenu[randomMenu];
                }
            } 
            else if (gender == 'female') {
                if (emotion == 'angry') {
                    menu = arrmenu[7];
                } else if (emotion == 'disgust') {
                    menu = arrmenu[8];
                } else if (emotion == 'fear') {
                    menu = arrmenu[2];
                } else if (emotion == 'laugh') {
                    menu = arrmenu[6];
                } else if (emotion == 'neutral') {
                    menu = arrmenu[5];
                } else if (emotion == 'sad') {
                    menu = arrmenu[1];
                } else if (emotion == 'surprise') {
                    menu = arrmenu[5];
                } else if (emotion == 'smile') {
                    menu = arrmenu[3];
                } else if (emotion == 'talking') {
                    menu = arrmenu[9];
                } else {
                    randomMenu=Math.floor(Math.random()*10);
                    menu = arrmenu[randomMenu];
                }
            } else {
                randomMenu=Math.floor(Math.random()*10);
                menu = arrmenu[randomMenu];
            }

            console.log(menu);
        }
    });
}

//좌표 변경
const proj4 = require('proj4');
function TMtoWGS(x,y){
    var to = 'WGS84'
    var from = 'TM128'
 
    proj4.defs('WGS84', "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
    proj4.defs('TM128', '+proj=tmerc +lat_0=38 +lon_0=128 +k=0.9999 +x_0=400000 +y_0=600000 +ellps=bessel +units=m +no_defs +towgs84=-115.80,474.99,674.11,1.16,-2.31,-1.63,6.43');
 
    var xy = [x, y];
    var result = proj4(from, to, xy);
 
    return result;
}

//사용자가 보낸 사진 저장
const config = ({
    channelAccessToken: `${TOKEN}`,
    channelSecret: `${SECRET}`,
});

const client = new line.Client(config);
function downloadContent(messageId, downloadPath) {
    return client.getMessageContent(messageId)
      .then((stream) => new Promise((resolve, reject) => {
        const writable = fs.createWriteStream(downloadPath);
        stream.pipe(writable);
        stream.on('end', () => resolve(downloadPath));
        stream.on('error', reject);
      }));
}


try {
    const option = {
      ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
      key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
      cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
    };
    if (usingMessage == '') {
        HTTPS.createServer(option, app).listen(sslport, () => {
            console.log(`[HTTPS] Server is started on port ${sslport}`);
        });
    }
  } catch (error) {
    console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
    console.log(error);
}