최현준

개인별 언어 저장추가

Showing 1 changed file with 71 additions and 35 deletions
......@@ -40,6 +40,12 @@ userinfo[0] =
userInLn : 'userInLn',
userPaLn : 'userPaLn'
}
userinfo[1] =
{
userId : 'userId',
userInLn : 'userInLn',
userPaLn : 'userPaLn'
}
app.use(bodyParser.json());
......@@ -48,9 +54,11 @@ app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
var message = eventObj.message;
var Inln = message.text; // input message
var InputMessage = message.text; // input message
var LnCh = 1; // Language change attempt => value 0;
var user_id = eventObj.source.userId;
var NumberLock = 0;
// request log
// console.log('======================', new Date() ,'======================');
// console.log('[request]', req.body);
......@@ -59,25 +67,78 @@ app.post('/hook', function (req, res) {
// console.log('[request message]', eventObj.message);
// console.log('my meesage: ', message.text);
// console.log('my ip check: ', req.ip);
for(var i in userinfo)
{
console.log('i값 출력', i);
console.log('userinfolength', userinfo.length);
if (userinfo[i].userId == user_id)
{
console.log('동일한 id 존재.');
NumberLock = i;
break;
}
else if ( i == userinfo.length-1)
{
console.log('동일한 것이 존재하지 않습니다. 새로운 개체를 생성합니다.');
userinfo[i+1] =
{
userId : user_id,
userInLn : 'en',
userPaLn : 'en' //default value
}
NumberLock = i+1;
}
}
for(var i in lnDetec) // Detect Language change
{
if (Inln == lnDetec[i].InLn)
if (InputMessage == lnDetec[i].InLn)
{
ln = lnDetec[i].PaLn;
userinfo[NumberLock].userPaLn = lnDetec[i].PaLn;
LnCh = 0;
}
}
if(LnCh) // if there is no attempt to change language then call operators
{
detector(message.text);
trans(eventObj.replyToken, eventObj.message.text);
detector(message.text, NumberLock);
trans(eventObj.replyToken, eventObj.message.text, NumberLock);
}
res.sendStatus(200);
});
function trans(replyToken, message) {
function detector(message, NumberLock)
{
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);
console.log('[user 객체 정보 확인]',userinfo[NumberLock]);
console.log('[NumberLock value?]',NumberLock);
//tempc = body.langCode;
//userinfo에 user입력 language언어 감지 및 저장
userinfo[NumberLock].userInLn = body.langCode;
}
}
)
}
function trans(replyToken, message, NumberLock) {
request.post(
{
......@@ -88,7 +149,7 @@ function trans(replyToken, message) {
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
//body: 'source=ko&target='+ln+'&text=' + message,
body: 'source='+tempc+'&target='+ln+'&text='+message,
body: 'source='+userinfo[NumberLock].userInLn+'&target='+userinfo[NumberLock].userPaLn+'&text='+message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
......@@ -114,34 +175,9 @@ function trans(replyToken, message) {
});
}
});
}
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'),
......