Showing
1 changed file
with
93 additions
and
76 deletions
| ... | @@ -4,8 +4,9 @@ const bodyParser = require('body-parser'); | ... | @@ -4,8 +4,9 @@ const bodyParser = require('body-parser'); |
| 4 | const session = require('express-session'); | 4 | const session = require('express-session'); |
| 5 | const util = require('util'); | 5 | const util = require('util'); |
| 6 | const vision = require('@google-cloud/vision'); //google cloud vision api module | 6 | const vision = require('@google-cloud/vision'); //google cloud vision api module |
| 7 | -const translate = require('@google-cloud/translate').v3beta1; //google cloud translate api module | 7 | +const {Translate} = require('@google-cloud/translate').v2; |
| 8 | const credentials = require('/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'); | 8 | const credentials = require('/home/ubuntu/termproject/focused-clock-277615-3a427d503822.json'); |
| 9 | +const translate = new Translate({credentials}); | ||
| 9 | const client = new vision.ImageAnnotatorClient( {credentials} ); | 10 | const client = new vision.ImageAnnotatorClient( {credentials} ); |
| 10 | const {Storage} = require('@google-cloud/storage'); //google cloud translate api storage api moudle | 11 | const {Storage} = require('@google-cloud/storage'); //google cloud translate api storage api moudle |
| 11 | const projectId = 'focused-clock-277615'; | 12 | const projectId = 'focused-clock-277615'; |
| ... | @@ -24,6 +25,7 @@ const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | ... | @@ -24,6 +25,7 @@ const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' |
| 24 | const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK' | 25 | const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK' |
| 25 | const PAPAGO_SECRET = 'doQdf3ZMU3' | 26 | const PAPAGO_SECRET = 'doQdf3ZMU3' |
| 26 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | 27 | const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' |
| 28 | +const PAPAGO_LANG_URL = 'https://naveropenapi.apigw.ntruss.com/langs/v1/dect' | ||
| 27 | var request = require('request'); | 29 | var request = require('request'); |
| 28 | 30 | ||
| 29 | var _storage = multer.diskStorage({ | 31 | var _storage = multer.diskStorage({ |
| ... | @@ -50,17 +52,17 @@ app.get('/upload', function(req, res){ | ... | @@ -50,17 +52,17 @@ app.get('/upload', function(req, res){ |
| 50 | res.render('upload'); | 52 | res.render('upload'); |
| 51 | }) | 53 | }) |
| 52 | 54 | ||
| 53 | -var func_result; | ||
| 54 | //업로드된 파일 받기 | 55 | //업로드된 파일 받기 |
| 56 | +var transMessage; | ||
| 55 | app.post('/upload', upload.single('userfile'), function(req, res){ | 57 | app.post('/upload', upload.single('userfile'), function(req, res){ |
| 56 | console.log(req.file); | 58 | console.log(req.file); |
| 57 | 59 | ||
| 58 | ImageRecognition(req.file.originalname); //사진에서 문자 인식 | 60 | ImageRecognition(req.file.originalname); //사진에서 문자 인식 |
| 59 | - var transresult = PAPAGOtranslation(func_result); | ||
| 60 | 61 | ||
| 61 | - res.send('Uploaded' + req.file.originalname + ' 번역결과 ' + transresult ); | 62 | + res.send('Uploaded' + req.file.originalname + "번역결과 : "+ transMessage); |
| 62 | }) | 63 | }) |
| 63 | 64 | ||
| 65 | +var langcode2 = 'en'; | ||
| 64 | app.use(bodyParser.json()); | 66 | app.use(bodyParser.json()); |
| 65 | app.post('/hook', function (req, res) { | 67 | app.post('/hook', function (req, res) { |
| 66 | 68 | ||
| ... | @@ -74,9 +76,62 @@ app.post('/hook', function (req, res) { | ... | @@ -74,9 +76,62 @@ app.post('/hook', function (req, res) { |
| 74 | console.log('[request source] ', eventObj.source); | 76 | console.log('[request source] ', eventObj.source); |
| 75 | console.log('[request message]', eventObj.message); | 77 | console.log('[request message]', eventObj.message); |
| 76 | 78 | ||
| 77 | - var detect = detectLangs(eventObj.message.text); | 79 | + if(IsItCommand(eventObj.message.text)){ |
| 80 | + if (eventObj.message.text == "!명령어"){ | ||
| 81 | + request.post( | ||
| 82 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":eventObj.replyToken, | ||
| 83 | + "messages":[{"type":"text", "text":"!명령어\n!언어\n!사진번역"}]} | ||
| 84 | + },(error, response, body) => { | ||
| 85 | + console.log(body) | ||
| 86 | + }); | ||
| 87 | + }else if(eventObj.message.text == "!언어"){ | ||
| 88 | + request.post( | ||
| 89 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":eventObj.replyToken, | ||
| 90 | + "messages":[{"type":"text", "text":"원하시는 언어를 !영어 와 같은 형태로 입력해주세요."}, | ||
| 91 | + {"type":"text", "text":"지원 가능 언어 목록\n한국어\n영어\n일본어\n중국어 번체\n중국어 간체\n베트남어\n이탈리아어\n프랑스어\n인도네시아어\n독일어\n태국어\n러시아어\n스페인어"}]} | ||
| 92 | + },(error, response, body) => { | ||
| 93 | + console.log(body) | ||
| 94 | + }); | ||
| 95 | + | ||
| 96 | + }else if(eventObj.message.text == "!사진번역"){ | ||
| 97 | + request.post( | ||
| 98 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":eventObj.replyToken, | ||
| 99 | + "messages":[{"type":"text", "text":"https://www.tplinechatbot.tk:23023/upload"}]} | ||
| 100 | + },(error, response, body) => { | ||
| 101 | + console.log(body) | ||
| 102 | + }); | ||
| 103 | + }else{ | ||
| 104 | + if (langList.some(x=>{return "!" + x.name == eventObj.message.text})){ | ||
| 105 | + var setlangcode = langList.find(x=>{return "!" + x.name == eventObj.message.text}); | ||
| 106 | + if (setlangcode.code == 'jp'){ | ||
| 107 | + langcode2 = 'ja'; | ||
| 108 | + } else{ | ||
| 109 | + langcode2 = setlangcode.code; | ||
| 110 | + } | ||
| 111 | + request.post( | ||
| 112 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":eventObj.replyToken, | ||
| 113 | + "messages":[{"type":"text", "text":"언어가 설정되었습니다."}]} | ||
| 114 | + },(error, response, body) => { | ||
| 115 | + console.log(body) | ||
| 116 | + }); | ||
| 78 | 117 | ||
| 79 | - PAPAGOtrans_forLine(eventObj.replyToken, eventObj.message.text, detect); | 118 | + |
| 119 | + } else{ | ||
| 120 | + request.post( | ||
| 121 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":eventObj.replyToken, | ||
| 122 | + "messages":[{"type":"text", "text":"지원하지 않는 명령어 혹은 언어입니다."}]} | ||
| 123 | + },(error, response, body) => { | ||
| 124 | + console.log(body) | ||
| 125 | + }); | ||
| 126 | + | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + }else { | ||
| 130 | + PAPAGOtrans_forLine(eventObj.replyToken, eventObj.message.text, langcode2); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + | ||
| 134 | + //PAPAGOtrans_forLine(eventObj.replyToken, eventObj.message.text, langcode2); | ||
| 80 | 135 | ||
| 81 | 136 | ||
| 82 | res.sendStatus(200); | 137 | res.sendStatus(200); |
| ... | @@ -87,28 +142,10 @@ app.post('/hook', function (req, res) { | ... | @@ -87,28 +142,10 @@ app.post('/hook', function (req, res) { |
| 87 | async function ImageRecognition(uploadedfilename) { | 142 | async function ImageRecognition(uploadedfilename) { |
| 88 | const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`; | 143 | const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`; |
| 89 | const [result] = await client.textDetection(fileName); | 144 | const [result] = await client.textDetection(fileName); |
| 90 | - const func_result = await result.fullTextAnnotation.text; | ||
| 91 | console.log('Text: '+ result.fullTextAnnotation.text); | 145 | console.log('Text: '+ result.fullTextAnnotation.text); |
| 92 | - //transresult = PAPAGOtranslation (result.fullTextAnnotation.text); //번역 | 146 | + PAPAGOtranslation (result.fullTextAnnotation.text); //번역 |
| 93 | - //const detections = result.textAnnotations; | ||
| 94 | - //detections.forEach(text => console.log(text)); | ||
| 95 | } | 147 | } |
| 96 | 148 | ||
| 97 | -//google cloud storage bucket list 함수 | ||
| 98 | -async function googlevisiontest(){ | ||
| 99 | - | ||
| 100 | - try { | ||
| 101 | - const [buckets] = await storage.getBuckets(); | ||
| 102 | - | ||
| 103 | - console.log('Buckets:'); | ||
| 104 | - buckets.forEach((bucket) => { | ||
| 105 | - console.log(bucket.name); | ||
| 106 | - }); | ||
| 107 | - } catch (err) { | ||
| 108 | - console.error('ERROR:', err); | ||
| 109 | - } | ||
| 110 | -} | ||
| 111 | -//googlevisiontest(); | ||
| 112 | 149 | ||
| 113 | //papago trans | 150 | //papago trans |
| 114 | function PAPAGOtranslation (query) { | 151 | function PAPAGOtranslation (query) { |
| ... | @@ -121,37 +158,27 @@ function PAPAGOtranslation (query) { | ... | @@ -121,37 +158,27 @@ function PAPAGOtranslation (query) { |
| 121 | json:true | 158 | json:true |
| 122 | }, (error, response, body) => { | 159 | }, (error, response, body) => { |
| 123 | if(!error && response.statusCode == 200) { | 160 | if(!error && response.statusCode == 200) { |
| 124 | - var transMessage = body.message.result.translatedText; | 161 | + transMessage = body.message.result.translatedText; |
| 125 | console.log("번역 결과: " + transMessage); | 162 | console.log("번역 결과: " + transMessage); |
| 126 | } | 163 | } |
| 127 | }); | 164 | }); |
| 128 | -return transMessage; | ||
| 129 | } | 165 | } |
| 130 | 166 | ||
| 131 | -//detectLangs | 167 | +//line message trans |
| 132 | -function detectLangs(query){ | 168 | +async function PAPAGOtrans_forLine(replyToken, query, lang2) { |
| 133 | - request.post( | ||
| 134 | - { | ||
| 135 | - url: PAPAGO_LANG_URL, | ||
| 136 | - form: {'query': query}, | ||
| 137 | - headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET} | ||
| 138 | - }, (error, response, body) => { | ||
| 139 | - if (!error && response.statusCode == 200) { | ||
| 140 | - var result = body.langCode; | ||
| 141 | - } | ||
| 142 | - }); | ||
| 143 | - return result; | ||
| 144 | - | ||
| 145 | - } | ||
| 146 | 169 | ||
| 170 | + //언어감지 | ||
| 171 | + let [detections] = await translate.detect(query); | ||
| 172 | + detections = Array.isArray(detections) ? detections : [detections]; | ||
| 173 | + console.log('Detections:'); | ||
| 174 | + console.log(detections[0].language); | ||
| 175 | + langcode1 = detections[0].language; | ||
| 147 | 176 | ||
| 148 | -//line message trans | ||
| 149 | -function PAPAGOtrans_forLine (replyToken, query, detect) { | ||
| 150 | 177 | ||
| 151 | request.post( | 178 | request.post( |
| 152 | { | 179 | { |
| 153 | url: PAPAGO_URL, | 180 | url: PAPAGO_URL, |
| 154 | - form: {'source':`${detect}`, 'target':'en', 'text':query}, | 181 | + form: {'source':`${langcode1}`, 'target':`${lang2}`, 'text':query}, |
| 155 | headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}, | 182 | headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}, |
| 156 | body: `source=ko&target=en&text=` + query, | 183 | body: `source=ko&target=en&text=` + query, |
| 157 | json:true | 184 | json:true |
| ... | @@ -177,47 +204,37 @@ function PAPAGOtrans_forLine (replyToken, query, detect) { | ... | @@ -177,47 +204,37 @@ function PAPAGOtrans_forLine (replyToken, query, detect) { |
| 177 | },(error, response, body) => { | 204 | },(error, response, body) => { |
| 178 | console.log(body) | 205 | console.log(body) |
| 179 | }); | 206 | }); |
| 207 | + }else{request.post( | ||
| 208 | + {url: TARGET_URL, headers: {'Authorization': `Bearer ${TOKEN}`},json: {"replyToken":replyToken, | ||
| 209 | + "messages":[{"type":"text", "text":"오류가 발생했습니다.\n1. 번역이 지원되지 않는 언어입니다.\n2.설정된 언어로 번역을 시도했습니다.\n\n언어를 다시 설정해주세요.\n명령어 예시 : !영어"}]} | ||
| 210 | + },(error, response, body) => { | ||
| 211 | + console.log(body) | ||
| 212 | + }); | ||
| 180 | } | 213 | } |
| 181 | }); | 214 | }); |
| 182 | } | 215 | } |
| 183 | 216 | ||
| 184 | function IsItCommand (message_){ | 217 | function IsItCommand (message_){ |
| 185 | - if (message_[0] == "!" ){ | 218 | + if (message_.charAt(0) == "!" ){ |
| 186 | return true; | 219 | return true; |
| 187 | } | 220 | } |
| 188 | return false; | 221 | return false; |
| 189 | } | 222 | } |
| 190 | 223 | ||
| 191 | -function selectCommand (replyToken, message_){ | 224 | +const langList = new Array( |
| 192 | - var result; | 225 | + {code : 'ko', name: '한국어'}, |
| 193 | - if (message_ == "!명령어"){ | 226 | + {code : 'en', name: '영어'}, |
| 194 | - result = "명령어 설명"; | 227 | + {code : 'jp', name: '일본어'}, |
| 195 | - }else if (message_ == ("!이미지" || "!사진") ){ | 228 | + {code : 'zh-CN', name: '중국어 간체'}, |
| 196 | - result = "https://www.tplinechatbot.tk:23023/upload.\n\ | 229 | + {code : 'zh-TW', name: '중국어 번체'}, |
| 197 | - 링크에 접속하여 사진을 첨부해주세요."; | 230 | + {code : 'vi', name: '베트남어'}, |
| 198 | - }else if (message_ == "!언어"){ | 231 | + {code : 'id', name: '인도네시아어'}, |
| 199 | - result = "번역가능한 언어 리스트입니다.\n\ | 232 | + {code : 'th', name: '태국어'}, |
| 200 | - 원하시는 언어를 '!영어'와 같이 입력해주세요."; | 233 | + {code : 'de', name: '독일어'}, |
| 201 | - } | 234 | + {code : 'ru', name: '러시아어'}, |
| 202 | - request.post( | 235 | + {code : 'es', name: '스페인어'}, |
| 203 | - { | 236 | + {code : 'it', name: '이탈리아어'}, |
| 204 | - url: TARGET_URL, | 237 | + {code : 'fr', name: '프랑스어'}) |
| 205 | - headers: { | ||
| 206 | - 'Authorization': `Bearer ${TOKEN}` | ||
| 207 | - }, | ||
| 208 | - json: { | ||
| 209 | - "replyToken":replyToken, | ||
| 210 | - "messages":[ | ||
| 211 | - { | ||
| 212 | - "type":"text", | ||
| 213 | - "text":result | ||
| 214 | - } | ||
| 215 | - ] | ||
| 216 | - } | ||
| 217 | - },(error, response, body) => { | ||
| 218 | - console.log(body) | ||
| 219 | - }); | ||
| 220 | -} | ||
| 221 | 238 | ||
| 222 | //sever | 239 | //sever |
| 223 | try { | 240 | try { | ... | ... |
-
Please register or login to post a comment