app.js 5.83 KB

// 모듈 불러오기
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var app = express(); // express 객체 저장
var webcrawl = require('./crawling/Reol');
var async = require('async');

//body-parser 미들웨어 사용
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// http://서버주소/keyboard
app.get('/keyboard', function(req,res){
    var data = {
        'type' : 'buttons', 
        'buttons' : ['일본', '한국']};
    res.json(data);
    
});

//8080포트로 서버 접속
http.createServer(app).listen(8080, () => {
    console.log('Server running..');
});

// http://서버주소/message
app.post('/message', function(req,res){
    //유저가 입력한 데이터
    var msg = req.body.content;
    console.log('전달받은 메시지 : ' + msg);
    var send = {};
    var task = [
        function(callback){
            switch(msg){
                case 'Reol-Discography' :
                    webcrawl.crawl_Reol('https://namu.wiki/w/%EB%A0%88%EC%98%A4%EB%A3%A8/%EC%9D%8C%EB%B0%98#toc',function(Reol){
                        callback(null,Reol);
                    })
                    break;
                case '米津玄師-Discography' :

            }
            
        },
        function(discography, callback){
            switch (msg) {
                case '일본':
                    send = {
                        'message': {
                            'text': '등록된 가수 목록입니다.'
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['Reol', '米津玄師', 'yanaginagi', 'ヨルシカ', 'ダズビ', 'Polkadot Stingray', 'Aimyong']
                        }

                    };
                    break;

                case '한국':
                    send = {
                        'message': {
                            'text': '등록된 가수 목록입니다.'
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['볼빨간 사춘기', 'MOT', 'Gukkasten', 'ZICO', 'DEAN', 'IU', 'Heize']
                        }
                    };
                    break;
          // ---------- contents -------------- // 
                ///// Reol ////             
                case 'Reol' :
                    send = {
                        'message' : {
                            'text' : '이름 : Reol(れをる) \n성별 : 여성 \n생년월일 : 1993년 11월 9일 \n혈액형 : AB형',
                            'photo' : {
                                    'url' : 'https://www.reol.jp/images/profile/reol_Aphoto_2.png',
                                    'width' : 1000,
                                    'height' : 667
                            },
                            'message_button' : {
                                'label' : '공식 홈페이지',
                                'url' : "https://www.reol.jp/"
                            }
                        },
                        keyboard : {
                            'type' : 'buttons',
                            'buttons' : ['Reol-Discography']
                        }
                    };
                    break;
                case 'Reol-Discography' :
                    console.log(discography);
                    send = {
                        'message': {
                            'text': '앨범 목록입니다.'
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': discography[0].json_album
                        }
                    }
                    break;
                case discography[0].json_album[0]:
                    send = {
                        'message' : {
                            'text' : discography[0].json_album[0] + '\n 발매년도 : ' + discography[0].json_year[0] + '\n 수록곡 : \n' + discography[0].json_track[0] 
                        }
                    }
                /// 米津玄師 ///
                case '米津玄師' :
                    send = {
                        'message' : {
                            'text' : '이름 : 米津玄師(Yonezu Kenshi) \n 성별 : 남성 \n생년월일 : 1991년 3월 10일 \n혈액형 : O형',
                            'photo' : {
                                'url' : 'http://reissuerecords.net/rr/wp-content/uploads/flamingo_photo2.jpg',
                                'width' : 1000,
                                'height' : 667
                            },
                            'message_button' : {
                                'label' : '공식 홈페이지',
                                'url' : "http://reissuerecords.net/"
                            }
                        },
                        keyboard : {
                            'type' : 'buttons',
                            'buttons' : ['米津玄師-Discography']
                        }
                    };
                    break;
                case '米津玄師-Discography' :
                    send = {
                        'message' : {
                            'text' : 'Album List'
                        },                
                        keyboard : {
                            'type' : 'buttons',
                            'buttons' : ['Reol', '米津玄師', 'yanaginagi', 'ヨルシカ', 'ダズビ', 'Polkadot Stingray', 'Aimyong']
                        }
        
                    };
                default:
                    break;
            }
            callback(null ,send);
        }
    ];
    async.waterfall(task, function(err,result){
        console.log(send);
        res.json(send);
    });
    
});