EC2 Default User

app.js demo

1 var end = false; 1 var end = false;
2 var count = 0; 2 var count = 0;
3 -n1 = ''; 3 +var real_data = '';
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']]; 4 +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']];
5 var save_file_name = ['fin_1.txt','fin_2.txt','fin_3.txt','fin.txt']; 5 var save_file_name = ['fin_1.txt','fin_2.txt','fin_3.txt','fin.txt'];
6 var choosen = new Array(); 6 var choosen = new Array();
7 var express = require('express'); 7 var express = require('express');
...@@ -9,8 +9,8 @@ const request = require('request'); ...@@ -9,8 +9,8 @@ const request = require('request');
9 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 9 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
10 const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU=' 10 const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU='
11 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' 11 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
12 -const PAPAGO_ID = '1Z83W3lUGLqnNbrDAk8A' 12 +const PAPAGO_ID = '7S7IVuHMvSgjpygsJ0uN'
13 -const PAPAGO_SECRET = 'ST7ElRPFPP' 13 +const PAPAGO_SECRET = 'WpWAr55nkz'
14 var fs = require('fs'); 14 var fs = require('fs');
15 const path = require('path'); 15 const path = require('path');
16 const HTTPS = require('https'); 16 const HTTPS = require('https');
...@@ -20,6 +20,7 @@ const bodyParser = require('body-parser'); ...@@ -20,6 +20,7 @@ const bodyParser = require('body-parser');
20 const res = require('express/lib/response'); 20 const res = require('express/lib/response');
21 const { eventNames } = require('process'); 21 const { eventNames } = require('process');
22 const req = require('express/lib/request'); 22 const req = require('express/lib/request');
23 +const { compileFunction } = require('vm');
23 var app = express(); 24 var app = express();
24 app.use(bodyParser.json()); 25 app.use(bodyParser.json());
25 app.post('/hook', function (req, res) { 26 app.post('/hook', function (req, res) {
...@@ -42,7 +43,7 @@ app.post('/hook', function (req, res) { ...@@ -42,7 +43,7 @@ app.post('/hook', function (req, res) {
42 { 43 {
43 count++; 44 count++;
44 opt_1(eventObj.replyToken, eventObj.message.text); 45 opt_1(eventObj.replyToken, eventObj.message.text);
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) 46 + }else if((eventObj.message.text === '1' || eventObj.message.text === '2' || eventObj.message.text === '3' || eventObj.message.text == '4' || eventObj.message.text == '5') && count ==2)
46 { 47 {
47 choosen.push(eventObj.message.text); 48 choosen.push(eventObj.message.text);
48 count++; 49 count++;
...@@ -66,12 +67,26 @@ app.post('/hook', function (req, res) { ...@@ -66,12 +67,26 @@ app.post('/hook', function (req, res) {
66 { 67 {
67 choosen.push(eventObj.message.text); 68 choosen.push(eventObj.message.text);
68 count++; 69 count++;
70 + var txt = intersection();
69 Rmx_comment(eventObj.replyToken); 71 Rmx_comment(eventObj.replyToken);
70 }else if(count == 7 && end) 72 }else if(count == 7 && end)
71 { 73 {
72 - intersection(); 74 + async function read () {
73 - read_file('fin.txt') 75 + await fs.readFile(dir, 'utf8', (err,data) => {
74 - Rmx(eventObj.replyToken,n1); 76 + if(err) {
77 + console.error(err);
78 + return 'err';
79 + }
80 + sleep(500);
81 + real_data =real_data + data + ' ';
82 + console.log(typeof(data));
83 + });
84 + }
85 + console.log('hi');
86 + sleep(1000);
87 + console.log(txt);
88 + Rmx(eventObj.replyToken, txt);
89 +
75 count++; 90 count++;
76 }else if(count ==8 && end) 91 }else if(count ==8 && end)
77 { 92 {
...@@ -416,41 +431,33 @@ async function err(replyToken) ...@@ -416,41 +431,33 @@ async function err(replyToken)
416 }); 431 });
417 } 432 }
418 433
419 -async function read_file(dir)
420 -{
421 - await fs.readFile(dir, function(err, data) {
422 - if(err) throw err;
423 - n1 = data.toString().split("\n");
424 - });
425 -
426 -}
427 -
428 async function intersection() 434 async function intersection()
429 { 435 {
430 const spawn = await require('child_process').spawn; 436 const spawn = await require('child_process').spawn;
431 for (var i = 0, j = 0; i < 3; i += 2, j++) { 437 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]]); 438 + var result = await spawn('python', ['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) { 439 + await result.stdout.on('data', function (data) {
434 console.log(data.toString()); 440 console.log(data.toString());
435 }); 441 });
436 - result.stderr.on('data', function (data) { 442 + await result.stderr.on('data', function (data) {
437 console.log(data.toString()); 443 console.log(data.toString());
438 }); 444 });
439 } 445 }
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]]); 446 + result = await spawn('python', ['fileread_practice.py', save_file_name[0], save_file_name[1], save_file_name[2]]);
441 result.stdout.on('data', function (data) { 447 result.stdout.on('data', function (data) {
442 console.log(data.toString()); 448 console.log(data.toString());
443 }); 449 });
444 - result.stderr.on('data', function (data) { 450 + await result.stderr.on('data', function (data) {
445 console.log(data.toString()); 451 console.log(data.toString());
446 }); 452 });
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]]); 453 + result = await spawn('python', ['fileread_practice.py', save_file_name[2], directory[4][Number(choosen[4]) - 1], save_file_name[3]]);
448 result.stdout.on('data', function (data) { 454 result.stdout.on('data', function (data) {
449 console.log(data.toString()); 455 console.log(data.toString());
450 }); 456 });
451 - result.stderr.on('data', function (data) { 457 + await result.stderr.on('data', function (data) {
452 console.log(data.toString()); 458 console.log(data.toString());
453 }); 459 });
460 + return 'complete';
454 } 461 }
455 462
456 function sleep(ms) { 463 function sleep(ms) {
......