최원섭

[Develop] Naver Papago SMT Translate Update

Showing 1 changed file with 38 additions and 8 deletions
1 +var request = require('request');
1 var express = require('express'); 2 var express = require('express');
2 var app = express(); 3 var app = express();
3 4
4 var bodyParser = require('body-parser'); 5 var bodyParser = require('body-parser');
5 6
7 +var api_url = 'https://openapi.naver.com/v1/papago/n2mt';
8 +
9 +var client_id = '86rKmat0DijccSxKa01P';
10 +var client_secret = 'rMapNjB8DP';
6 app.use(bodyParser.json()); 11 app.use(bodyParser.json());
7 app.use(bodyParser.urlencoded({ extended: true})); 12 app.use(bodyParser.urlencoded({ extended: true}));
8 13
...@@ -24,16 +29,41 @@ app.post('/message', function(req, res) { ...@@ -24,16 +29,41 @@ app.post('/message', function(req, res) {
24 }; 29 };
25 30
26 console.log(_obj.content) 31 console.log(_obj.content)
27 - 32 +
28 - let massage = { 33 + var options = {
29 - "message": { 34 + url: api_url,
30 - "text": _obj.content 35 + form: {'source':'ko', 'target':'en', 'text':req.body.content},
31 - }, 36 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
32 }; 37 };
38 + request.post(options, function(error, response, body){
39 + if(!error && response.statusCode == 200){
40 + var objBody = JSON.parse(response.body);
41 + console.log(objBody.message.result.translatedText);
42 +
43 + let massage = {
44 + "message": {
45 + "text": objBody.message.result.translatedText
46 + },
47 + };
33 48
34 - res.set({ 49 + res.set({
35 - 'content-type': 'application/json' 50 + 'content-type': 'application/json'
36 - }).send(JSON.stringify(massage)); 51 + }).send(JSON.stringify(massage));
52 + }else{
53 + res.status(response.statusCode).end();
54 + console.log('error = ' + response.statusCode);
55 +
56 + let massage = {
57 + "message": {
58 + "text": response.statusCode
59 + },
60 + };
61 +
62 + res.set({
63 + 'content-type': 'application/json'
64 + }).send(JSON.stringify(massage));
65 + }
66 + });
37 }); 67 });
38 68
39 app.listen(80, function(){ 69 app.listen(80, function(){
......