Showing
1 changed file
with
113 additions
and
82 deletions
1 | - | ||
2 | var express = require("express"); | 1 | var express = require("express"); |
3 | var app = express(); | 2 | var app = express(); |
4 | const line = require('@line/bot-sdk'); | 3 | const line = require('@line/bot-sdk'); |
5 | var request = require('request'); | 4 | var request = require('request'); |
6 | var https=require('https'); | 5 | var https=require('https'); |
7 | var http=require('http'); | 6 | var http=require('http'); |
8 | -const lex = require('greenlock-express').create({ | 7 | + |
8 | +/* | ||
9 | +var fs = require("fs"); | ||
10 | + | ||
11 | +var httpsOptions = { | ||
12 | + key: fs.readFileSync('/etc/letsencrypt/live/oss.chatbot.bu.to/privkey.pem'), | ||
13 | + cert: fs.readFileSync('/etc/letsencrypt/live/oss.chatbot.bu.to/cert.pem') | ||
14 | +}; | ||
15 | + | ||
16 | +http.createServer(app).listen(80); | ||
17 | +https.createServer(httpsOptions, app).listen(443); | ||
18 | +*/ | ||
19 | +/* if ssl expired | ||
20 | +var greenlock= require('greenlock-express'); | ||
21 | +const lex = greenlock .create({ | ||
9 | version: 'draft-11', // 버전2 | 22 | version: 'draft-11', // 버전2 |
10 | store: require('greenlock-store-fs'), | 23 | store: require('greenlock-store-fs'), |
11 | configDir: '/etc/letsencrypt', // 또는 ~/letsencrypt/etc | 24 | configDir: '/etc/letsencrypt', // 또는 ~/letsencrypt/etc |
... | @@ -21,15 +34,11 @@ const lex = require('greenlock-express').create({ | ... | @@ -21,15 +34,11 @@ const lex = require('greenlock-express').create({ |
21 | }, | 34 | }, |
22 | renewWithin: 81 * 24 * 60 * 60 * 1000, | 35 | renewWithin: 81 * 24 * 60 * 60 * 1000, |
23 | renewBy: 80 * 24 * 60 * 60 * 1000, | 36 | renewBy: 80 * 24 * 60 * 60 * 1000, |
24 | -});//papago api | 37 | +});*/ |
38 | + | ||
39 | +//papago api | ||
25 | 40 | ||
26 | 41 | ||
27 | -https.createServer(lex.httpsOptions, lex.middleware(app)).listen((process.env.SSL_PORT || 443),()=>{ | ||
28 | - console.log("server on 443"); | ||
29 | -}); | ||
30 | -http.createServer(lex.middleware(require('redirect-https')())).listen(process.env.PORT || 80,()=>{ | ||
31 | - console.log("server on 80"); | ||
32 | -}); | ||
33 | //번역 api_url | 42 | //번역 api_url |
34 | var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | 43 | var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; |
35 | 44 | ||
... | @@ -53,22 +62,31 @@ const client = new line.Client(config); | ... | @@ -53,22 +62,31 @@ const client = new line.Client(config); |
53 | 62 | ||
54 | // register a webhook handler with middleware | 63 | // register a webhook handler with middleware |
55 | // about the middleware, please refer to doc | 64 | // about the middleware, please refer to doc |
56 | -const vision = require('@google-cloud/vision'); | ||
57 | 65 | ||
58 | // Creates a client | 66 | // Creates a client |
59 | -const visionclient = new vision.ImageAnnotatorClient(); | 67 | +var vision = require('google-vision-api-client'); |
68 | + | ||
69 | +var requtil = vision.requtil; | ||
70 | + | ||
71 | + | ||
72 | + | ||
73 | +//Prepare your service account from trust preview certificated project | ||
74 | + | ||
75 | +var jsonfile = '/home/ubuntu/a/LINEBOT/googlevisionapikey'; | ||
76 | + | ||
77 | + | ||
60 | 78 | ||
61 | -/** | 79 | +//Initialize the api |
62 | - * TODO(developer): Uncomment the following line before running the sample. | ||
63 | - */ | ||
64 | -// const fileName = 'Local image file, e.g. /path/to/image.png'; | ||
65 | 80 | ||
66 | -// Performs text detection on the local file | 81 | +vision.init(jsonfile); |
67 | -const [result] = await visionclient.textDetection('/home/ubuntu/a/LINEBOT/photo/Fancy-TWICE.jpg'); | ||
68 | -const detections = result.textAnnotations; | ||
69 | -console.log('Text:'); | ||
70 | -detections.forEach(text => console.log(text)); | ||
71 | 82 | ||
83 | + | ||
84 | + | ||
85 | +//Build the request payloads | ||
86 | +var d = requtil.createRequests().addRequest( | ||
87 | +requtil.createRequest('/home/ubuntu/a/LINEBOT/photo/Fancy=TWICE.jpg') | ||
88 | +.withFeature('Optical Character Recognition', 3) | ||
89 | +.build()); | ||
72 | //Do query to the api server | 90 | //Do query to the api server |
73 | vision.query(d, function(e, r, d){ | 91 | vision.query(d, function(e, r, d){ |
74 | if(e) console.log('ERROR:', e); | 92 | if(e) console.log('ERROR:', e); |
... | @@ -77,6 +95,9 @@ if(e) console.log('ERROR:', e); | ... | @@ -77,6 +95,9 @@ if(e) console.log('ERROR:', e); |
77 | 95 | ||
78 | 96 | ||
79 | 97 | ||
98 | + | ||
99 | + | ||
100 | + | ||
80 | app.post('/webhook', line.middleware(config), (req, res) => { | 101 | app.post('/webhook', line.middleware(config), (req, res) => { |
81 | Promise | 102 | Promise |
82 | .all(req.body.events.map(handleEvent)) | 103 | .all(req.body.events.map(handleEvent)) |
... | @@ -87,72 +108,82 @@ app.post('/webhook', line.middleware(config), (req, res) => { | ... | @@ -87,72 +108,82 @@ app.post('/webhook', line.middleware(config), (req, res) => { |
87 | // event handler | 108 | // event handler |
88 | function handleEvent(event) { | 109 | function handleEvent(event) { |
89 | console.log(event.message); | 110 | console.log(event.message); |
90 | - if (event.type !== 'message' || event.message.type !== 'text') { | 111 | + |
91 | - // ignore non-text-message event | 112 | + |
113 | + if (event.type !== 'message' && (event.message.type !== 'text'||event.message.type !== 'image')) { | ||
114 | + // ignore non-message event | ||
92 | return Promise.resolve(null); | 115 | return Promise.resolve(null); |
93 | } | 116 | } |
94 | - return new Promise(function(resolve, reject) { | 117 | + else if(event.type == 'message' && event.message.type == 'image') |
95 | - //언어 감지 option | 118 | + { |
96 | - var detect_options = { | 119 | + |
97 | - url : languagedetect_api_url, | 120 | + } |
98 | - form : {'query': event.message.text}, | 121 | + else if(event.type == 'message' && event.message.type == 'text') |
99 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | 122 | + { |
100 | - }; | 123 | + return new Promise(function(resolve, reject) { |
101 | - //papago 언어 감지 | 124 | + //언어 감지 option |
102 | - request.post(detect_options, (error,response,body)=>{ | 125 | + var detect_options = { |
103 | - if(!error && response.statusCode == 200){ | 126 | + url : languagedetect_api_url, |
104 | - var detect_body = JSON.parse(response.body); | 127 | + form : {'query': event.message.text}, |
105 | - var source = ''; | 128 | + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
106 | - var target = ''; | 129 | + }; |
107 | - var result = { type: 'text', text:''}; | 130 | + //papago 언어 감지 |
108 | - | 131 | + request.post(detect_options, (error,response,body)=>{ |
109 | - //언어 감지가 제대로 됐는지 확인 | 132 | + if(!error && response.statusCode == 200){ |
110 | - console.log(detect_body.langCode); | 133 | + var detect_body = JSON.parse(response.body); |
111 | - | 134 | + var source = ''; |
112 | - | 135 | + var target = ''; |
113 | - //번역은 한국어->영어 / 영어->한국어만 지원 | 136 | + var result = { type: 'text', text:''}; |
114 | - if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | 137 | + |
115 | - source = detect_body.langCode == 'ko' ? 'ko':'en'; | 138 | + //언어 감지가 제대로 됐는지 확인 |
116 | - target = source == 'ko' ? 'en':'ko'; | 139 | + console.log(detect_body.langCode); |
117 | - //papago 번역 option | 140 | + |
118 | - var options = { | 141 | + |
119 | - url: translate_api_url, | 142 | + //번역은 한국어->영어 / 영어->한국어만 지원 |
120 | - // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | 143 | + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ |
121 | - form: {'source':source, 'target':target, 'text':event.message.text}, | 144 | + source = detect_body.langCode == 'ko' ? 'ko':'en'; |
122 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | 145 | + target = source == 'ko' ? 'en':'ko'; |
123 | - | 146 | + //papago 번역 option |
124 | - }; | 147 | + var options = { |
125 | - | 148 | + url: translate_api_url, |
126 | - // Naver Post API | 149 | + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) |
127 | - console.log("?!"); | 150 | + form: {'source':source, 'target':target, 'text':event.message.text}, |
128 | - request.post(options, function(error, response, body){ | 151 | + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
129 | - // Translate API Sucess | 152 | + |
130 | - if(!error && response.statusCode == 200){ | 153 | + }; |
131 | - // JSON | 154 | + |
132 | - var objBody = JSON.parse(response.body); | 155 | + // Naver Post API |
133 | - // Message 잘 찍히는지 확인 | 156 | + console.log("?!"); |
134 | - | 157 | + request.post(options, function(error, response, body){ |
135 | - result.text = objBody.message.result.translatedText; | 158 | + // Translate API Sucess |
136 | - console.log("result: "+result.text); | 159 | + if(!error && response.statusCode == 200){ |
137 | - //번역된 문장 보내기 | 160 | + // JSON |
138 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | 161 | + var objBody = JSON.parse(response.body); |
139 | - } | 162 | + // Message 잘 찍히는지 확인 |
140 | - }); | 163 | + |
141 | - console.log("?!") | 164 | + result.text = objBody.message.result.translatedText; |
165 | + console.log("result: "+result.text); | ||
166 | + //번역된 문장 보내기 | ||
167 | + client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
168 | + } | ||
169 | + }); | ||
170 | + console.log("?!") | ||
171 | + } | ||
172 | + // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | ||
173 | + else{ | ||
174 | + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | ||
175 | + client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
176 | + } | ||
177 | + | ||
142 | } | 178 | } |
143 | - // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | ||
144 | else{ | 179 | else{ |
145 | - result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | 180 | + console.log("status code is not 200"); |
146 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
147 | } | 181 | } |
148 | - | 182 | + |
149 | - } | 183 | + }); |
150 | - else{ | 184 | + |
151 | - console.log("status code is not 200"); | 185 | + }); |
152 | - } | 186 | + } |
153 | - | ||
154 | - }); | ||
155 | - | ||
156 | - }); | ||
157 | } | 187 | } |
188 | + | ||
158 | 189 | ... | ... |
-
Please register or login to post a comment