taegwon

app.js almost final

var end = false;
var selected_food = '';
var located = '';
var count = 0;
var real_data = '';
var directory = [['food_division_txt_file/korean_food_final.txt','food_division_txt_file/chinese_food_final.txt','food_division_txt_file/japanese_food_final.txt','food_division_txt_file/western_food_final.txt','food_division_txt_file/etc_food_final.txt'],['food_division_txt_file/meat.txt','food_division_txt_file/fish.txt','food_division_txt_file/ect.txt','\0','\0'],['food_division_txt_file/spicy_food_final.txt','food_division_txt_file/not_spicy_food_final.txt','\0','\0','\0'],['food_division_txt_file/water.txt','food_division_txt_file/dry.txt','\0','\0','\0'],['food_division_txt_file/cold_food_final.txt','food_division_txt_file/hot_food_final.txt','\0','\0','\0']];
......@@ -9,8 +11,8 @@ const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU='
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_ID = '7S7IVuHMvSgjpygsJ0uN'
const PAPAGO_SECRET = 'WpWAr55nkz'
const PAPAGO_ID = '1Z83W3lUGLqnNbrDAk8A'
const PAPAGO_SECRET = 'oDlgYoi4LO'
var fs = require('fs');
const path = require('path');
const HTTPS = require('https');
......@@ -20,7 +22,7 @@ const bodyParser = require('body-parser');
const res = require('express/lib/response');
const { eventNames } = require('process');
const req = require('express/lib/request');
const { compileFunction } = require('vm');
const { getEnabledCategories } = require('trace_events');
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
......@@ -39,8 +41,16 @@ app.post('/hook', function (req, res) {
count++;
start_msg(eventObj.replyToken, eventObj.message.text);
}else if(eventObj.message.text === 'y' && count ==1)
}else if((eventObj.message.text === 'y' || eventObj.message.text == 'n' )&& count ==1)
{
if(eventObj.message.text == 'n')
{
async function ending () {
await end_of_chatbot(eventObj.replyToken);
setTimeout(process.exit(1), 5 * 1000);
}
ending();
}
count++;
opt_1(eventObj.replyToken, eventObj.message.text);
}else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.text == '5') && count ==2)
......@@ -68,39 +78,71 @@ app.post('/hook', function (req, res) {
choosen.push(eventObj.message.text);
count++;
var txt = intersection();
Rmx_comment(eventObj.replyToken);
}else if(count == 7 && end)
Rmx_1(eventObj.replyToken,eventObj.message.text);
}else if(count == 7 && (eventObj.message.text == 'y' || eventObj.message.text == 'n') && end)
{
async function read () {
await fs.readFile(dir, 'utf8', (err,data) => {
if(err) {
console.error(err);
return 'err';
if(eventObj.message.text == 'n')
{
async function ending () {
await end_of_chatbot(eventObj.replyToken);
setTimeout(process.exit(1), 5 * 1000);
}
ending();
}
async function read () {
await fs.readFile('fin.txt', 'utf8', async function (err,data) {
var ary = data.toString().split("\n");
for(i in ary)
{
real_data = await real_data + ary[i] + ' ';
console.log(ary[i]);
sleep(500);
real_data =real_data + data + ' ';
console.log(typeof(data));
}
});
}
console.log('hi');
sleep(1000);
console.log(txt);
Rmx(eventObj.replyToken, txt);
read();
Rmx_comment(eventObj.replyToken)
count++;
}else if(count ==8 && end)
{
if(real_data == ' ')
{
no(eventObj.replyToken);
}else{
Rmx(eventObj.replyToken, real_data);
}
}else if(count == 9)
count++;
}else if(count == 9 && end)
{
load_map(eventObj.replyToken);
selected_food = eventObj.message.text;
if(real_data.indexOf(selected_food) != -1)
{
loc(eventObj.replyToken);
count++;
}else{
err(eventObj.replyToken);
}
}else if(count == 10 && end)
{
located = eventObj.message.text;
load_map(eventObj.replyToken);
}else if(count == 11 && end)
{
async function ending () {
await end_of_chatbot(eventObj.replyToken);
setTimeout(process.exit(1), 5 * 1000);
}
ending();
}
else
{
err(eventObj.replyToken);
}
res.sendStatus(200);
});
......@@ -359,7 +401,44 @@ async function Rmx(replyToken,message) {
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'source=en&target=ko&text=' + message,
body: 'source=en&target=ko&text=' + "추천 음식은" + message + "입니다. 음식을 고르시오.",
json: true
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
console.log(body.message);
var transMessage = body.message.result.translatedText;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type": "text",
"text": transMessage
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
});
}
async function Rmx_1(replyToken,message) {
await request.post(
{
url: PAPAGO_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'source=en&target=ko&text=' + "추천음식을 보겠습니까? (y/n)",
json: true
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
......@@ -389,6 +468,8 @@ async function Rmx(replyToken,message) {
async function load_map(replyToken)
{
var map_txt = "https://map.naver.com/v5/search/" + located + "%20" + selected_food;
map_txt = map_txt.replace(/\s/g, "%20");
await request.post(
{
url: TARGET_URL,
......@@ -400,7 +481,51 @@ async function load_map(replyToken)
"messages": [
{
"type": "text",
"text": "https://www.daleseo.com/js-sleep/"
"text": "지도 : " + map_txt
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
async function no(replyToken)
{
await request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type": "text",
"text": "추천음식은 없습니다.\n 챗봇을 종료합니다."
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
async function loc(replyToken)
{
await request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type": "text",
"text": "현재 위치를 입력하세요."
}
]
}
......@@ -460,6 +585,28 @@ async function intersection()
return 'complete';
}
async function end_of_chatbot(replyToken)
{
await request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type": "text",
"text": "챗봇을 종료합니다. 이용해주셔서 감사합니다."
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
function sleep(ms) {
const wakeUpTime = Date.now() + ms;
while (Date.now() < wakeUpTime) {}
......