송민석

Modify chatbot code

Showing 1 changed file with 68 additions and 0 deletions
...@@ -27,3 +27,71 @@ const connect = mongoose.connect(config.url, ...@@ -27,3 +27,71 @@ const connect = mongoose.connect(config.url,
27 const Subinfo = mongoose.model('Subinfo', SubSchema); 27 const Subinfo = mongoose.model('Subinfo', SubSchema);
28 28
29 module.exports = {Timeinfo, Subinfo}; 29 module.exports = {Timeinfo, Subinfo};
30 +
31 + var express = require('express');
32 +const request = require('request');
33 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
34 +const TOKEN = 'KDfacj4wYOewyww1JPcvRZYDz+9UaV/mtjr9jkYiZw2Ywm0NO9R100opUwuRHMzXoTxmstALtM5a6l4Ro8nkjQBOT8HsBsARXeqRt6gaKDb1Hu7FAOmQ6kcKN8qsiwyEdKWDJnqaCB7im3QIiPGqIAdB04t89/1O/w1cDnyilFU='
35 +const fs = require('fs');
36 +const path = require('path');
37 +const HTTPS = require('https');
38 +const domain = "2019102187.osschatbot2022.ml"
39 +const sslport = 23023;
40 +
41 +const bodyParser = require('body-parser');
42 +var app = express();
43 +app.use(bodyParser.json());
44 +app.post('/hook', function (req, res) {
45 +
46 + var eventObj = req.body.events[0];
47 + var source = eventObj.source;
48 + var message = eventObj.message;
49 +
50 + // request log
51 + console.log('======================', new Date() ,'======================');
52 + console.log('[request]', req.body);
53 + console.log('[request source] ', eventObj.source);
54 + console.log('[request message]', eventObj.message);
55 +
56 + request.post(
57 + {
58 + url: TARGET_URL,
59 + headers: {
60 + 'Authorization': `Bearer ${TOKEN}`
61 + },
62 + json: {
63 + "replyToken":eventObj.replyToken,
64 + "messages":[
65 + {
66 + "type":"text",
67 + "text":"Hello, user"
68 + },
69 + {
70 + "type":"text",
71 + "text":"May I help you?"
72 + }
73 + ]
74 + }
75 + },(error, response, body) => {
76 + console.log(body)
77 + });
78 +
79 +
80 + res.sendStatus(200);
81 +});
82 +
83 +try {
84 + const option = {
85 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
86 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
87 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
88 + };
89 +
90 + HTTPS.createServer(option, app).listen(sslport, () => {
91 + console.log(`[HTTPS] Server is started on port ${sslport}`);
92 + });
93 + } catch (error) {
94 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
95 + console.log(error);
96 + }
97 +
......