app.js 5.65 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;

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;


    start(eventObj.replyToken, eventObj.message.text);
    test1(eventObj.replyToken, eventObj.message.text);


    res.sendStatus(200);
});

function start(replyToken, message) {
    if (message == "Y") {
        request.post(
            {
                url: TARGET_URL,
                headers: {
                    'Authorization': `Bearer ${TOKEN}`
                },
                json: {
                    "replyToken": replyToken,
                    "messages": [
                        {
                            "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)
            });
    }
}

function test1(replyToken, message) {
    if (message == "네") {
        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);
}