yangjisu

이벤트가 이미지일때 텍스트일때

Showing 1 changed file with 48 additions and 96 deletions
var express = require('express');
var app = express();
const line = require('@line/bot-sdk');
const config = require('./config');
//papago api
......@@ -12,125 +13,77 @@ var translate_api_url = 'https://openapi.naver.com/v1/papago/n2mt';
//언어감지 api_url
var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs';
//eng grammar check
var EnGrammarCheck_api_url = 'https://api.textgears.com/check.php';
import textgears from 'textgears';
textgears({
key: '9WUGcY6ZayYMphG7',
text: prompt('Text'),
}).then(res => {
for (const error of res.errors) {
console.log('Bad: %s. Better: %s', error.bad, error.better.join(', '));
}
});
// create LINE SDK client
const client = new line.Client(config.line_config);
const client = new line.Client(config);
// create Express app
// about Express itself: https://expressjs.com/
// register a webhook handler with middleware
// about the middleware, please refer to doc
app.post('/webhook', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err); res.status(200).end();
});
app.post("/webhook", line.middleware(config.line_config), (req, res) => {
// res.status(200).end();
Promise.all(req.body.events.map(handleEvent)).then(result =>
res.json(result)
);
});
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
function handleImgEvent(event) {
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const fileName = 'Local image file, e.g. /path/to/image.png';
// Read a local image as a text document
const [result] = await client.documentTextDetection(fileName);
const fullTextAnnotation = result.fullTextAnnotation;
//console.log(`Full text: ${fullTextAnnotation.text}`);
fullTextAnnotation.pages.forEach(page => {
paragraph.words.forEach(word => {
const wordText = word.symbols.map(s => s.text).join('');
// console.log(`Word text: ${wordText}`);
word.symbols.forEach(symbol => {
// console.log(`Symbol text: ${symbol.text}`);
});
});
});
switch (event) {
case event.type = 'image':
async function detectText(fileName) {
// [START vision_text_detection]
const vision = require('@google-cloud/vision');
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
// Creates a client
const client = new vision.ImageAnnotatorClient();
/**
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const fileName = 'Local image file, e.g. /path/to/image.png';
// Performs text detection on the local file
const [result] = await client.textDetection(fileName);
const detections = result.textAnnotations;
//console.log('Text:');
detections.forEach(text => console.log(text));
// event handler
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
// const fileName = 'Local image file, e.g. /path/to/image.png';
// Performs text detection on the local file
const [result] = await client.textDetection(fileName);
const detections = result.textAnnotations;
console.log('Text:');
detections.forEach(text => console.log(text));
// [END vision_text_detection]
}
return new Promise(function(resolve, reject) {
//언어 감지 option
break;
case event.type = 'message':
var detect_options = {
url : languagedetect_api_url,
form : {'query': event.message.text},
headers: config.naver_header
};
//papago 언어 감지
request.post(detect_options,function(error,response,body){
url: languagedetect_api_url,
form: {'query': event.message.text},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
}
request.post(detect_options, function (error, response, body) {
console.log(response.statusCode);
if(!error && response.statusCode == 200){
if (!error && response.statusCode == 200) {
var detect_body = JSON.parse(response.body);
var source = '';
var target = '';
var result = { type: 'text', text:''};
var result = {type: 'text', text: ''};
//언어 감지가 제대로 됐는지 확인
console.log(detect_body.langCode);
//번역은 한국어->영어 / 영어->한국어만 지원
if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){
source = detect_body.langCode == 'ko' ? 'ko':'en';
target = source == 'ko' ? 'en':'ko';
if (detect_body.langCode == 'ko' || detect_body.langCode == 'en') {
source = detect_body.langCode == 'ko' ? 'ko' : 'en';
target = source == 'ko' ? 'en' : 'ko';
//papago 번역 option
var options = {
url: translate_api_url,
// 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
form: {'source':source, 'target':target, 'text':event.message.text},
headers: config.naver_header
form: {'source': source, 'target': target, 'text': event.message.text},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
// Naver Post API
request.post(options, function(error, response, body){
request.post(options, function (error, response, body) {
// Translate API Sucess
if(!error && response.statusCode == 200){
if (!error && response.statusCode == 200) {
// JSON
var objBody = JSON.parse(response.body);
// Message 잘 찍히는지 확인
......@@ -138,23 +91,22 @@ function handleEvent(event) {
result.text = objBody.message.result.translatedText;
console.log(result.text);
//번역된 문장 보내기
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
}
});
}
// 메시지의 언어가 영어 또는 한국어가 아닐 경우
else{
else {
result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
}
client.replyMessage(event.replyToken, result).then(resolve).catch(reject);
}
break;
});
});
}
})
}
}
app.listen(3000, function () {
console.log('Linebot listening on port 3000!');
});
})
......