정민혁

언어 변경 옵션 제작, 한->영,영->한 중 선택가능

...@@ -3,6 +3,7 @@ var express = require('express'); ...@@ -3,6 +3,7 @@ var express = require('express');
3 var path = require('path'); 3 var path = require('path');
4 var cookieParser = require('cookie-parser'); 4 var cookieParser = require('cookie-parser');
5 var logger = require('morgan'); 5 var logger = require('morgan');
6 +var bodyparser = require('body-parser');
6 7
7 var indexRouter = require('./routes/index'); 8 var indexRouter = require('./routes/index');
8 var keyboardRouter = require('./routes/keyboard'); 9 var keyboardRouter = require('./routes/keyboard');
...@@ -15,8 +16,9 @@ app.set('views', path.join(__dirname, 'views')); ...@@ -15,8 +16,9 @@ app.set('views', path.join(__dirname, 'views'));
15 app.set('view engine', 'ejs'); 16 app.set('view engine', 'ejs');
16 17
17 app.use(logger('dev')); 18 app.use(logger('dev'));
19 +app.use(bodyparser.json());
18 app.use(express.json()); 20 app.use(express.json());
19 -app.use(express.urlencoded({ extended: false })); 21 +app.use(express.urlencoded({ extended: true }));
20 app.use(cookieParser()); 22 app.use(cookieParser());
21 app.use(express.static(path.join(__dirname, 'public'))); 23 app.use(express.static(path.join(__dirname, 'public')));
22 24
......
1 var express = require('express'); 1 var express = require('express');
2 var request = require('request'); 2 var request = require('request');
3 var app = express.Router(); 3 var app = express.Router();
4 -
5 // Naver Auth Key // to do)) get a new Auth key 4 // Naver Auth Key // to do)) get a new Auth key
6 var client_id = 'is216aNtQ6QLNkWW03bc'; 5 var client_id = 'is216aNtQ6QLNkWW03bc';
7 var client_secret = 'Ot0BMdWXOJ'; 6 var client_secret = 'Ot0BMdWXOJ';
8 7
9 // Naver API URL 8 // Naver API URL
10 var api_url = 'https://openapi.naver.com/v1/language/translate'; 9 var api_url = 'https://openapi.naver.com/v1/language/translate';
11 - 10 +var source = 'ko';
11 +var target = 'en';
12 //what kind of task to do 12 //what kind of task to do
13 var translate = false; 13 var translate = false;
14 var dictionary = false; 14 var dictionary = false;
...@@ -32,7 +32,7 @@ app.post('/', function(req, res) { ...@@ -32,7 +32,7 @@ app.post('/', function(req, res) {
32 "keyboard": { 32 "keyboard": {
33 "type": "buttons", 33 "type": "buttons",
34 "buttons":["한-영", "영-한"] 34 "buttons":["한-영", "영-한"]
35 - }, 35 + }
36 }); 36 });
37 }else if (_obj.content == '번역'){ 37 }else if (_obj.content == '번역'){
38 translate = true; 38 translate = true;
...@@ -65,7 +65,7 @@ app.post('/', function(req, res) { ...@@ -65,7 +65,7 @@ app.post('/', function(req, res) {
65 var options = { 65 var options = {
66 url: api_url, 66 url: api_url,
67 // (source : 번역할 대상), (target: 번역후 언어), 카톡에서 받는 메시지(text) 67 // (source : 번역할 대상), (target: 번역후 언어), 카톡에서 받는 메시지(text)
68 - form: {'source':'ko', 'target':'en', 'text':req.body.content}, 68 + form: {'source':source, 'target':target, 'text':req.body.content},
69 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} 69 headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
70 }; 70 };
71 // Naver Post API 71 // Naver Post API
...@@ -78,24 +78,23 @@ app.post('/', function(req, res) { ...@@ -78,24 +78,23 @@ app.post('/', function(req, res) {
78 console.log(objBody.message.result.translatedText); 78 console.log(objBody.message.result.translatedText);
79 79
80 // Kakao Message API 80 // Kakao Message API
81 - let massage = { 81 + let message = {
82 "message": { 82 "message": {
83 // Naver API Translate 결과를 Kakao Message 83 // Naver API Translate 결과를 Kakao Message
84 "text": objBody.message.result.translatedText 84 "text": objBody.message.result.translatedText
85 } 85 }
86 }; 86 };
87 -
88 // Kakao Message API 전송 87 // Kakao Message API 전송
89 res.set({ 88 res.set({
90 'content-type': 'application/json' 89 'content-type': 'application/json'
91 - }).send(JSON.stringify(massage)); 90 + }).send(JSON.stringify(message));
92 } 91 }
93 else{ 92 else{
94 // Naver Message Error 발생 93 // Naver Message Error 발생
95 res.status(response.statusCode).end(); 94 res.status(response.statusCode).end();
96 console.log('error = ' + response.statusCode); 95 console.log('error = ' + response.statusCode);
97 96
98 - let massage = { 97 + let message = {
99 "message": { 98 "message": {
100 "text": response.statusCode 99 "text": response.statusCode
101 }, 100 },
...@@ -104,13 +103,61 @@ app.post('/', function(req, res) { ...@@ -104,13 +103,61 @@ app.post('/', function(req, res) {
104 // Kakao에 Error Message 103 // Kakao에 Error Message
105 res.set({ 104 res.set({
106 'content-type': 'application/json' 105 'content-type': 'application/json'
107 - }).send(JSON.stringify(massage)); 106 + }).send(JSON.stringify(message));
108 } 107 }
109 }); 108 });
110 } 109 }
111 else if (dictionary) 110 else if (dictionary)
112 { 111 {
112 + var options = {
113 + url: 'https://openapi.naver.com/v1/search/encyc.json?query=' + encodeURI(req.query.query)
114 + + '&display=3&start=1',
115 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
116 + };
117 + request.get(options, function (error, response, body) {
118 + if (!error && response.statusCode == 200) {
119 + var objBody = JSON.parse(response.body);
113 120
121 + let message = {
122 + "message": {
123 + "text": objBody.title + " : " + objBody.description
124 + },
125 + "message_button": {
126 + "label": "백과사전에서 직접보기",
127 + "url": objBody.link
128 + }
129 + };
130 +
131 + }
132 + else {
133 + res.status(response.statusCode).end();
134 + console.log('error = ' + response.statusCode);
135 + }
136 + });
137 +
138 + }
139 + else if (option)
140 + {
141 + if (_obj.content == "한-영")
142 + {
143 + source = 'ko';
144 + target = 'en';
145 + }
146 + else if (_obj.content == "영-한")
147 + {
148 + source = "en";
149 + target = "ko";
150 + }
151 + console.log(source, target);
152 + res.json({
153 + "message": {
154 + "text": source + "에서" + target + "으로 번역합니다."
155 + },
156 + "keyboard": {
157 + "type": "buttons",
158 + "buttons":["설정", "번역","사전"]
159 + }
160 + });
114 } 161 }
115 }); 162 });
116 163
......