taegwon

app.js almost final

1 var end = false; 1 var end = false;
2 +var selected_food = '';
3 +var located = '';
2 var count = 0; 4 var count = 0;
3 var real_data = ''; 5 var real_data = '';
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']]; 6 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'); ...@@ -9,8 +11,8 @@ const request = require('request');
9 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 11 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
10 const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU=' 12 const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU='
11 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' 13 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
12 -const PAPAGO_ID = '7S7IVuHMvSgjpygsJ0uN' 14 +const PAPAGO_ID = '1Z83W3lUGLqnNbrDAk8A'
13 -const PAPAGO_SECRET = 'WpWAr55nkz' 15 +const PAPAGO_SECRET = 'oDlgYoi4LO'
14 var fs = require('fs'); 16 var fs = require('fs');
15 const path = require('path'); 17 const path = require('path');
16 const HTTPS = require('https'); 18 const HTTPS = require('https');
...@@ -20,7 +22,7 @@ const bodyParser = require('body-parser'); ...@@ -20,7 +22,7 @@ const bodyParser = require('body-parser');
20 const res = require('express/lib/response'); 22 const res = require('express/lib/response');
21 const { eventNames } = require('process'); 23 const { eventNames } = require('process');
22 const req = require('express/lib/request'); 24 const req = require('express/lib/request');
23 -const { compileFunction } = require('vm'); 25 +const { getEnabledCategories } = require('trace_events');
24 var app = express(); 26 var app = express();
25 app.use(bodyParser.json()); 27 app.use(bodyParser.json());
26 app.post('/hook', function (req, res) { 28 app.post('/hook', function (req, res) {
...@@ -39,8 +41,16 @@ app.post('/hook', function (req, res) { ...@@ -39,8 +41,16 @@ app.post('/hook', function (req, res) {
39 41
40 count++; 42 count++;
41 start_msg(eventObj.replyToken, eventObj.message.text); 43 start_msg(eventObj.replyToken, eventObj.message.text);
42 - }else if(eventObj.message.text === 'y' && count ==1) 44 + }else if((eventObj.message.text === 'y' || eventObj.message.text == 'n' )&& count ==1)
43 { 45 {
46 + if(eventObj.message.text == 'n')
47 + {
48 + async function ending () {
49 + await end_of_chatbot(eventObj.replyToken);
50 + setTimeout(process.exit(1), 5 * 1000);
51 + }
52 + ending();
53 + }
44 count++; 54 count++;
45 opt_1(eventObj.replyToken, eventObj.message.text); 55 opt_1(eventObj.replyToken, eventObj.message.text);
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) 56 }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,36 +78,68 @@ app.post('/hook', function (req, res) { ...@@ -68,36 +78,68 @@ app.post('/hook', function (req, res) {
68 choosen.push(eventObj.message.text); 78 choosen.push(eventObj.message.text);
69 count++; 79 count++;
70 var txt = intersection(); 80 var txt = intersection();
71 - Rmx_comment(eventObj.replyToken); 81 + Rmx_1(eventObj.replyToken,eventObj.message.text);
72 - }else if(count == 7 && end) 82 + }else if(count == 7 && (eventObj.message.text == 'y' || eventObj.message.text == 'n') && end)
73 { 83 {
84 + if(eventObj.message.text == 'n')
85 + {
86 + async function ending () {
87 + await end_of_chatbot(eventObj.replyToken);
88 + setTimeout(process.exit(1), 5 * 1000);
89 + }
90 + ending();
91 + }
74 async function read () { 92 async function read () {
75 - await fs.readFile(dir, 'utf8', (err,data) => { 93 + await fs.readFile('fin.txt', 'utf8', async function (err,data) {
76 - if(err) { 94 + var ary = data.toString().split("\n");
77 - console.error(err); 95 + for(i in ary)
78 - return 'err'; 96 + {
97 + real_data = await real_data + ary[i] + ' ';
98 + console.log(ary[i]);
99 + sleep(500);
79 } 100 }
80 - sleep(500);
81 - real_data =real_data + data + ' ';
82 - console.log(typeof(data));
83 }); 101 });
84 } 102 }
85 - console.log('hi'); 103 +
86 - sleep(1000); 104 + read();
87 - console.log(txt); 105 + Rmx_comment(eventObj.replyToken)
88 - Rmx(eventObj.replyToken, txt);
89 106
90 count++; 107 count++;
91 }else if(count ==8 && end) 108 }else if(count ==8 && end)
92 { 109 {
93 - 110 + if(real_data == ' ')
94 - 111 + {
112 + no(eventObj.replyToken);
113 + }else{
114 + Rmx(eventObj.replyToken, real_data);
115 + }
95 116
96 - }else if(count == 9) 117 + count++;
118 + }else if(count == 9 && end)
97 { 119 {
120 + selected_food = eventObj.message.text;
121 + if(real_data.indexOf(selected_food) != -1)
122 + {
123 + loc(eventObj.replyToken);
124 + count++;
125 + }else{
126 + err(eventObj.replyToken);
127 + }
128 +
129 + }else if(count == 10 && end)
130 + {
131 + located = eventObj.message.text;
98 load_map(eventObj.replyToken); 132 load_map(eventObj.replyToken);
99 - count++; 133 + }else if(count == 11 && end)
100 - }else{ 134 + {
135 + async function ending () {
136 + await end_of_chatbot(eventObj.replyToken);
137 + setTimeout(process.exit(1), 5 * 1000);
138 + }
139 + ending();
140 + }
141 + else
142 + {
101 err(eventObj.replyToken); 143 err(eventObj.replyToken);
102 } 144 }
103 145
...@@ -359,7 +401,44 @@ async function Rmx(replyToken,message) { ...@@ -359,7 +401,44 @@ async function Rmx(replyToken,message) {
359 'X-Naver-Client-Id': `${PAPAGO_ID}`, 401 'X-Naver-Client-Id': `${PAPAGO_ID}`,
360 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 402 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
361 }, 403 },
362 - body: 'source=en&target=ko&text=' + message, 404 + body: 'source=en&target=ko&text=' + "추천 음식은" + message + "입니다. 음식을 고르시오.",
405 + json: true
406 + }, (error, response, body) => {
407 + if (!error && response.statusCode == 200) {
408 + console.log(body.message);
409 + var transMessage = body.message.result.translatedText;
410 + request.post(
411 + {
412 + url: TARGET_URL,
413 + headers: {
414 + 'Authorization': `Bearer ${TOKEN}`
415 + },
416 + json: {
417 + "replyToken": replyToken,
418 + "messages": [
419 + {
420 + "type": "text",
421 + "text": transMessage
422 + }
423 + ]
424 + }
425 + }, (error, response, body) => {
426 + console.log(body)
427 + });
428 + }
429 + });
430 +}
431 +
432 +async function Rmx_1(replyToken,message) {
433 + await request.post(
434 + {
435 + url: PAPAGO_URL,
436 + headers: {
437 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
438 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
439 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
440 + },
441 + body: 'source=en&target=ko&text=' + "추천음식을 보겠습니까? (y/n)",
363 json: true 442 json: true
364 }, (error, response, body) => { 443 }, (error, response, body) => {
365 if (!error && response.statusCode == 200) { 444 if (!error && response.statusCode == 200) {
...@@ -389,6 +468,8 @@ async function Rmx(replyToken,message) { ...@@ -389,6 +468,8 @@ async function Rmx(replyToken,message) {
389 468
390 async function load_map(replyToken) 469 async function load_map(replyToken)
391 { 470 {
471 + var map_txt = "https://map.naver.com/v5/search/" + located + "%20" + selected_food;
472 + map_txt = map_txt.replace(/\s/g, "%20");
392 await request.post( 473 await request.post(
393 { 474 {
394 url: TARGET_URL, 475 url: TARGET_URL,
...@@ -400,7 +481,51 @@ async function load_map(replyToken) ...@@ -400,7 +481,51 @@ async function load_map(replyToken)
400 "messages": [ 481 "messages": [
401 { 482 {
402 "type": "text", 483 "type": "text",
403 - "text": "https://www.daleseo.com/js-sleep/" 484 + "text": "지도 : " + map_txt
485 + }
486 + ]
487 + }
488 + }, (error, response, body) => {
489 + console.log(body)
490 + });
491 +}
492 +
493 +async function no(replyToken)
494 +{
495 + await request.post(
496 + {
497 + url: TARGET_URL,
498 + headers: {
499 + 'Authorization': `Bearer ${TOKEN}`
500 + },
501 + json: {
502 + "replyToken": replyToken,
503 + "messages": [
504 + {
505 + "type": "text",
506 + "text": "추천음식은 없습니다.\n 챗봇을 종료합니다."
507 + }
508 + ]
509 + }
510 + }, (error, response, body) => {
511 + console.log(body)
512 + });
513 +}
514 +
515 +async function loc(replyToken)
516 +{
517 + await request.post(
518 + {
519 + url: TARGET_URL,
520 + headers: {
521 + 'Authorization': `Bearer ${TOKEN}`
522 + },
523 + json: {
524 + "replyToken": replyToken,
525 + "messages": [
526 + {
527 + "type": "text",
528 + "text": "현재 위치를 입력하세요."
404 } 529 }
405 ] 530 ]
406 } 531 }
...@@ -460,6 +585,28 @@ async function intersection() ...@@ -460,6 +585,28 @@ async function intersection()
460 return 'complete'; 585 return 'complete';
461 } 586 }
462 587
588 +async function end_of_chatbot(replyToken)
589 +{
590 + await request.post(
591 + {
592 + url: TARGET_URL,
593 + headers: {
594 + 'Authorization': `Bearer ${TOKEN}`
595 + },
596 + json: {
597 + "replyToken": replyToken,
598 + "messages": [
599 + {
600 + "type": "text",
601 + "text": "챗봇을 종료합니다. 이용해주셔서 감사합니다."
602 + }
603 + ]
604 + }
605 + }, (error, response, body) => {
606 + console.log(body)
607 + });
608 +}
609 +
463 function sleep(ms) { 610 function sleep(ms) {
464 const wakeUpTime = Date.now() + ms; 611 const wakeUpTime = Date.now() + ms;
465 while (Date.now() < wakeUpTime) {} 612 while (Date.now() < wakeUpTime) {}
......