김형석

이미지 인식 추가. 번역은 미완성

Showing 1 changed file with 63 additions and 29 deletions
1 +var express = require('express');
2 +var app = express();
1 const line = require('@line/bot-sdk'); 3 const line = require('@line/bot-sdk');
2 -const express = require('express'); 4 +
5 +
3 //papago api 6 //papago api
4 var request = require('request'); 7 var request = require('request');
5 8
...@@ -14,18 +17,17 @@ var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs' ...@@ -14,18 +17,17 @@ var languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'
14 var client_id = 'xZMx34y7uru1v8lywZ2d'; 17 var client_id = 'xZMx34y7uru1v8lywZ2d';
15 var client_secret = 'p6L7M7WsH9'; 18 var client_secret = 'p6L7M7WsH9';
16 19
17 -// create LINE SDK config from env variables
18 const config = { 20 const config = {
19 channelAccessToken: 'mnny0MJSezgBXzR9C3Ddcc1Csdb7Y9jkvy2nqV5saOmvR2YOJ1/kj/2M0CNsLA+57B2qDpdUQ7WbCTtIKx/LAJ6Kwfop4tX3up7EM8H9EZK1td6GMbhhCb6wvUFVdb1PcTO4joCv8mspd3ubo8a+gAdB04t89/1O/w1cDnyilFU=', 21 channelAccessToken: 'mnny0MJSezgBXzR9C3Ddcc1Csdb7Y9jkvy2nqV5saOmvR2YOJ1/kj/2M0CNsLA+57B2qDpdUQ7WbCTtIKx/LAJ6Kwfop4tX3up7EM8H9EZK1td6GMbhhCb6wvUFVdb1PcTO4joCv8mspd3ubo8a+gAdB04t89/1O/w1cDnyilFU=',
20 channelSecret: 'bde77633a16fc5bfbd532d5990c6170e', 22 channelSecret: 'bde77633a16fc5bfbd532d5990c6170e',
21 }; 23 };
22 24
25 +
23 // create LINE SDK client 26 // create LINE SDK client
24 const client = new line.Client(config); 27 const client = new line.Client(config);
25 28
26 // create Express app 29 // create Express app
27 // about Express itself: https://expressjs.com/ 30 // about Express itself: https://expressjs.com/
28 -const app = express();
29 31
30 // register a webhook handler with middleware 32 // register a webhook handler with middleware
31 // about the middleware, please refer to doc 33 // about the middleware, please refer to doc
...@@ -35,7 +37,7 @@ app.post('/webhook', line.middleware(config), (req, res) => { ...@@ -35,7 +37,7 @@ app.post('/webhook', line.middleware(config), (req, res) => {
35 .then((result) => res.json(result)) 37 .then((result) => res.json(result))
36 .catch((err) => { 38 .catch((err) => {
37 console.error(err); 39 console.error(err);
38 - res.status(500).end(); 40 + res.status(200).end();
39 }); 41 });
40 }); 42 });
41 43
...@@ -46,33 +48,65 @@ function handleEvent(event) { ...@@ -46,33 +48,65 @@ function handleEvent(event) {
46 return Promise.resolve(null); 48 return Promise.resolve(null);
47 } 49 }
48 return new Promise(function(resolve, reject) { 50 return new Promise(function(resolve, reject) {
49 - // Naver Papago Translate 51 + //언어 감지 option
50 - var options = { 52 + var detect_options = {
51 - url: translate_api_url, 53 + url : languagedetect_api_url,
52 - // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) 54 + form : {'query': event.message.text},
53 - form: {'source':'ko', 'target':'en', 'text':event.message.text}, 55 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
54 - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} 56 + };
55 - }; 57 +
56 - 58 + //papago 언어 감지
57 - // Naver Post API 59 + request.post(detect_options,function(error,response,body){
58 - request.post(options, function(error, response, body){ 60 + console.log(response.statusCode);
59 - // Translate API Sucess 61 + if(!error && response.statusCode == 200){
60 - if(!error && response.statusCode == 200){ 62 + var detect_body = JSON.parse(response.body);
61 - // JSON 63 + var source = '';
62 - var objBody = JSON.parse(response.body); 64 + var target = '';
63 - // Message 잘 찍히는지 확인 65 + var result = { type: 'text', text:''};
64 - 66 +
65 - const result = { type: 'text', text: objBody.message.result.translatedText}; 67 + //언어 감지가 제대로 됐는지 확인
66 - console.log(result.text); 68 + console.log(detect_body.langCode);
67 - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 69 +
68 - }
69 - });
70 - });
71 - // use reply API
72 - //return client.replyMessage(event.replyToken,objBody.message.result.translatedText);
73 - }
74 70
71 + //번역은 한국어->영어 / 영어->한국어만 지원
72 + if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){
73 + source = detect_body.langCode == 'ko' ? 'ko':'en';
74 + target = source == 'ko' ? 'en':'ko';
75 + //papago 번역 option
76 + var options = {
77 + url: translate_api_url,
78 + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text)
79 + form: {'source':source, 'target':target, 'text':event.message.text},
80 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
81 + };
75 82
83 + // Naver Post API
84 + request.post(options, function(error, response, body){
85 + // Translate API Sucess
86 + if(!error && response.statusCode == 200){
87 + // JSON
88 + var objBody = JSON.parse(response.body);
89 + // Message 잘 찍히는지 확인
90 +
91 + result.text = objBody.message.result.translatedText;
92 + console.log(result.text);
93 + //번역된 문장 보내기
94 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
95 + }
96 + });
97 + }
98 + // 메시지의 언어가 영어 또는 한국어가 아닐 경우
99 + else{
100 + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
101 + client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
102 + }
103 +
104 + }
105 +
106 + });
107 +
108 + });
109 + }
76 110
77 app.listen(3000, function () { 111 app.listen(3000, function () {
78 console.log('Linebot listening on port 3000!'); 112 console.log('Linebot listening on port 3000!');
......