Showing
2 changed files
with
91 additions
and
87 deletions
| ... | @@ -61,6 +61,10 @@ | ... | @@ -61,6 +61,10 @@ |
| 61 | <workItem from="1574494473410" duration="2577000" /> | 61 | <workItem from="1574494473410" duration="2577000" /> |
| 62 | <workItem from="1574565997693" duration="30000" /> | 62 | <workItem from="1574565997693" duration="30000" /> |
| 63 | <workItem from="1574569996051" duration="559000" /> | 63 | <workItem from="1574569996051" duration="559000" /> |
| 64 | + <workItem from="1574579419218" duration="794000" /> | ||
| 65 | + <workItem from="1574662422550" duration="816000" /> | ||
| 66 | + <workItem from="1574669792266" duration="65000" /> | ||
| 67 | + <workItem from="1574670031852" duration="24000" /> | ||
| 64 | </task> | 68 | </task> |
| 65 | <servers /> | 69 | <servers /> |
| 66 | </component> | 70 | </component> | ... | ... |
| ... | @@ -98,104 +98,104 @@ function handleEvent(event) { | ... | @@ -98,104 +98,104 @@ function handleEvent(event) { |
| 98 | }) | 98 | }) |
| 99 | }) | 99 | }) |
| 100 | } | 100 | } |
| 101 | - | 101 | + else { |
| 102 | - // 번역 기능 & 음성 기능 | 102 | + // 번역 기능 & 음성 기능 |
| 103 | - return new Promise(function(resolve, reject) { | 103 | + return new Promise(function (resolve, reject) { |
| 104 | - //언어 감지 option | 104 | + //언어 감지 option |
| 105 | - var detect_options = { | 105 | + var detect_options = { |
| 106 | - url : languagedetect_api_url, | 106 | + url: languagedetect_api_url, |
| 107 | - form : {'query': event.message.text}, | 107 | + form: {'query': event.message.text}, |
| 108 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
| 109 | - }; | ||
| 110 | - //papago 언어 감지 | ||
| 111 | - request.post(detect_options,function(error,response,body){ | ||
| 112 | - console.log(response.statusCode); | ||
| 113 | - if(!error && response.statusCode == 200){ | ||
| 114 | - var detect_body = JSON.parse(response.body); | ||
| 115 | - var source = ''; | ||
| 116 | - var target = ''; | ||
| 117 | - var result = { type: 'text', text:''}; | ||
| 118 | - //언어 감지가 제대로 됐는지 확인 | ||
| 119 | - console.log(detect_body.langCode); | ||
| 120 | - if(detect_options.form.query=='음성'){ | ||
| 121 | - console.log('audio streaming'); | ||
| 122 | - if (!error && response.statusCode == 200){ | ||
| 123 | - console.log(response.statusCode); | ||
| 124 | - for_audio_client.replyAudio(event.replyToken,{ | ||
| 125 | - "originalContentUrl": "https://panguin.ml/speech.m4a", | ||
| 126 | - "duration": 24000 | ||
| 127 | - }).then(resolve).catch(reject); | ||
| 128 | - } | ||
| 129 | - } | ||
| 130 | - //번역은 한국어->영어 / 영어->한국어만 지원 | ||
| 131 | - else if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | ||
| 132 | - source = detect_body.langCode == 'ko' ? 'ko':'en'; | ||
| 133 | - target = source == 'ko' ? 'en':'ko'; | ||
| 134 | - //papago 번역 option | ||
| 135 | - var options = { | ||
| 136 | - url: translate_api_url, | ||
| 137 | - // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
| 138 | - form: {'source':source, 'target':target, 'text':event.message.text}, | ||
| 139 | headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | 108 | headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
| 140 | }; | 109 | }; |
| 141 | - | 110 | + //papago 언어 감지 |
| 142 | - // Naver Post API | 111 | + request.post(detect_options, function (error, response, body) { |
| 143 | - request.post(options, function(error, response, body){ | 112 | + console.log(response.statusCode); |
| 144 | - // Translate API Sucess | 113 | + if (!error && response.statusCode == 200) { |
| 145 | - if(!error && response.statusCode == 200){ | 114 | + var detect_body = JSON.parse(response.body); |
| 146 | - // JSON | 115 | + var source = ''; |
| 147 | - var objBody = JSON.parse(response.body); | 116 | + var target = ''; |
| 148 | - result.text = objBody.message.result.translatedText; | 117 | + var result = {type: 'text', text: ''}; |
| 149 | - //번역된 문자 audio로 저장 | 118 | + //언어 감지가 제대로 됐는지 확인 |
| 150 | - if(options.form.target=='ko'){ | 119 | + console.log(detect_body.langCode); |
| 151 | - let audio_options={ | 120 | + if (detect_options.form.query == '음성') { |
| 152 | - 'Text': result.text, | 121 | + console.log('audio streaming'); |
| 153 | - 'OutputFormat': 'mp3', | 122 | + if (!error && response.statusCode == 200) { |
| 154 | - 'VoiceId':'Amy', | 123 | + console.log(response.statusCode); |
| 155 | - "LanguageCode":'ko-KR' | 124 | + for_audio_client.replyAudio(event.replyToken, { |
| 156 | - }; | 125 | + "originalContentUrl": "https://panguin.ml/speech.m4a", |
| 126 | + "duration": 24000 | ||
| 127 | + }).then(resolve).catch(reject); | ||
| 128 | + } | ||
| 157 | } | 129 | } |
| 158 | - else if(options.form.target=='en'){ | 130 | + //번역은 한국어->영어 / 영어->한국어만 지원 |
| 159 | - let audio_options={ | 131 | + else if (detect_body.langCode == 'ko' || detect_body.langCode == 'en') { |
| 160 | - 'Text': result.text, | 132 | + source = detect_body.langCode == 'ko' ? 'ko' : 'en'; |
| 161 | - 'OutputFormat': 'mp3', | 133 | + target = source == 'ko' ? 'en' : 'ko'; |
| 162 | - 'VoiceId':'Amy', | 134 | + //papago 번역 option |
| 163 | - "LanguageCode":'en-US' | 135 | + var options = { |
| 136 | + url: translate_api_url, | ||
| 137 | + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
| 138 | + form: {'source': source, 'target': target, 'text': event.message.text}, | ||
| 139 | + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
| 164 | }; | 140 | }; |
| 165 | - } | 141 | + |
| 166 | - Polly.synthesizeSpeech(audio_options, (err, data) => { | 142 | + // Naver Post API |
| 167 | - console.log("check"); | 143 | + request.post(options, function (error, response, body) { |
| 168 | - if (err) { | 144 | + // Translate API Sucess |
| 169 | - throw err; | 145 | + if (!error && response.statusCode == 200) { |
| 170 | - } else if (data) { | 146 | + // JSON |
| 171 | - if (data.AudioStream instanceof Buffer) { | 147 | + var objBody = JSON.parse(response.body); |
| 172 | - fs.writeFile("public/speech.m4a", data.AudioStream, function(err) { | 148 | + result.text = objBody.message.result.translatedText; |
| 149 | + //번역된 문자 audio로 저장 | ||
| 150 | + if (options.form.target == 'ko') { | ||
| 151 | + var audio_options = { | ||
| 152 | + 'Text': result.text, | ||
| 153 | + 'OutputFormat': 'mp3', | ||
| 154 | + 'VoiceId': 'Seoyeon', | ||
| 155 | + "LanguageCode": 'ko-KR' | ||
| 156 | + }; | ||
| 157 | + } else if (options.form.target == 'en') { | ||
| 158 | + var audio_options = { | ||
| 159 | + 'Text': result.text, | ||
| 160 | + 'OutputFormat': 'mp3', | ||
| 161 | + 'VoiceId': 'Kendra', | ||
| 162 | + "LanguageCode": 'en-US' | ||
| 163 | + }; | ||
| 164 | + } | ||
| 165 | + Polly.synthesizeSpeech(audio_options, (err, data) => { | ||
| 166 | + console.log("check"); | ||
| 173 | if (err) { | 167 | if (err) { |
| 174 | - return console.log(err); | 168 | + throw err; |
| 169 | + } else if (data) { | ||
| 170 | + if (data.AudioStream instanceof Buffer) { | ||
| 171 | + fs.writeFile("public/speech.m4a", data.AudioStream, function (err) { | ||
| 172 | + if (err) { | ||
| 173 | + return console.log(err); | ||
| 174 | + } | ||
| 175 | + console.log("The file was saved!"); | ||
| 176 | + }); | ||
| 177 | + } | ||
| 175 | } | 178 | } |
| 176 | - console.log("The file was saved!"); | ||
| 177 | }); | 179 | }); |
| 180 | + // Message 잘 찍히는지 확인 | ||
| 181 | + console.log(result.text); | ||
| 182 | + //번역된 문장 보내기 | ||
| 183 | + client.replyMessage(event.replyToken, result).then(resolve).catch(reject); | ||
| 178 | } | 184 | } |
| 179 | - } | 185 | + }); |
| 180 | - }); | 186 | + } |
| 181 | - // Message 잘 찍히는지 확인 | 187 | + // 메시지의 언어가 영어 또는 한국어가 아닐 경우 |
| 182 | - console.log(result.text); | 188 | + else { |
| 183 | - //번역된 문장 보내기 | 189 | + result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; |
| 184 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | 190 | + client.replyMessage(event.replyToken, result).then(resolve).catch(reject); |
| 185 | - } | 191 | + } |
| 186 | - }); | ||
| 187 | - } | ||
| 188 | - // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | ||
| 189 | - else{ | ||
| 190 | - result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | ||
| 191 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
| 192 | - } | ||
| 193 | 192 | ||
| 194 | - } | 193 | + } |
| 195 | 194 | ||
| 196 | - }); | 195 | + }); |
| 197 | 196 | ||
| 198 | - }); | 197 | + }); |
| 198 | + } | ||
| 199 | } | 199 | } |
| 200 | 200 | ||
| 201 | app.listen(80, function () { | 201 | app.listen(80, function () { | ... | ... |
-
Please register or login to post a comment