Showing
1 changed file
with
53 additions
and
39 deletions
... | @@ -7,46 +7,14 @@ const path = require('path'); | ... | @@ -7,46 +7,14 @@ const path = require('path'); |
7 | const HTTPS = require('https'); | 7 | const HTTPS = require('https'); |
8 | const domain = "2019102226.osschatbot2022.ml" | 8 | const domain = "2019102226.osschatbot2022.ml" |
9 | const sslport = 23023; | 9 | const sslport = 23023; |
10 | - | ||
11 | - | ||
12 | - | ||
13 | const bodyParser = require('body-parser'); | 10 | const bodyParser = require('body-parser'); |
14 | var app = express(); | 11 | var app = express(); |
15 | app.use(bodyParser.json()); | 12 | app.use(bodyParser.json()); |
16 | app.post('/hook', function (req, res) { | 13 | app.post('/hook', function (req, res) { |
17 | - | ||
18 | var eventObj = req.body.events[0]; | 14 | var eventObj = req.body.events[0]; |
19 | - var source = eventObj.source; | ||
20 | var message = eventObj.message; | 15 | var message = eventObj.message; |
21 | - | 16 | + function output_message(results, list) { |
22 | - // request log | 17 | + var num = list[Math.floor(Math.random() * list.length)]; |
23 | - console.log('======================', new Date(), '======================'); | ||
24 | - // mwsql | ||
25 | - var mysql = require('mysql'); | ||
26 | - var db = mysql.createConnection({ | ||
27 | - host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', | ||
28 | - user: 'chatbot', | ||
29 | - password: '', | ||
30 | - database: 'chatbot', | ||
31 | - port: '3306' | ||
32 | - }); | ||
33 | - | ||
34 | - db.connect(); | ||
35 | - | ||
36 | - db.query('SELECT * FROM data', function (error, results, fields) { | ||
37 | - if (error) { | ||
38 | - console.log(error); | ||
39 | - } | ||
40 | - var arr = new Array(); | ||
41 | - var input_ingredients_list = message.text.split(" "); | ||
42 | - for (var i = 0; i < results.length; i++) { | ||
43 | - var ingredients_list = results[i].ingredients.split(", "); | ||
44 | - if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) { | ||
45 | - arr.push(i); | ||
46 | - } | ||
47 | - } | ||
48 | - if (arr.length != 0) { | ||
49 | - var num = arr[Math.floor(Math.random() * arr.length)]; | ||
50 | request.post( | 18 | request.post( |
51 | { | 19 | { |
52 | url: TARGET_URL, | 20 | url: TARGET_URL, |
... | @@ -62,7 +30,7 @@ app.post('/hook', function (req, res) { | ... | @@ -62,7 +30,7 @@ app.post('/hook', function (req, res) { |
62 | }, | 30 | }, |
63 | { | 31 | { |
64 | "type": "text", | 32 | "type": "text", |
65 | - "text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.` | 33 | + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` |
66 | }, | 34 | }, |
67 | { | 35 | { |
68 | "type": "text", | 36 | "type": "text", |
... | @@ -74,6 +42,47 @@ app.post('/hook', function (req, res) { | ... | @@ -74,6 +42,47 @@ app.post('/hook', function (req, res) { |
74 | console.log(body) | 42 | console.log(body) |
75 | }); | 43 | }); |
76 | } | 44 | } |
45 | + // request log | ||
46 | + console.log('======================', new Date(), '======================'); | ||
47 | + // mwsql | ||
48 | + var mysql = require('mysql'); | ||
49 | + var db = mysql.createConnection({ | ||
50 | + host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', | ||
51 | + user: 'chatbot', | ||
52 | + password: '', | ||
53 | + database: 'chatbot', | ||
54 | + port: '3306' | ||
55 | + }); | ||
56 | + db.connect(); | ||
57 | + var max = 0; | ||
58 | + var arr2; | ||
59 | + db.query('SELECT * FROM recipe', function (error, results, fields) { | ||
60 | + if (error) { | ||
61 | + console.log(error); | ||
62 | + } | ||
63 | + var arr1 = new Array(); | ||
64 | + var input_ingredients_list = message.text.split(" "); | ||
65 | + console.log(input_ingredients_list); | ||
66 | + for (var i = 0; i < results.length; i++) { | ||
67 | + var ingredients_list = results[i].ingrediant.split(","); | ||
68 | + var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length; | ||
69 | + if (count > max) { | ||
70 | + max = count; | ||
71 | + arr2 = new Array(); | ||
72 | + } | ||
73 | + if (count === input_ingredients_list.length) { | ||
74 | + arr1.push(i); | ||
75 | + } | ||
76 | + else if (arr1.length === 0 && count != 0 && count === max) { | ||
77 | + arr2.push(i); | ||
78 | + } | ||
79 | + } | ||
80 | + if (arr1.length != 0) { | ||
81 | + output_message(results, arr1); | ||
82 | + } | ||
83 | + else if (arr2 != undefined) { | ||
84 | + output_message(results, arr2); | ||
85 | + } | ||
77 | else { | 86 | else { |
78 | request.post( | 87 | request.post( |
79 | { | 88 | { |
... | @@ -87,6 +96,15 @@ app.post('/hook', function (req, res) { | ... | @@ -87,6 +96,15 @@ app.post('/hook', function (req, res) { |
87 | { | 96 | { |
88 | "type": "text", | 97 | "type": "text", |
89 | "text": `재료를 다시 입력해주세요.` | 98 | "text": `재료를 다시 입력해주세요.` |
99 | + }, | ||
100 | + { | ||
101 | + "type": "text", | ||
102 | + "text": `여러 재료를 입력시\n띄어쓰기로만 구분을 해주세요.` | ||
103 | + } | ||
104 | + , | ||
105 | + { | ||
106 | + "type": "text", | ||
107 | + "text": `예시) 감자 양파 대파 (o)\n\n 감자, 양파,대파 (x)` | ||
90 | } | 108 | } |
91 | ] | 109 | ] |
92 | } | 110 | } |
... | @@ -95,18 +113,15 @@ app.post('/hook', function (req, res) { | ... | @@ -95,18 +113,15 @@ app.post('/hook', function (req, res) { |
95 | }); | 113 | }); |
96 | } | 114 | } |
97 | }); | 115 | }); |
98 | - | ||
99 | db.end(); | 116 | db.end(); |
100 | res.sendStatus(200); | 117 | res.sendStatus(200); |
101 | }); | 118 | }); |
102 | - | ||
103 | try { | 119 | try { |
104 | const option = { | 120 | const option = { |
105 | ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | 121 | ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), |
106 | key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | 122 | key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), |
107 | cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | 123 | cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), |
108 | }; | 124 | }; |
109 | - | ||
110 | HTTPS.createServer(option, app).listen(sslport, () => { | 125 | HTTPS.createServer(option, app).listen(sslport, () => { |
111 | console.log(`[HTTPS] Server is started on port ${sslport}`); | 126 | console.log(`[HTTPS] Server is started on port ${sslport}`); |
112 | }); | 127 | }); |
... | @@ -114,4 +129,3 @@ try { | ... | @@ -114,4 +129,3 @@ try { |
114 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 129 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
115 | console.log(error); | 130 | console.log(error); |
116 | } | 131 | } |
... | \ No newline at end of file | ... | \ No newline at end of file |
117 | - | ... | ... |
-
Please register or login to post a comment