최현준

add language detector

Showing 1 changed file with 46 additions and 7 deletions
......@@ -3,6 +3,7 @@ const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'm79m/yBsTVzZeC9pYqSLzEL00Hp0HqsVE/0ZsZxW2HNAwdnb9TwDgBcOyCn3/aJASXsE8ekpyGg4Gts/4r8LO72OTZec9Np5Mh9g1vrgyDj5theWv4g2miE5F1Cqax4X3waj1aIzDGHcUZdHD6fQvAdB04t89/1O/w1cDnyilFU='
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_D_URL = 'https://openapi.naver.com/v1/papago/detectLangs'
const PAPAGO_ID = 'tA41WEd_nbzy0nZcUQks'
const PAPAGO_SECRET = '6DHwov9gg2'
// const fs = require('fs');
......@@ -14,7 +15,9 @@ const bodyParser = require('body-parser');
var app = express();
var ln = 'en'; //default value en
var tempc = 'ko';
var lnDetec = new Array();
var userinfo = new Array();
lnDetec[0] =
{
"InLn" : '영어',
......@@ -31,6 +34,14 @@ lnDetec[2] =
"PaLn" : 'fr'
}
userinfo[0] =
{
userId : 'userId',
userInLn : 'userInLn',
userPaLn : 'userPaLn'
}
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
......@@ -44,7 +55,7 @@ app.post('/hook', function (req, res) {
// console.log('======================', new Date() ,'======================');
// console.log('[request]', req.body);
// console.log('[request source] ', eventObj.source);
console.log('userId? ', eventObj.source.userId);
console.log('userId: ', eventObj.source.userId);
// console.log('[request message]', eventObj.message);
// console.log('my meesage: ', message.text);
// console.log('my ip check: ', req.ip);
......@@ -57,13 +68,16 @@ app.post('/hook', function (req, res) {
LnCh = 0;
}
}
if(LnCh) // if there is no attempt to change language then call trans() operator
trans(eventObj.replyToken, eventObj.message.text, ln);
if(LnCh) // if there is no attempt to change language then call operators
{
detector(message.text);
trans(eventObj.replyToken, eventObj.message.text);
}
res.sendStatus(200);
});
function trans(replyToken, message, ln) {
function trans(replyToken, message) {
request.post(
{
......@@ -73,12 +87,12 @@ function trans(replyToken, message, ln) {
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'source=ko&target='+ln+'&text=' + message,
//body: 'source=ko&target=en&text=' + message,
//body: 'source=ko&target='+ln+'&text=' + message,
body: 'source='+tempc+'&target='+ln+'&text='+message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log(body.message);
console.log('[객체 정보 확인]',body.message);
var transMessage = body.message.result.translatedText;
request.post(
{
......@@ -103,6 +117,31 @@ function trans(replyToken, message, ln) {
}
function detector(message)
{
request.post
(
{
url: PAPAGO_D_URL,
headers:
{
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'query='+message,
json:true
},(error, response, body) =>
{
if(!error && response.statusCode == 200)
{
console.log('[넘어온 메시지]', message);
console.log('[객체 정보 확인]',body);
tempc = body.langCode;
}
}
)
}
// try {
// const option = {
// ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......