Showing
1 changed file
with
80 additions
and
128 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | var app = express(); | 2 | var app = express(); |
3 | const line = require('@line/bot-sdk'); | 3 | const line = require('@line/bot-sdk'); |
4 | + | ||
4 | const config = require('./config'); | 5 | const config = require('./config'); |
5 | 6 | ||
6 | //papago api | 7 | //papago api |
... | @@ -12,149 +13,100 @@ var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | ... | @@ -12,149 +13,100 @@ var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt'; |
12 | //언어감지 api_url | 13 | //언어감지 api_url |
13 | var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | 14 | var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; |
14 | 15 | ||
15 | -//eng grammar check | ||
16 | - | ||
17 | -var EnGrammarCheck_api_url = 'https://api.textgears.com/check.php'; | ||
18 | - | ||
19 | -import textgears from 'textgears'; | ||
20 | - | ||
21 | -textgears({ | ||
22 | - key: '9WUGcY6ZayYMphG7', | ||
23 | - text: prompt('Text'), | ||
24 | -}).then(res => { | ||
25 | - for (const error of res.errors) { | ||
26 | - console.log('Bad: %s. Better: %s', error.bad, error.better.join(', ')); | ||
27 | - } | ||
28 | -}); | ||
29 | 16 | ||
30 | // create LINE SDK client | 17 | // create LINE SDK client |
31 | -const client = new line.Client(config.line_config); | 18 | +const client = new line.Client(config); |
32 | 19 | ||
33 | // create Express app | 20 | // create Express app |
34 | // about Express itself: https://expressjs.com/ | 21 | // about Express itself: https://expressjs.com/ |
35 | - | ||
36 | // register a webhook handler with middleware | 22 | // register a webhook handler with middleware |
37 | // about the middleware, please refer to doc | 23 | // about the middleware, please refer to doc |
38 | -app.post('/webhook', line.middleware(config), (req, res) => { | 24 | +app.post("/webhook", line.middleware(config.line_config), (req, res) => { |
39 | - Promise | 25 | + // res.status(200).end(); |
40 | - .all(req.body.events.map(handleEvent)) | 26 | + Promise.all(req.body.events.map(handleEvent)).then(result => |
41 | - .then((result) => res.json(result)) | 27 | + res.json(result) |
42 | - .catch((err) => { | 28 | + ); |
43 | - console.error(err); res.status(200).end(); | ||
44 | - }); | ||
45 | -}); | ||
46 | - | ||
47 | -// Imports the Google Cloud client library | ||
48 | -const vision = require('@google-cloud/vision'); | ||
49 | - | ||
50 | -// Creates a client | ||
51 | -const client = new vision.ImageAnnotatorClient(); | ||
52 | - | ||
53 | -/** | ||
54 | - * TODO(developer): Uncomment the following line before running the sample. | ||
55 | - */ | ||
56 | -// const fileName = 'Local image file, e.g. /path/to/image.png'; | ||
57 | - | ||
58 | -// Read a local image as a text document | ||
59 | -const [result] = await client.documentTextDetection(fileName); | ||
60 | -const fullTextAnnotation = result.fullTextAnnotation; | ||
61 | -//console.log(`Full text: ${fullTextAnnotation.text}`); | ||
62 | -fullTextAnnotation.pages.forEach(page => { | ||
63 | - paragraph.words.forEach(word => { | ||
64 | - const wordText = word.symbols.map(s => s.text).join(''); | ||
65 | - // console.log(`Word text: ${wordText}`); | ||
66 | - | ||
67 | - word.symbols.forEach(symbol => { | ||
68 | - // console.log(`Symbol text: ${symbol.text}`); | ||
69 | - | ||
70 | - }); | ||
71 | - }); | ||
72 | }); | 29 | }); |
73 | 30 | ||
74 | -const vision = require('@google-cloud/vision'); | 31 | +function handleImgEvent(event) { |
75 | - | 32 | + |
76 | -// Creates a client | 33 | + switch (event) { |
77 | -const client = new vision.ImageAnnotatorClient(); | 34 | + case event.type = 'image': |
78 | - | 35 | + async function detectText(fileName) { |
79 | -/** | 36 | + // [START vision_text_detection] |
80 | - * TODO(developer): Uncomment the following line before running the sample. | 37 | + const vision = require('@google-cloud/vision'); |
81 | - */ | 38 | + |
82 | -// const fileName = 'Local image file, e.g. /path/to/image.png'; | 39 | + // Creates a client |
83 | - | 40 | + const client = new vision.ImageAnnotatorClient(); |
84 | -// Performs text detection on the local file | 41 | + |
85 | -const [result] = await client.textDetection(fileName); | 42 | + /** |
86 | -const detections = result.textAnnotations; | 43 | + * TODO(developer): Uncomment the following line before running the sample. |
87 | -//console.log('Text:'); | 44 | + */ |
88 | -detections.forEach(text => console.log(text)); | 45 | + // const fileName = 'Local image file, e.g. /path/to/image.png'; |
89 | - | 46 | + |
90 | - | 47 | + // Performs text detection on the local file |
91 | -// event handler | 48 | + const [result] = await client.textDetection(fileName); |
92 | -function handleEvent(event) { | 49 | + const detections = result.textAnnotations; |
93 | - if (event.type !== 'message' || event.message.type !== 'text') { | 50 | + console.log('Text:'); |
94 | - // ignore non-text-message event | 51 | + detections.forEach(text => console.log(text)); |
95 | - return Promise.resolve(null); | 52 | + // [END vision_text_detection] |
96 | - } | 53 | + } |
97 | - return new Promise(function(resolve, reject) { | 54 | + break; |
98 | - //언어 감지 option | 55 | + case event.type = 'message': |
99 | - var detect_options = { | 56 | + var detect_options = { |
100 | - url : languagedetect_api_url, | 57 | + url: languagedetect_api_url, |
101 | - form : {'query': event.message.text}, | 58 | + form: {'query': event.message.text}, |
102 | - headers: config.naver_header | 59 | + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
103 | - }; | 60 | + } |
104 | - | 61 | + request.post(detect_options, function (error, response, body) { |
105 | - //papago 언어 감지 | 62 | + console.log(response.statusCode); |
106 | - request.post(detect_options,function(error,response,body){ | 63 | + if (!error && response.statusCode == 200) { |
107 | - console.log(response.statusCode); | 64 | + var detect_body = JSON.parse(response.body); |
108 | - if(!error && response.statusCode == 200){ | 65 | + var source = ''; |
109 | - var detect_body = JSON.parse(response.body); | 66 | + var target = ''; |
110 | - var source = ''; | 67 | + var result = {type: 'text', text: ''}; |
111 | - var target = ''; | 68 | + |
112 | - var result = { type: 'text', text:''}; | 69 | + //언어 감지가 제대로 됐는지 확인 |
113 | - | 70 | + console.log(detect_body.langCode); |
114 | - //언어 감지가 제대로 됐는지 확인 | 71 | + //번역은 한국어->영어 / 영어->한국어만 지원 |
115 | - console.log(detect_body.langCode); | 72 | + if (detect_body.langCode == 'ko' || detect_body.langCode == 'en') { |
116 | - | 73 | + source = detect_body.langCode == 'ko' ? 'ko' : 'en'; |
117 | - | 74 | + target = source == 'ko' ? 'en' : 'ko'; |
118 | - //번역은 한국어->영어 / 영어->한국어만 지원 | 75 | + //papago 번역 option |
119 | - if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | 76 | + var options = { |
120 | - source = detect_body.langCode == 'ko' ? 'ko':'en'; | 77 | + url: translate_api_url, |
121 | - target = source == 'ko' ? 'en':'ko'; | ||
122 | - //papago 번역 option | ||
123 | - var options = { | ||
124 | - url: translate_api_url, | ||
125 | // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | 78 | // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) |
126 | - form: {'source':source, 'target':target, 'text':event.message.text}, | 79 | + form: {'source': source, 'target': target, 'text': event.message.text}, |
127 | - headers: config.naver_header | 80 | + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
128 | - }; | 81 | + }; |
129 | 82 | ||
130 | - // Naver Post API | 83 | + // Naver Post API |
131 | - request.post(options, function(error, response, body){ | 84 | + request.post(options, function (error, response, body) { |
132 | // Translate API Sucess | 85 | // Translate API Sucess |
133 | - if(!error && response.statusCode == 200){ | 86 | + if (!error && response.statusCode == 200) { |
134 | - // JSON | 87 | + // JSON |
135 | - var objBody = JSON.parse(response.body); | 88 | + var objBody = JSON.parse(response.body); |
136 | - // Message 잘 찍히는지 확인 | 89 | + // Message 잘 찍히는지 확인 |
137 | - | 90 | + |
138 | - result.text = objBody.message.result.translatedText; | 91 | + result.text = objBody.message.result.translatedText; |
139 | - console.log(result.text); | 92 | + console.log(result.text); |
140 | - //번역된 문장 보내기 | 93 | + //번역된 문장 보내기 |
141 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | 94 | + client.replyMessage(event.replyToken, result).then(resolve).catch(reject); |
142 | } | 95 | } |
143 | - }); | 96 | + }); |
144 | - } | 97 | + } |
145 | - // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | 98 | + // 메시지의 언어가 영어 또는 한국어가 아닐 경우 |
146 | - else{ | 99 | + else { |
147 | - result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | 100 | + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; |
148 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | 101 | + client.replyMessage(event.replyToken, result).then(resolve).catch(reject); |
149 | - } | 102 | + } |
150 | - | 103 | + break; |
151 | - } | ||
152 | 104 | ||
153 | - }); | ||
154 | 105 | ||
155 | - }); | 106 | + } |
107 | + }) | ||
156 | } | 108 | } |
157 | - | 109 | +} |
158 | app.listen(3000, function () { | 110 | app.listen(3000, function () { |
159 | console.log('Linebot listening on port 3000!'); | 111 | console.log('Linebot listening on port 3000!'); |
160 | -}); | 112 | +}) | ... | ... |
-
Please register or login to post a comment