정민혁

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

......@@ -3,6 +3,7 @@ var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyparser = require('body-parser');
var indexRouter = require('./routes/index');
var keyboardRouter = require('./routes/keyboard');
......@@ -15,8 +16,9 @@ app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyparser.json());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
......
var express = require('express');
var request = require('request');
var app = express.Router();
// Naver Auth Key // to do)) get a new Auth key
var client_id = 'is216aNtQ6QLNkWW03bc';
var client_secret = 'Ot0BMdWXOJ';
// Naver API URL
var api_url = 'https://openapi.naver.com/v1/language/translate';
var source = 'ko';
var target = 'en';
//what kind of task to do
var translate = false;
var dictionary = false;
......@@ -32,7 +32,7 @@ app.post('/', function(req, res) {
"keyboard": {
"type": "buttons",
"buttons":["한-영", "영-한"]
},
}
});
}else if (_obj.content == '번역'){
translate = true;
......@@ -65,7 +65,7 @@ app.post('/', function(req, res) {
var options = {
url: api_url,
// (source : 번역할 대상), (target: 번역후 언어), 카톡에서 받는 메시지(text)
form: {'source':'ko', 'target':'en', 'text':req.body.content},
form: {'source':source, 'target':target, 'text':req.body.content},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
// Naver Post API
......@@ -78,24 +78,23 @@ app.post('/', function(req, res) {
console.log(objBody.message.result.translatedText);
// Kakao Message API
let massage = {
let message = {
"message": {
// Naver API Translate 결과를 Kakao Message
"text": objBody.message.result.translatedText
}
};
// Kakao Message API 전송
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(massage));
}).send(JSON.stringify(message));
}
else{
// Naver Message Error 발생
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
let massage = {
let message = {
"message": {
"text": response.statusCode
},
......@@ -104,13 +103,61 @@ app.post('/', function(req, res) {
// Kakao에 Error Message
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(massage));
}).send(JSON.stringify(message));
}
});
}
else if (dictionary)
{
var options = {
url: 'https://openapi.naver.com/v1/search/encyc.json?query=' + encodeURI(req.query.query)
+ '&display=3&start=1',
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var objBody = JSON.parse(response.body);
let message = {
"message": {
"text": objBody.title + " : " + objBody.description
},
"message_button": {
"label": "백과사전에서 직접보기",
"url": objBody.link
}
};
}
else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
}
else if (option)
{
if (_obj.content == "한-영")
{
source = 'ko';
target = 'en';
}
else if (_obj.content == "영-한")
{
source = "en";
target = "ko";
}
console.log(source, target);
res.json({
"message": {
"text": source + "에서" + target + "으로 번역합니다."
},
"keyboard": {
"type": "buttons",
"buttons":["설정", "번역","사전"]
}
});
}
});
......