오세헌

Merge branch 'ohsehun' into 'master'

Ohsehun



See merge request !8
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,34 +19,8 @@ app.post('/hook', function (req, res) { ...@@ -19,34 +19,8 @@ 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 - // request log 22 + function output_message(results, list) {
23 - console.log('======================', new Date(), '======================'); 23 + var num = list[Math.floor(Math.random() * list.length)];
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( 24 request.post(
51 { 25 {
52 url: TARGET_URL, 26 url: TARGET_URL,
...@@ -62,7 +36,7 @@ app.post('/hook', function (req, res) { ...@@ -62,7 +36,7 @@ app.post('/hook', function (req, res) {
62 }, 36 },
63 { 37 {
64 "type": "text", 38 "type": "text",
65 - "text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.` 39 + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
66 }, 40 },
67 { 41 {
68 "type": "text", 42 "type": "text",
...@@ -74,6 +48,52 @@ app.post('/hook', function (req, res) { ...@@ -74,6 +48,52 @@ app.post('/hook', function (req, res) {
74 console.log(body) 48 console.log(body)
75 }); 49 });
76 } 50 }
51 +
52 + // request log
53 + console.log('======================', new Date(), '======================');
54 + // mwsql
55 + var mysql = require('mysql');
56 + var db = mysql.createConnection({
57 + host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com',
58 + user: 'chatbot',
59 + password: '11111111',
60 + database: 'chatbot',
61 + port: '3306'
62 + });
63 +
64 + db.connect();
65 +
66 + var max = 0;
67 +
68 + db.query('SELECT * FROM recipe', function (error, results, fields) {
69 + if (error) {
70 + console.log(error);
71 + }
72 + var arr1 = new Array();
73 + var input_ingredients_list = message.text.split(" ");
74 + console.log(input_ingredients_list);
75 + for (var i = 0; i < results.length; i++) {
76 + var ingredients_list = results[i].ingrediant.split(",");
77 + var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length;
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);
87 + }
88 + }
89 +
90 + if (arr1.length != 0) {
91 + output_message(results, arr1);
92 + }
93 +
94 + else if (arr2.length != 0) {
95 + output_message(results, arr2);
96 + }
77 else { 97 else {
78 request.post( 98 request.post(
79 { 99 {
...@@ -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
...@@ -114,4 +136,3 @@ try { ...@@ -114,4 +136,3 @@ try {
114 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 136 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
115 console.log(error); 137 console.log(error);
116 } 138 }
...\ No newline at end of file ...\ No newline at end of file
117 -
......