Merge branch 'ohsehun' into 'master'
Ohsehun See merge request !8
Showing
1 changed file
with
58 additions
and
37 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | const request = require('request'); | 2 | const request = require('request'); |
3 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 3 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
4 | -const TOKEN = '' | 4 | +const TOKEN = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU=' |
5 | const fs = require('fs'); | 5 | const fs = require('fs'); |
6 | const path = require('path'); | 6 | const path = require('path'); |
7 | const HTTPS = require('https'); | 7 | const HTTPS = require('https'); |
... | @@ -19,6 +19,36 @@ app.post('/hook', function (req, res) { | ... | @@ -19,6 +19,36 @@ app.post('/hook', function (req, res) { |
19 | var source = eventObj.source; | 19 | var source = eventObj.source; |
20 | var message = eventObj.message; | 20 | var message = eventObj.message; |
21 | 21 | ||
22 | + function output_message(results, list) { | ||
23 | + var num = list[Math.floor(Math.random() * list.length)]; | ||
24 | + request.post( | ||
25 | + { | ||
26 | + url: TARGET_URL, | ||
27 | + headers: { | ||
28 | + 'Authorization': `Bearer ${TOKEN}` | ||
29 | + }, | ||
30 | + json: { | ||
31 | + "replyToken": eventObj.replyToken, | ||
32 | + "messages": [ | ||
33 | + { | ||
34 | + "type": "text", | ||
35 | + "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
36 | + }, | ||
37 | + { | ||
38 | + "type": "text", | ||
39 | + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.` | ||
40 | + }, | ||
41 | + { | ||
42 | + "type": "text", | ||
43 | + "text": `레시피\n\n${results[[num]].recipe}` | ||
44 | + } | ||
45 | + ] | ||
46 | + } | ||
47 | + }, (error, response, body) => { | ||
48 | + console.log(body) | ||
49 | + }); | ||
50 | + } | ||
51 | + | ||
22 | // request log | 52 | // request log |
23 | console.log('======================', new Date(), '======================'); | 53 | console.log('======================', new Date(), '======================'); |
24 | // mwsql | 54 | // mwsql |
... | @@ -26,53 +56,43 @@ app.post('/hook', function (req, res) { | ... | @@ -26,53 +56,43 @@ app.post('/hook', function (req, res) { |
26 | var db = mysql.createConnection({ | 56 | var db = mysql.createConnection({ |
27 | host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', | 57 | host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', |
28 | user: 'chatbot', | 58 | user: 'chatbot', |
29 | - password: '', | 59 | + password: '11111111', |
30 | database: 'chatbot', | 60 | database: 'chatbot', |
31 | port: '3306' | 61 | port: '3306' |
32 | }); | 62 | }); |
33 | 63 | ||
34 | db.connect(); | 64 | db.connect(); |
35 | 65 | ||
36 | - db.query('SELECT * FROM data', function (error, results, fields) { | 66 | + var max = 0; |
67 | + | ||
68 | + db.query('SELECT * FROM recipe', function (error, results, fields) { | ||
37 | if (error) { | 69 | if (error) { |
38 | console.log(error); | 70 | console.log(error); |
39 | } | 71 | } |
40 | - var arr = new Array(); | 72 | + var arr1 = new Array(); |
41 | var input_ingredients_list = message.text.split(" "); | 73 | var input_ingredients_list = message.text.split(" "); |
74 | + console.log(input_ingredients_list); | ||
42 | for (var i = 0; i < results.length; i++) { | 75 | for (var i = 0; i < results.length; i++) { |
43 | - var ingredients_list = results[i].ingredients.split(", "); | 76 | + var ingredients_list = results[i].ingrediant.split(","); |
44 | - if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) { | 77 | + var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length; |
45 | - arr.push(i); | 78 | + if (count > max) { |
79 | + max = count; | ||
80 | + var arr2 = new Array(); | ||
81 | + } | ||
82 | + if (count === input_ingredients_list.length) { | ||
83 | + arr1.push(i); | ||
84 | + } | ||
85 | + else if (arr1.length === 0 && count != 0 && count === max) { | ||
86 | + arr2.push(i); | ||
46 | } | 87 | } |
47 | } | 88 | } |
48 | - if (arr.length != 0) { | 89 | + |
49 | - var num = arr[Math.floor(Math.random() * arr.length)]; | 90 | + if (arr1.length != 0) { |
50 | - request.post( | 91 | + output_message(results, arr1); |
51 | - { | 92 | + } |
52 | - url: TARGET_URL, | 93 | + |
53 | - headers: { | 94 | + else if (arr2.length != 0) { |
54 | - 'Authorization': `Bearer ${TOKEN}` | 95 | + output_message(results, arr2); |
55 | - }, | ||
56 | - json: { | ||
57 | - "replyToken": eventObj.replyToken, | ||
58 | - "messages": [ | ||
59 | - { | ||
60 | - "type": "text", | ||
61 | - "text": `메뉴는 "${results[[num]].menu}" 입니다.` | ||
62 | - }, | ||
63 | - { | ||
64 | - "type": "text", | ||
65 | - "text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.` | ||
66 | - }, | ||
67 | - { | ||
68 | - "type": "text", | ||
69 | - "text": `레시피\n\n${results[[num]].recipe}` | ||
70 | - } | ||
71 | - ] | ||
72 | - } | ||
73 | - }, (error, response, body) => { | ||
74 | - console.log(body) | ||
75 | - }); | ||
76 | } | 96 | } |
77 | else { | 97 | else { |
78 | request.post( | 98 | request.post( |
... | @@ -94,9 +114,11 @@ app.post('/hook', function (req, res) { | ... | @@ -94,9 +114,11 @@ app.post('/hook', function (req, res) { |
94 | console.log(body) | 114 | console.log(body) |
95 | }); | 115 | }); |
96 | } | 116 | } |
117 | + | ||
97 | }); | 118 | }); |
98 | 119 | ||
99 | db.end(); | 120 | db.end(); |
121 | + | ||
100 | res.sendStatus(200); | 122 | res.sendStatus(200); |
101 | }); | 123 | }); |
102 | 124 | ||
... | @@ -113,5 +135,4 @@ try { | ... | @@ -113,5 +135,4 @@ try { |
113 | } catch (error) { | 135 | } catch (error) { |
114 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | 136 | console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); |
115 | console.log(error); | 137 | console.log(error); |
116 | -} | 138 | +} |
117 | - | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment