Showing
1 changed file
with
107 additions
and
0 deletions
chatbot.js
0 → 100644
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 = 'ymSHuQ8buLQKs3fgTQcUIMVHfnsGgZEbeTsLIzfRRrwKbZ5Kh0FvfqPmMLQ1EWYLgRQaX8ejC2ir3aaNTTLQIPGM8o3Q8GoSE0V4FiedWNT1t198m84paR5Q3lKPTzCm/SjPLpXJpf0T7u3+c0TkBgdB04t89/1O/w1cDnyilFU=' | ||
5 | +const fs = require('fs'); | ||
6 | +const path = require('path'); | ||
7 | +const HTTPS = require('https'); | ||
8 | +const domain = "2019102146.osschatbot2022.tk" | ||
9 | +const sslport = 23023; | ||
10 | + | ||
11 | +const bodyParser = require('body-parser'); | ||
12 | +var app = express(); | ||
13 | +app.use(bodyParser.json()); | ||
14 | + | ||
15 | + | ||
16 | +ID_data_array = [] | ||
17 | + | ||
18 | +app.post('/hook', function (req, res) { | ||
19 | + | ||
20 | + var eventObj = req.body.events[0]; | ||
21 | + var source = eventObj.source; | ||
22 | + var message = eventObj.message; | ||
23 | + | ||
24 | + var user_ID = eventObj.source['userId'] | ||
25 | + var data = eventObj.message['text'] | ||
26 | + if (ID_data_array.length == 0) { | ||
27 | + ID_data_array.push([user_ID]) | ||
28 | + ID_data_array[0].push(data) | ||
29 | + } | ||
30 | + else { | ||
31 | + var find = 0 | ||
32 | + for (var i in ID_data_array) { | ||
33 | + if (ID_data_array[i][0] == user_ID) { | ||
34 | + find = 1 | ||
35 | + if (ID_data_array[i].length < 4) { | ||
36 | + ID_data_array[i].push(data) | ||
37 | + } | ||
38 | + else { | ||
39 | + ID_data_array[i] = [user_ID]; | ||
40 | + ID_data_array[i].push(data); | ||
41 | + } | ||
42 | + } | ||
43 | + } | ||
44 | + if (find == 0) { | ||
45 | + ID_data_array.push([user_ID]); | ||
46 | + ID_data_array[ID_data_array.length-1].push(data); | ||
47 | + } | ||
48 | + } | ||
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 | + console.log(ID_data_array) | ||
57 | + for (var i in ID_data_array) { | ||
58 | + console.log(ID_data_array[i]) | ||
59 | + } | ||
60 | + | ||
61 | + | ||
62 | + request.post( | ||
63 | + { | ||
64 | + url: TARGET_URL, | ||
65 | + headers: { | ||
66 | + 'Authorization': `Bearer ${TOKEN}` | ||
67 | + }, | ||
68 | + json: { | ||
69 | + "replyToken":eventObj.replyToken, | ||
70 | + "messages":[ | ||
71 | + { | ||
72 | + "type":"text", | ||
73 | + "text":"Hello, user" | ||
74 | + }, | ||
75 | + { | ||
76 | + "type":"text", | ||
77 | + "text":"May I help you?" | ||
78 | + } | ||
79 | + ] | ||
80 | + } | ||
81 | + },(error, response, body) => { | ||
82 | + console.log(body) | ||
83 | + }); | ||
84 | + | ||
85 | + | ||
86 | + res.sendStatus(200); | ||
87 | + | ||
88 | + if (data.length >= 3) { | ||
89 | + data = [] | ||
90 | + } | ||
91 | +}); | ||
92 | + | ||
93 | + | ||
94 | +try { | ||
95 | + const option = { | ||
96 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
97 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
98 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
99 | + }; | ||
100 | + | ||
101 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
102 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
103 | + }); | ||
104 | + } catch (error) { | ||
105 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
106 | + console.log(error); | ||
107 | + } |
-
Please register or login to post a comment