오세헌

making output_message function

......@@ -19,6 +19,36 @@ app.post('/hook', function (req, res) {
var source = eventObj.source;
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
......@@ -56,63 +86,13 @@ app.post('/hook', function (req, res) {
arr2.push(i);
}
}
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)
});
output_message(results, arr1);
}
else if (arr2.length != 0) {
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": `메뉴는 "${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)
});
output_message(results, arr2);
}
else {
request.post(
......@@ -134,9 +114,11 @@ app.post('/hook', function (req, res) {
console.log(body)
});
}
});
db.end();
res.sendStatus(200);
});
......