최현준

개인별 언어 저장추가

Showing 1 changed file with 71 additions and 35 deletions
...@@ -40,6 +40,12 @@ userinfo[0] = ...@@ -40,6 +40,12 @@ userinfo[0] =
40 userInLn : 'userInLn', 40 userInLn : 'userInLn',
41 userPaLn : 'userPaLn' 41 userPaLn : 'userPaLn'
42 } 42 }
43 +userinfo[1] =
44 +{
45 + userId : 'userId',
46 + userInLn : 'userInLn',
47 + userPaLn : 'userPaLn'
48 +}
43 49
44 50
45 app.use(bodyParser.json()); 51 app.use(bodyParser.json());
...@@ -48,9 +54,11 @@ app.post('/hook', function (req, res) { ...@@ -48,9 +54,11 @@ app.post('/hook', function (req, res) {
48 var eventObj = req.body.events[0]; 54 var eventObj = req.body.events[0];
49 var source = eventObj.source; 55 var source = eventObj.source;
50 var message = eventObj.message; 56 var message = eventObj.message;
51 - var Inln = message.text; // input message 57 + var InputMessage = message.text; // input message
52 var LnCh = 1; // Language change attempt => value 0; 58 var LnCh = 1; // Language change attempt => value 0;
53 - 59 + var user_id = eventObj.source.userId;
60 + var NumberLock = 0;
61 +
54 // request log 62 // request log
55 // console.log('======================', new Date() ,'======================'); 63 // console.log('======================', new Date() ,'======================');
56 // console.log('[request]', req.body); 64 // console.log('[request]', req.body);
...@@ -59,25 +67,78 @@ app.post('/hook', function (req, res) { ...@@ -59,25 +67,78 @@ app.post('/hook', function (req, res) {
59 // console.log('[request message]', eventObj.message); 67 // console.log('[request message]', eventObj.message);
60 // console.log('my meesage: ', message.text); 68 // console.log('my meesage: ', message.text);
61 // console.log('my ip check: ', req.ip); 69 // console.log('my ip check: ', req.ip);
62 - 70 +
71 + for(var i in userinfo)
72 + {
73 + console.log('i값 출력', i);
74 + console.log('userinfolength', userinfo.length);
75 + if (userinfo[i].userId == user_id)
76 + {
77 + console.log('동일한 id 존재.');
78 + NumberLock = i;
79 + break;
80 + }
81 + else if ( i == userinfo.length-1)
82 + {
83 + console.log('동일한 것이 존재하지 않습니다. 새로운 개체를 생성합니다.');
84 + userinfo[i+1] =
85 + {
86 + userId : user_id,
87 + userInLn : 'en',
88 + userPaLn : 'en' //default value
89 + }
90 + NumberLock = i+1;
91 + }
92 + }
93 +
63 for(var i in lnDetec) // Detect Language change 94 for(var i in lnDetec) // Detect Language change
64 { 95 {
65 - if (Inln == lnDetec[i].InLn) 96 + if (InputMessage == lnDetec[i].InLn)
66 { 97 {
67 - ln = lnDetec[i].PaLn; 98 + userinfo[NumberLock].userPaLn = lnDetec[i].PaLn;
68 LnCh = 0; 99 LnCh = 0;
69 } 100 }
70 } 101 }
71 102
72 if(LnCh) // if there is no attempt to change language then call operators 103 if(LnCh) // if there is no attempt to change language then call operators
73 { 104 {
74 - detector(message.text); 105 + detector(message.text, NumberLock);
75 - trans(eventObj.replyToken, eventObj.message.text); 106 + trans(eventObj.replyToken, eventObj.message.text, NumberLock);
76 } 107 }
77 res.sendStatus(200); 108 res.sendStatus(200);
78 }); 109 });
79 110
80 -function trans(replyToken, message) { 111 +function detector(message, NumberLock)
112 +{
113 + request.post
114 + (
115 + {
116 + url: PAPAGO_D_URL,
117 + headers:
118 + {
119 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
120 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
121 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
122 + },
123 + body: 'query='+message,
124 + json:true
125 + },(error, response, body) =>
126 + {
127 + if(!error && response.statusCode == 200)
128 + {
129 + console.log('[넘어온 메시지]', message);
130 + console.log('[객체 정보 확인]',body);
131 + console.log('[user 객체 정보 확인]',userinfo[NumberLock]);
132 + console.log('[NumberLock value?]',NumberLock);
133 + //tempc = body.langCode;
134 + //userinfo에 user입력 language언어 감지 및 저장
135 + userinfo[NumberLock].userInLn = body.langCode;
136 + }
137 + }
138 + )
139 +}
140 +
141 +function trans(replyToken, message, NumberLock) {
81 142
82 request.post( 143 request.post(
83 { 144 {
...@@ -88,7 +149,7 @@ function trans(replyToken, message) { ...@@ -88,7 +149,7 @@ function trans(replyToken, message) {
88 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` 149 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
89 }, 150 },
90 //body: 'source=ko&target='+ln+'&text=' + message, 151 //body: 'source=ko&target='+ln+'&text=' + message,
91 - body: 'source='+tempc+'&target='+ln+'&text='+message, 152 + body: 'source='+userinfo[NumberLock].userInLn+'&target='+userinfo[NumberLock].userPaLn+'&text='+message,
92 json:true 153 json:true
93 },(error, response, body) => { 154 },(error, response, body) => {
94 if(!error && response.statusCode == 200) { 155 if(!error && response.statusCode == 200) {
...@@ -114,34 +175,9 @@ function trans(replyToken, message) { ...@@ -114,34 +175,9 @@ function trans(replyToken, message) {
114 }); 175 });
115 } 176 }
116 }); 177 });
117 -
118 } 178 }
119 179
120 -function detector(message) 180 +
121 -{
122 - request.post
123 - (
124 - {
125 - url: PAPAGO_D_URL,
126 - headers:
127 - {
128 - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
129 - 'X-Naver-Client-Id': `${PAPAGO_ID}`,
130 - 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
131 - },
132 - body: 'query='+message,
133 - json:true
134 - },(error, response, body) =>
135 - {
136 - if(!error && response.statusCode == 200)
137 - {
138 - console.log('[넘어온 메시지]', message);
139 - console.log('[객체 정보 확인]',body);
140 - tempc = body.langCode;
141 - }
142 - }
143 - )
144 -}
145 // try { 181 // try {
146 // const option = { 182 // const option = {
147 // ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 183 // ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......