전언석

EDIT : text message

var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU='
const TOKEN = ''
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
......@@ -13,35 +13,7 @@ app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var message = eventObj.message;
function output_message(results, list) {
var num = list[Math.floor(Math.random() * list.length)];
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": eventObj.replyToken,
"messages": [
{
"type": "text",
"text": `메뉴는 "${results[[num]].menu}" 입니다.`
},
{
"type": "text",
"text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
},
{
"type": "text",
"text": `레시피\n\n${results[[num]].recipe}`
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
// request log
console.log('======================', new Date(), '======================');
// mwsql
......@@ -49,7 +21,7 @@ app.post('/hook', function (req, res) {
var db = mysql.createConnection({
host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com',
user: 'chatbot',
password: '11111111',
password: '',
database: 'chatbot',
port: '3306'
});
......@@ -77,11 +49,91 @@ app.post('/hook', function (req, res) {
arr2.push(i);
}
}
if (arr1.length != 0) {
output_message(results, arr1);
for (var k = 0; k < arr1.length; k++) {
var issamevalue = false;
var ingredients_list = results[arr1[k]].ingrediant.split(",");
for (var i = 0; i < ingredients_list.length; i++) {
const currelm = ingredients_list[i];
for (var j = i + 1; j < ingredients_list.length; j++) {
if (currelm === ingredients_list[j]) {
issamevalue = true;
break;
}
}
if (issamevalue) {
arr2.push(arr1[k]);
arr1.splice(k, 1);
k--;
break;
}
}
}
}
if (arr1.length != 0) {
var num = arr1[Math.floor(Math.random() * arr1.length)];
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": eventObj.replyToken,
"messages": [
{
"type": "text",
"text": `메뉴는 "${results[[num]].menu}" 입니다.`
},
{
"type": "text",
"text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
},
{
"type": "text",
"text": `레시피\n\n${results[[num]].recipe}`
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
else if (arr2 != undefined) {
output_message(results, arr2);
var num = arr2[Math.floor(Math.random() * arr2.length)];
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": eventObj.replyToken,
"messages": [
{
"type": "text",
"text": `입력하신 재료를 모두 포함하는 레시피를 찾지 못하였습니다.\n\n입력하신 재료를 최대한 많이 포함하는 레시피를 보여드리겠습니다.`
},
{
"type": "text",
"text": `메뉴는 "${results[[num]].menu}" 입니다.`
},
{
"type": "text",
"text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
},
{
"type": "text",
"text": `레시피\n\n${results[[num]].recipe}`
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
else {
request.post(
......