app.js 3.17 KB
var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'dClDD+86CVGoR/ozUsC1ViT0lyRMsFwQ3bOoUejOVI4yJ2AQRKHFyBF+CjYW5DLtF7EztDgigy+VSDFQxYNtvGqLGngGNYz1Fzq6kxxI7YqlDnVRV7gHFVkc42VfcEMQ/ozXTqeCY1JYFAmw30fBXQdB04t89/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 = "2019102195.oss2021.tk"
const sslport = 23023;

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":"저에게 '테스트'라고 입력해주세요!"
                }
            ]
        }
    },(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;

    // request log
    console.log('======================', new Date() ,'======================');
    console.log('[request]', req.body);
    console.log('[request source] ', eventObj.source);
    console.log('[request message]', eventObj.message);

    if(eventObj.message.text=='테스트'){
        request.post(
            {
                url: TARGET_URL,
                headers: {
                    'Authorization': `Bearer ${TOKEN}`
                },
                json: {
                    "replyToken":eventObj.replyToken,
                    "messages":[
                        {
                            "type":"text",
                            "text":"테스트라고 입력해주셨군요!"
                        },
                        {
                            "type":"text",
                            "text":"다음 단계로 넘어가고 싶으신가요? [Y/N]"
                        }
                    ]
                }
            },(error, response, body) => {
                console.log(body)
            });
    }
    

    res.sendStatus(200);
});

function color_chatbot_test1(replyToken, message){
    
}



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