Showing
1 changed file
with
57 additions
and
43 deletions
... | @@ -7,18 +7,41 @@ const path = require('path'); | ... | @@ -7,18 +7,41 @@ 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) { |
17 | + var num = list[Math.floor(Math.random() * list.length)]; | ||
18 | + request.post( | ||
19 | + { | ||
20 | + url: TARGET_URL, | ||
21 | + headers: { | ||
22 | + 'Authorization': `Bearer ${TOKEN}` | ||
23 | + }, | ||
24 | + json: { | ||
25 | + "replyToken": eventObj.replyToken, | ||
26 | + "messages": [ | ||
27 | + { | ||
28 | + "type": "text", | ||
29 | + "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
30 | + }, | ||
31 | + { | ||
32 | + "type": "text", | ||
33 | + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` | ||
34 | + }, | ||
35 | + { | ||
36 | + "type": "text", | ||
37 | + "text": `레시피\n\n${results[[num]].recipe}` | ||
38 | + } | ||
39 | + ] | ||
40 | + } | ||
41 | + }, (error, response, body) => { | ||
42 | + console.log(body) | ||
43 | + }); | ||
44 | + } | ||
22 | // request log | 45 | // request log |
23 | console.log('======================', new Date(), '======================'); | 46 | console.log('======================', new Date(), '======================'); |
24 | // mwsql | 47 | // mwsql |
... | @@ -30,23 +53,37 @@ app.post('/hook', function (req, res) { | ... | @@ -30,23 +53,37 @@ app.post('/hook', function (req, res) { |
30 | database: 'chatbot', | 53 | database: 'chatbot', |
31 | port: '3306' | 54 | port: '3306' |
32 | }); | 55 | }); |
33 | - | ||
34 | db.connect(); | 56 | db.connect(); |
35 | - | 57 | + var max = 0; |
36 | - db.query('SELECT * FROM data', function (error, results, fields) { | 58 | + var arr2; |
59 | + db.query('SELECT * FROM recipe', function (error, results, fields) { | ||
37 | if (error) { | 60 | if (error) { |
38 | console.log(error); | 61 | console.log(error); |
39 | } | 62 | } |
40 | - var arr = new Array(); | 63 | + var arr1 = new Array(); |
41 | var input_ingredients_list = message.text.split(" "); | 64 | var input_ingredients_list = message.text.split(" "); |
65 | + console.log(input_ingredients_list); | ||
42 | for (var i = 0; i < results.length; i++) { | 66 | for (var i = 0; i < results.length; i++) { |
43 | - var ingredients_list = results[i].ingredients.split(", "); | 67 | + var ingredients_list = results[i].ingrediant.split(","); |
44 | - if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) { | 68 | + var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length; |
45 | - arr.push(i); | 69 | + if (count > max) { |
70 | + max = count; | ||
71 | + arr2 = new Array(); | ||
46 | } | 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); | ||
47 | } | 85 | } |
48 | - if (arr.length != 0) { | 86 | + else { |
49 | - var num = arr[Math.floor(Math.random() * arr.length)]; | ||
50 | request.post( | 87 | request.post( |
51 | { | 88 | { |
52 | url: TARGET_URL, | 89 | url: TARGET_URL, |
... | @@ -58,35 +95,16 @@ app.post('/hook', function (req, res) { | ... | @@ -58,35 +95,16 @@ app.post('/hook', function (req, res) { |
58 | "messages": [ | 95 | "messages": [ |
59 | { | 96 | { |
60 | "type": "text", | 97 | "type": "text", |
61 | - "text": `메뉴는 "${results[[num]].menu}" 입니다.` | 98 | + "text": `재료를 다시 입력해주세요.` |
62 | - }, | ||
63 | - { | ||
64 | - "type": "text", | ||
65 | - "text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.` | ||
66 | }, | 99 | }, |
67 | { | 100 | { |
68 | "type": "text", | 101 | "type": "text", |
69 | - "text": `레시피\n\n${results[[num]].recipe}` | 102 | + "text": `여러 재료를 입력시\n띄어쓰기로만 구분을 해주세요.` |
70 | } | 103 | } |
71 | - ] | 104 | + , |
72 | - } | ||
73 | - }, (error, response, body) => { | ||
74 | - console.log(body) | ||
75 | - }); | ||
76 | - } | ||
77 | - else { | ||
78 | - request.post( | ||
79 | - { | ||
80 | - url: TARGET_URL, | ||
81 | - headers: { | ||
82 | - 'Authorization': `Bearer ${TOKEN}` | ||
83 | - }, | ||
84 | - json: { | ||
85 | - "replyToken": eventObj.replyToken, | ||
86 | - "messages": [ | ||
87 | { | 105 | { |
88 | "type": "text", | 106 | "type": "text", |
89 | - "text": `재료를 다시 입력해주세요.` | 107 | + "text": `예시) 감자 양파 대파 (o)\n\n 감자, 양파,대파 (x)` |
90 | } | 108 | } |
91 | ] | 109 | ] |
92 | } | 110 | } |
... | @@ -95,23 +113,19 @@ app.post('/hook', function (req, res) { | ... | @@ -95,23 +113,19 @@ 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 | }); |
113 | } catch (error) { | 128 | } catch (error) { |
114 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 129 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
115 | console.log(error); | 130 | console.log(error); |
116 | -} | 131 | +} |
117 | - | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment