taegwon

fix app.js chatbot message

1 var end = false; 1 var end = false;
2 var count = 0; 2 var count = 0;
3 +n1 = '';
4 +var directory = [['korean_food_final.txt','chinese_food_final.txt','japanese_food_final.txt','western_food_final.txt','ect_food_final.txt'],['meat.txt','fish.txt','ect.txt','\0','\0'],['spicy_food_final.txt','not_spicy_food_final.txt','\0','\0','\0'],['water.txt','dry.txt','\0','\0','\0'],['hot_food_final.txt','cold_food_final.txt','\0','\0','\0']];
5 +var save_file_name = ['fin_1.txt','fin_2.txt','fin_3.txt','fin.txt'];
3 var choosen = new Array(); 6 var choosen = new Array();
4 var express = require('express'); 7 var express = require('express');
5 const request = require('request'); 8 const request = require('request');
...@@ -37,51 +40,52 @@ app.post('/hook', function (req, res) { ...@@ -37,51 +40,52 @@ app.post('/hook', function (req, res) {
37 start_msg(eventObj.replyToken, eventObj.message.text); 40 start_msg(eventObj.replyToken, eventObj.message.text);
38 }else if(eventObj.message.text === 'y' && count ==1) 41 }else if(eventObj.message.text === 'y' && count ==1)
39 { 42 {
43 + count++;
40 opt_1(eventObj.replyToken, eventObj.message.text); 44 opt_1(eventObj.replyToken, eventObj.message.text);
41 - }else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.texts == '5') && count ==1) 45 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.texts == '5') && count ==2)
42 { 46 {
43 choosen.push(eventObj.message.text); 47 choosen.push(eventObj.message.text);
44 count++; 48 count++;
45 opt_2(eventObj.replyToken, eventObj.message.text); 49 opt_2(eventObj.replyToken, eventObj.message.text);
46 - }else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3') && count ==2) 50 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3') && count ==3)
47 { 51 {
48 choosen.push(eventObj.message.text); 52 choosen.push(eventObj.message.text);
49 count++; 53 count++;
50 opt_3(eventObj.replyToken, eventObj.message.text); 54 opt_3(eventObj.replyToken, eventObj.message.text);
51 - }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==3) 55 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==4)
52 { 56 {
53 choosen.push(eventObj.message.text); 57 choosen.push(eventObj.message.text);
54 count++; 58 count++;
55 opt_4(eventObj.replyToken, eventObj.message.text); 59 opt_4(eventObj.replyToken, eventObj.message.text);
56 - }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==4) 60 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==5)
57 { 61 {
58 choosen.push(eventObj.message.text); 62 choosen.push(eventObj.message.text);
59 count++; 63 count++;
60 opt_5(eventObj.replyToken, eventObj.message.text); 64 opt_5(eventObj.replyToken, eventObj.message.text);
61 - }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==5) 65 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2') && count ==6)
62 { 66 {
63 choosen.push(eventObj.message.text); 67 choosen.push(eventObj.message.text);
64 count++; 68 count++;
65 Rmx_comment(eventObj.replyToken); 69 Rmx_comment(eventObj.replyToken);
66 - }else if(count == 6 && end) 70 + }else if(count == 7 && end)
67 { 71 {
68 - Rmx(eventObj.replyToken); 72 + intersection();
73 + read_file('fin.txt')
74 + Rmx(eventObj.replyToken,n1);
69 count++; 75 count++;
70 - }else if(count ==7 && end && eventObj.message.text == 'y') 76 + }else if(count ==8 && end)
77 + {
78 +
79 +
80 +
81 + }else if(count == 9)
71 { 82 {
72 load_map(eventObj.replyToken); 83 load_map(eventObj.replyToken);
73 count++; 84 count++;
74 }else{ 85 }else{
75 err(eventObj.replyToken); 86 err(eventObj.replyToken);
76 } 87 }
77 - const spawn = require('child_process').spawn; 88 +
78 - const result = spawn('python', ['/home/ec2-user/menu_recommand_webpage-1/CHATBOT/fileread_practice.py', '/home/ec2-user/menu_recommand_webpage-1/CHATBOT/1_person.txt', '/home/ec2-user/menu_recommand_webpage-1/CHATBOT/water.txt']);
79 - result.stdout.on('data', function (data) {
80 - console.log(data.toString());
81 - });
82 - result.stderr.on('data', function (data) {
83 - console.log(data.toString());
84 - });
85 89
86 res.sendStatus(200); 90 res.sendStatus(200);
87 }); 91 });
...@@ -322,7 +326,7 @@ async function Rmx_comment(replyToken) ...@@ -322,7 +326,7 @@ async function Rmx_comment(replyToken)
322 "messages": [ 326 "messages": [
323 { 327 {
324 "type": "text", 328 "type": "text",
325 - "text": "계속하려면 아무키나 입력하시오." 329 + "text": "아무키나 입력해 추천 음식 보기."
326 } 330 }
327 ] 331 ]
328 } 332 }
...@@ -331,25 +335,40 @@ async function Rmx_comment(replyToken) ...@@ -331,25 +335,40 @@ async function Rmx_comment(replyToken)
331 }); 335 });
332 } 336 }
333 337
334 -async function Rmx(replyToken) 338 +async function Rmx(replyToken,message) {
335 -{
336 await request.post( 339 await request.post(
337 { 340 {
338 - url: TARGET_URL, 341 + url: PAPAGO_URL,
339 headers: { 342 headers: {
340 - 'Authorization': `Bearer ${TOKEN}` 343 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
344 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
345 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
341 }, 346 },
342 - json: { 347 + body: 'source=en&target=ko&text=' + message,
343 - "replyToken": replyToken, 348 + json: true
344 - "messages": [ 349 + }, (error, response, body) => {
350 + if (!error && response.statusCode == 200) {
351 + console.log(body.message);
352 + var transMessage = body.message.result.translatedText;
353 + request.post(
345 { 354 {
346 - "type": "text", 355 + url: TARGET_URL,
347 - "text": "추천 음식은 ~~~~ 입니다.\n 음식점을 추천 받으시겠습니까? (y/n)" 356 + headers: {
348 - } 357 + 'Authorization': `Bearer ${TOKEN}`
349 - ] 358 + },
359 + json: {
360 + "replyToken": replyToken,
361 + "messages": [
362 + {
363 + "type": "text",
364 + "text": transMessage
365 + }
366 + ]
367 + }
368 + }, (error, response, body) => {
369 + console.log(body)
370 + });
350 } 371 }
351 - }, (error, response, body) => {
352 - console.log(body)
353 }); 372 });
354 } 373 }
355 374
...@@ -397,16 +416,43 @@ async function err(replyToken) ...@@ -397,16 +416,43 @@ async function err(replyToken)
397 }); 416 });
398 } 417 }
399 418
400 -async function read_file(dir, arr) 419 +async function read_file(dir)
401 { 420 {
402 await fs.readFile(dir, function(err, data) { 421 await fs.readFile(dir, function(err, data) {
403 if(err) throw err; 422 if(err) throw err;
404 - arr = data.toString().split("\n"); 423 + n1 = data.toString().split("\n");
405 - n1=arr;
406 }); 424 });
407 425
408 } 426 }
409 427
428 +async function intersection()
429 +{
430 + const spawn = await require('child_process').spawn;
431 + for (var i = 0, j = 0; i < 3; i += 2, j++) {
432 + const result = spawn('python', ['/home/ec2-user/menu_recommand_webpage-1/CHATBOT/fileread_practice.py', directory[i][Number(choosen[i]) - 1], directory[i + 1][Number(choosen[i + 1]) - 1], save_file_name[j]]);
433 + result.stdout.on('data', function (data) {
434 + console.log(data.toString());
435 + });
436 + result.stderr.on('data', function (data) {
437 + console.log(data.toString());
438 + });
439 + }
440 + result = spawn('python', ['/home/ec2-user/menu_recommand_webpage-1/CHATBOT/fileread_practice.py', save_file_name[0], save_file_name[1], save_file_name[2]]);
441 + result.stdout.on('data', function (data) {
442 + console.log(data.toString());
443 + });
444 + result.stderr.on('data', function (data) {
445 + console.log(data.toString());
446 + });
447 + result = spawn('python', ['/home/ec2-user/menu_recommand_webpage-1/CHATBOT/fileread_practice.py', save_file_name[2], directory[4][Number(choosen[4]) - 1], save_file_name[3]]);
448 + result.stdout.on('data', function (data) {
449 + console.log(data.toString());
450 + });
451 + result.stderr.on('data', function (data) {
452 + console.log(data.toString());
453 + });
454 +}
455 +
410 function sleep(ms) { 456 function sleep(ms) {
411 const wakeUpTime = Date.now() + ms; 457 const wakeUpTime = Date.now() + ms;
412 while (Date.now() < wakeUpTime) {} 458 while (Date.now() < wakeUpTime) {}
......