EC2 Default User

ADD : reply.js

Showing 1 changed file with 69 additions and 0 deletions
1 +var express = require('express');
2 +const request = require('request');
3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 +const TOKEN = '채널 토큰으로 변경'
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "도메인 변경"
9 +const sslport = 23023;
10 +
11 +const bodyParser = require('body-parser');
12 +var app = express();
13 +app.use(bodyParser.json());
14 +app.post('/hook', function (req, res) {
15 +
16 + var eventObj = req.body.events[0];
17 + var source = eventObj.source;
18 + var message = eventObj.message;
19 +
20 + // request log
21 + console.log('======================', new Date() ,'======================');
22 + console.log('[request]', req.body);
23 + console.log('[request source] ', eventObj.source);
24 + console.log('[request message]', eventObj.message);
25 +
26 + if (message.text == 'help') {
27 + request.post(
28 + {
29 + url: TARGET_URL,
30 + headers: {
31 + 'Authorization': `Bearer ${TOKEN}`
32 + },
33 + json: {
34 + "replyToken":eventObj.replyToken,
35 + "messages":[
36 + {
37 + "type":"text",
38 + "text":"반갑습니다. 주식 알리미 입니다."
39 + },
40 + {
41 + "type":"text",
42 + "text":"May I help you?"
43 + }
44 + ]
45 + }
46 + },(error, response, body) => {
47 + console.log(body)
48 + });
49 + }
50 +
51 +
52 + res.sendStatus(200);
53 +});
54 +
55 +try {
56 + const option = {
57 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
58 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
59 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
60 + };
61 +
62 + HTTPS.createServer(option, app).listen(sslport, () => {
63 + console.log(`[HTTPS] Server is started on port ${sslport}`);
64 + });
65 + } catch (error) {
66 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
67 + console.log(error);
68 + }
69 +