최원섭

[Develop] Naver Papago SMT Translate Update

Showing 1 changed file with 31 additions and 1 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
...@@ -25,15 +30,40 @@ app.post('/message', function(req, res) { ...@@ -25,15 +30,40 @@ app.post('/message', function(req, res) {
25 30
26 console.log(_obj.content) 31 console.log(_obj.content)
27 32
33 + var options = {
34 + url: api_url,
35 + form: {'source':'ko', 'target':'en', 'text':req.body.content},
36 + headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
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 + };
48 +
49 + res.set({
50 + 'content-type': 'application/json'
51 + }).send(JSON.stringify(massage));
52 + }else{
53 + res.status(response.statusCode).end();
54 + console.log('error = ' + response.statusCode);
55 +
28 let massage = { 56 let massage = {
29 "message": { 57 "message": {
30 - "text": _obj.content 58 + "text": response.statusCode
31 }, 59 },
32 }; 60 };
33 61
34 res.set({ 62 res.set({
35 'content-type': 'application/json' 63 'content-type': 'application/json'
36 }).send(JSON.stringify(massage)); 64 }).send(JSON.stringify(massage));
65 + }
66 + });
37 }); 67 });
38 68
39 app.listen(80, function(){ 69 app.listen(80, function(){
......