Ubuntu

modified

Showing 1 changed file with 111 additions and 8 deletions
...@@ -13,23 +13,30 @@ const keyFilename = '/home/ubuntu/termproject/focused-clock-277615-3a427d503822. ...@@ -13,23 +13,30 @@ const keyFilename = '/home/ubuntu/termproject/focused-clock-277615-3a427d503822.
13 const storage = new Storage({projectId, keyFilename}); 13 const storage = new Storage({projectId, keyFilename});
14 var upload = multer({ storage: _storage}); 14 var upload = multer({ storage: _storage});
15 var fs = require('fs'); 15 var fs = require('fs');
16 +const path = require('path');
17 +const HTTPS = require('https');
18 +const domain = "www.tplinechatbot.tk"
19 +const sslport = 23023;
16 var app = express(); 20 var app = express();
17 21
18 const TOKEN = 'h8xN39Zg0nRmPhRcwCfUt35UhGQvinzOSzl1VSHWPdaKaW4VDDs2bTvhvxnOH1FVzzJ4t1L0ih/uJ1idIKz8SvPin3PM4nLgt6roTaEVw105aIsZqDwlsKPS2ewb2WVbL2TK/BqVEBQ2MmHO2xFRzAdB04t89/1O/w1cDnyilFU=' 22 const TOKEN = 'h8xN39Zg0nRmPhRcwCfUt35UhGQvinzOSzl1VSHWPdaKaW4VDDs2bTvhvxnOH1FVzzJ4t1L0ih/uJ1idIKz8SvPin3PM4nLgt6roTaEVw105aIsZqDwlsKPS2ewb2WVbL2TK/BqVEBQ2MmHO2xFRzAdB04t89/1O/w1cDnyilFU='
19 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' 23 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
20 const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK' 24 const PAPAGO_ID = 'W5BknW1dTHXMah9QZ_KK'
21 const PAPAGO_SECRET = 'doQdf3ZMU3' 25 const PAPAGO_SECRET = 'doQdf3ZMU3'
26 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
22 var request = require('request'); 27 var request = require('request');
23 28
24 var _storage = multer.diskStorage({ 29 var _storage = multer.diskStorage({
25 - destination: function (req, file, cb) { //path - 파일의 형식에 따라 나눔 if문 30 + destination: function (req, file, cb) {
26 cb(null, 'uploads/') 31 cb(null, 'uploads/')
27 }, 32 },
28 - filename: function (req, file, cb) { //filename 중복된 파일이 존재하는지 33 + filename: function (req, file, cb) {
29 cb(null, file.originalname) 34 cb(null, file.originalname)
30 } 35 }
31 }) 36 })
32 37
38 +
39 +
33 app.use(bodyParser.urlencoded({ extended: false })); 40 app.use(bodyParser.urlencoded({ extended: false }));
34 app.locals.pretty = true; 41 app.locals.pretty = true;
35 app.set('views', './views_file'); 42 app.set('views', './views_file');
...@@ -43,14 +50,15 @@ app.get('/upload', function(req, res){ ...@@ -43,14 +50,15 @@ app.get('/upload', function(req, res){
43 res.render('upload'); 50 res.render('upload');
44 }) 51 })
45 52
53 +var func_result;
46 //업로드된 파일 받기 54 //업로드된 파일 받기
47 app.post('/upload', upload.single('userfile'), function(req, res){ 55 app.post('/upload', upload.single('userfile'), function(req, res){
48 console.log(req.file); 56 console.log(req.file);
49 57
50 ImageRecognition(req.file.originalname); //사진에서 문자 인식 58 ImageRecognition(req.file.originalname); //사진에서 문자 인식
51 - //PAPAGOtranslation(); 59 + var transresult = PAPAGOtranslation(func_result);
52 60
53 - res.send('Uploaded' + req.file.originalname ); 61 + res.send('Uploaded' + req.file.originalname + ' 번역결과 ' + transresult );
54 }) 62 })
55 63
56 64
...@@ -58,8 +66,9 @@ app.post('/upload', upload.single('userfile'), function(req, res){ ...@@ -58,8 +66,9 @@ app.post('/upload', upload.single('userfile'), function(req, res){
58 async function ImageRecognition(uploadedfilename) { 66 async function ImageRecognition(uploadedfilename) {
59 const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`; 67 const fileName = `/home/ubuntu/termproject/uploads/${uploadedfilename}`;
60 const [result] = await client.textDetection(fileName); 68 const [result] = await client.textDetection(fileName);
69 + const func_result = await result.fullTextAnnotation.text;
61 console.log('Text: '+ result.fullTextAnnotation.text); 70 console.log('Text: '+ result.fullTextAnnotation.text);
62 - PAPAGOtranslation (result.fullTextAnnotation.text); //번역 71 + //transresult = PAPAGOtranslation (result.fullTextAnnotation.text); //번역
63 //const detections = result.textAnnotations; 72 //const detections = result.textAnnotations;
64 //detections.forEach(text => console.log(text)); 73 //detections.forEach(text => console.log(text));
65 } 74 }
...@@ -82,6 +91,49 @@ async function googlevisiontest(){ ...@@ -82,6 +91,49 @@ async function googlevisiontest(){
82 91
83 //papago trans 92 //papago trans
84 function PAPAGOtranslation (query) { 93 function PAPAGOtranslation (query) {
94 + request.post(
95 + {
96 + url: PAPAGO_URL,
97 + form: {'source':'ko', 'target':'en', 'text':query},
98 + headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET},
99 + body: `source=ko&target=en&text=` + query,
100 + json:true
101 + }, (error, response, body) => {
102 + if(!error && response.statusCode == 200) {
103 + var transMessage = body.message.result.translatedText;
104 + console.log("번역 결과: " + transMessage);
105 + }
106 +});
107 +return transMessage;
108 +}
109 +
110 +/*
111 +function detectLangs(query){
112 + var options = {
113 + url: PAPAGO_LANG_URL,
114 + form: {'query': query},
115 + headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}
116 + };
117 + request.post(
118 + {
119 + url: PAPAGO_LANG_URL,
120 + form: {'query': query},
121 + headers: {'X-Naver-Client-Id':PAPAGO_ID, 'X-Naver-Client-Secret': PAPAGO_SECRET}
122 + }, function (error, response, body) {
123 + if (!error && response.statusCode == 200) {
124 + res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
125 + res.end(body);
126 + } else {
127 + res.status(response.statusCode).end();
128 + console.log('error = ' + response.statusCode);
129 + }
130 + });
131 +
132 + }
133 + */
134 +
135 +//line message trans
136 +function PAPAGOtrans_forLine (replyToken, query) {
85 137
86 request.post( 138 request.post(
87 { 139 {
...@@ -94,7 +146,6 @@ function PAPAGOtranslation (query) { ...@@ -94,7 +146,6 @@ function PAPAGOtranslation (query) {
94 if(!error && response.statusCode == 200) { 146 if(!error && response.statusCode == 200) {
95 var transMessage = body.message.result.translatedText; 147 var transMessage = body.message.result.translatedText;
96 console.log(transMessage); 148 console.log(transMessage);
97 - /* //라인에 전송
98 request.post( 149 request.post(
99 { 150 {
100 url: TARGET_URL, 151 url: TARGET_URL,
...@@ -112,9 +163,61 @@ function PAPAGOtranslation (query) { ...@@ -112,9 +163,61 @@ function PAPAGOtranslation (query) {
112 } 163 }
113 },(error, response, body) => { 164 },(error, response, body) => {
114 console.log(body) 165 console.log(body)
115 - }); */ 166 + });
116 } 167 }
117 }); 168 });
118 } 169 }
119 170
120 - var server = app.listen(23023);
...\ No newline at end of file ...\ No newline at end of file
171 +function IsItCommand (message_){
172 + if (message_[0] == "!" ){
173 + return true;
174 + }
175 + return false;
176 +}
177 +
178 +function selectCommand (replyToken, message_){
179 + var result;
180 + if (message_ == "!명령어"){
181 + result = "명령어 설명";
182 + }else if (message_ == ("!이미지" || "!사진") ){
183 + result = "https://www.tplinechatbot.tk:23023/upload.\n\
184 + 링크에 접속하여 사진을 첨부해주세요.";
185 + }else if (message_ == "!언어"){
186 + result = "번역가능한 언어 리스트입니다.\n\
187 + 원하시는 언어를 '!영어'와 같이 입력해주세요.";
188 + }
189 + request.post(
190 + {
191 + url: TARGET_URL,
192 + headers: {
193 + 'Authorization': `Bearer ${TOKEN}`
194 + },
195 + json: {
196 + "replyToken":replyToken,
197 + "messages":[
198 + {
199 + "type":"text",
200 + "text":result
201 + }
202 + ]
203 + }
204 + },(error, response, body) => {
205 + console.log(body)
206 + });
207 +}
208 +
209 +//sever
210 + try {
211 + const option = {
212 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
213 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
214 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
215 + };
216 +
217 + HTTPS.createServer(option, app).listen(sslport, () => {
218 + console.log(`[HTTPS] Server is started on port ${sslport}`);
219 + });
220 +} catch (error) {
221 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
222 + console.log(error);
223 +}
...\ No newline at end of file ...\ No newline at end of file
......