전언석

EDIT : text message

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 = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU=' 4 +const TOKEN = ''
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');
...@@ -13,35 +13,7 @@ app.use(bodyParser.json()); ...@@ -13,35 +13,7 @@ app.use(bodyParser.json());
13 app.post('/hook', function (req, res) { 13 app.post('/hook', function (req, res) {
14 var eventObj = req.body.events[0]; 14 var eventObj = req.body.events[0];
15 var message = eventObj.message; 15 var message = eventObj.message;
16 - function output_message(results, list) { 16 +
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 - }
45 // request log 17 // request log
46 console.log('======================', new Date(), '======================'); 18 console.log('======================', new Date(), '======================');
47 // mwsql 19 // mwsql
...@@ -49,7 +21,7 @@ app.post('/hook', function (req, res) { ...@@ -49,7 +21,7 @@ app.post('/hook', function (req, res) {
49 var db = mysql.createConnection({ 21 var db = mysql.createConnection({
50 host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', 22 host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com',
51 user: 'chatbot', 23 user: 'chatbot',
52 - password: '11111111', 24 + password: '',
53 database: 'chatbot', 25 database: 'chatbot',
54 port: '3306' 26 port: '3306'
55 }); 27 });
...@@ -77,11 +49,91 @@ app.post('/hook', function (req, res) { ...@@ -77,11 +49,91 @@ app.post('/hook', function (req, res) {
77 arr2.push(i); 49 arr2.push(i);
78 } 50 }
79 } 51 }
52 +
80 if (arr1.length != 0) { 53 if (arr1.length != 0) {
81 - output_message(results, arr1); 54 +
55 + for (var k = 0; k < arr1.length; k++) {
56 + var issamevalue = false;
57 + var ingredients_list = results[arr1[k]].ingrediant.split(",");
58 + for (var i = 0; i < ingredients_list.length; i++) {
59 + const currelm = ingredients_list[i];
60 +
61 + for (var j = i + 1; j < ingredients_list.length; j++) {
62 + if (currelm === ingredients_list[j]) {
63 + issamevalue = true;
64 + break;
65 + }
66 + }
67 + if (issamevalue) {
68 + arr2.push(arr1[k]);
69 + arr1.splice(k, 1);
70 + k--;
71 + break;
72 + }
73 + }
74 + }
75 + }
76 + if (arr1.length != 0) {
77 + var num = arr1[Math.floor(Math.random() * arr1.length)];
78 + request.post(
79 + {
80 + url: TARGET_URL,
81 + headers: {
82 + 'Authorization': `Bearer ${TOKEN}`
83 + },
84 + json: {
85 + "replyToken": eventObj.replyToken,
86 + "messages": [
87 + {
88 + "type": "text",
89 + "text": `메뉴는 "${results[[num]].menu}" 입니다.`
90 + },
91 + {
92 + "type": "text",
93 + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
94 + },
95 + {
96 + "type": "text",
97 + "text": `레시피\n\n${results[[num]].recipe}`
98 + }
99 + ]
100 + }
101 + }, (error, response, body) => {
102 + console.log(body)
103 + });
82 } 104 }
83 else if (arr2 != undefined) { 105 else if (arr2 != undefined) {
84 - output_message(results, arr2); 106 + var num = arr2[Math.floor(Math.random() * arr2.length)];
107 + request.post(
108 + {
109 + url: TARGET_URL,
110 + headers: {
111 + 'Authorization': `Bearer ${TOKEN}`
112 + },
113 + json: {
114 + "replyToken": eventObj.replyToken,
115 + "messages": [
116 + {
117 + "type": "text",
118 + "text": `입력하신 재료를 모두 포함하는 레시피를 찾지 못하였습니다.\n\n입력하신 재료를 최대한 많이 포함하는 레시피를 보여드리겠습니다.`
119 + },
120 + {
121 + "type": "text",
122 + "text": `메뉴는 "${results[[num]].menu}" 입니다.`
123 + },
124 + {
125 + "type": "text",
126 + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
127 + },
128 + {
129 + "type": "text",
130 + "text": `레시피\n\n${results[[num]].recipe}`
131 + }
132 + ]
133 + }
134 + }, (error, response, body) => {
135 + console.log(body)
136 + });
85 } 137 }
86 else { 138 else {
87 request.post( 139 request.post(
......