app.js 7.89 KB
// var express = require('express');
// const request = require('request');
// const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
// const TOKEN = 'Lei+CfpMSJB1lms9gmNi6kakwT2Zz5bv1E/vxXoewtNMoAaxoBEyQnniunTw19fCGl9KXU7UnVQE7IXAPgN0FyeG/bhBPvpdV0fEgpYBXM0PNiRoLsN8oMAlY99pXvq8QZ1KXefuTcr1WTBerglnagdB04t89/1O/w1cDnyilFU='
// const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
// const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
// const fs = require('fs');
// const path = require('path');
// const HTTPS = require('https');
// const domain = "2019102175.oss-2021.tk"
// const sslport = 23023;


var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'Lei+CfpMSJB1lms9gmNi6kakwT2Zz5bv1E/vxXoewtNMoAaxoBEyQnniunTw19fCGl9KXU7UnVQE7IXAPgN0FyeG/bhBPvpdV0fEgpYBXM0PNiRoLsN8oMAlY99pXvq8QZ1KXefuTcr1WTBerglnagdB04t89/1O/w1cDnyilFU='
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "2019102175.oss-2021.tk"
const sslport = 23023;

var spring = 0
var summer = 0
var fall = 0
var winter = 0

var files = [
    'https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png',
    'https://www.colorcombos.com/images/colors/990000.png'
    //     'https://plainbackground.com/download.php?imagename=5b0000.png',
    //     'https://www.solidbackgrounds.com/images/2048x2048/2048x2048-red-munsell-solid-color-background.jpg',
    //     'https://www.cmu.edu/brand/brand-guidelines/images/colors-carnegie-red-600x600.png',
    //     'https://3.imimg.com/data3/FO/EO/MY-149995/lake-amaranth-food-color-250x250.jpg'
]

var previews = [
    'https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png',
    'https://www.colorcombos.com/images/colors/990000/preview.png'
]



const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
const USER_ID = 'Ub99bb2ebebbbc1e06efe715b76ec9fcd'

const bodyParser = require('body-parser');
var app = express();

request.post(
    {
        url: BROAD_TARGET_URL,
        headers: {
            'Authorization': `Bearer ${TOKEN}`
        },
        json: {
            "messages": [
                {
                    "type": "text",
                    "text": "안녕하세요! "
                },
                {
                    "type": "text",
                    "text": "제시되는 4가지 사진 중 자신의 피부와 가장 잘 어울리는 사진의 번호를 입력하세요"
                },
                {
                    "type": "text",
                    "text": "퍼스널 컬러 진단 테스트를 시작하시겠습니까? [Y/N]"
                }
            ]
        }
    }, (error, response, body) => {
        console.log(body)
    });


app.use(bodyParser.json());
app.post('/hook', function (req, res) {

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

    test1(eventObj.replyToken, eventObj.message.text)
    test2(eventObj.replyToken, eventObj.message.text)

    res.sendStatus(200);
});

function test1(replyToken, message) {
    if (message == "Y") {

        request.post(
            {
                url: TARGET_URL,
                headers: {
                    'Authorization': `Bearer ${TOKEN}`
                },
                json: {
                    "replyToken": replyToken,
                    "messages": [
                        {
                            "type": "text",
                            "text": "test1"
                        },
                        {
                            "type": "image",
                            // "originalContentUrl": files[i],
                            // "previewImageUrl": previews[i],
                            "originalContentUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png",
                            "previewImageUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png",
                        },
                    ],

                }
            }, (error, response, body) => {
                console.log(body)
            });


    }


}

function test2(replyToken, message) {
    if (message == "1" || message == "2" || message == "3" || message == "4") {
        request.post(
            {
                url: TARGET_URL,
                headers: {
                    'Authorization': `Bearer ${TOKEN}`
                },
                json: {
                    "replyToken": replyToken,
                    "messages": [
                        {
                            "type": "text",
                            "text": "test2"
                        },
                        {
                            "type": "image",
                            "originalContentUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png",
                            "previewImageUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png",
                        },
                        {
                            "type": "image",
                            "originalContentUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png",
                            "previewImageUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png",
                        },
                        {
                            "type": "image",
                            "originalContentUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png",
                            "previewImageUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png",
                        },
                        {
                            "type": "image",
                            "originalContentUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/img.png",
                            "previewImageUrl": "https://blog.kakaocdn.net/dn/bf78xb/btqGzYCfgcc/QNj0SdL6KP6Ww89Mtgrmh1/preview.png",
                        },
                    ],

                }
            }, (error, response, body) => {
                console.log(body)
            });
    }
    if (message == "1") spring += 1
    else if (message == "2") summer += 1
    else if (message == "3") fall += 1
    else if (message == "4") winter += 1
    console.log(spring, summer, fall, winter)

}





function start(replyToken, message) {
    if (message == "Y") {
        request.post(
            {
                url: TARGET_URL,
                headers: {
                    'Authorization': `Bearer ${TOKEN}`
                },
                json: {
                    "replyToken": replyToken,
                    "messages": [
                        {
                            "type": "text",
                            "text": "네가지 사진 중 자신의 피부와 가장 잘 어울리는 색을 골라주세요"
                        }
                    ]
                }
            }, (error, response, body) => {
                console.log(body)
            });
    }
}


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(),
    };

    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);
}