Showing
1 changed file
with
27 additions
and
2 deletions
1 | +// Naver WEB Request | ||
1 | var request = require('request'); | 2 | var request = require('request'); |
3 | + | ||
4 | +// Web Package | ||
2 | var express = require('express'); | 5 | var express = require('express'); |
3 | var app = express(); | 6 | var app = express(); |
4 | 7 | ||
8 | +// Kakao Parser | ||
5 | var bodyParser = require('body-parser'); | 9 | var bodyParser = require('body-parser'); |
6 | 10 | ||
11 | +// Naver API URL | ||
7 | var api_url = 'https://openapi.naver.com/v1/papago/n2mt'; | 12 | var api_url = 'https://openapi.naver.com/v1/papago/n2mt'; |
8 | 13 | ||
14 | +// Naver Auth Key | ||
9 | var client_id = '86rKmat0DijccSxKa01P'; | 15 | var client_id = '86rKmat0DijccSxKa01P'; |
10 | var client_secret = 'rMapNjB8DP'; | 16 | var client_secret = 'rMapNjB8DP'; |
17 | + | ||
18 | +// Parse Application JSON | ||
11 | app.use(bodyParser.json()); | 19 | app.use(bodyParser.json()); |
20 | + | ||
21 | +// Parse Application/x-www-form-urlencoded | ||
12 | app.use(bodyParser.urlencoded({ extended: true})); | 22 | app.use(bodyParser.urlencoded({ extended: true})); |
13 | 23 | ||
24 | +// Kakao Keyboard API | ||
14 | app.get('/keyboard', function(req, res) { | 25 | app.get('/keyboard', function(req, res) { |
15 | const menu = { | 26 | const menu = { |
16 | "type": 'buttons', | 27 | "type": 'buttons', |
... | @@ -21,6 +32,7 @@ app.get('/keyboard', function(req, res) { | ... | @@ -21,6 +32,7 @@ app.get('/keyboard', function(req, res) { |
21 | }).send(JSON.stringify(menu)); | 32 | }).send(JSON.stringify(menu)); |
22 | }); | 33 | }); |
23 | 34 | ||
35 | +// Kakao Message API | ||
24 | app.post('/message', function(req, res) { | 36 | app.post('/message', function(req, res) { |
25 | const _obj = { | 37 | const _obj = { |
26 | user_key: req.body.user_key, | 38 | user_key: req.body.user_key, |
... | @@ -30,26 +42,37 @@ app.post('/message', function(req, res) { | ... | @@ -30,26 +42,37 @@ app.post('/message', function(req, res) { |
30 | 42 | ||
31 | console.log(_obj.content) | 43 | console.log(_obj.content) |
32 | 44 | ||
45 | + // Naver Papago Translate | ||
33 | var options = { | 46 | var options = { |
34 | url: api_url, | 47 | url: api_url, |
48 | + // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
35 | form: {'source':'ko', 'target':'en', 'text':req.body.content}, | 49 | form: {'source':'ko', 'target':'en', 'text':req.body.content}, |
36 | headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | 50 | headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} |
37 | }; | 51 | }; |
52 | + | ||
53 | + // Naver Post API | ||
38 | request.post(options, function(error, response, body){ | 54 | request.post(options, function(error, response, body){ |
55 | + // Translate API Sucess | ||
39 | if(!error && response.statusCode == 200){ | 56 | if(!error && response.statusCode == 200){ |
57 | + // JSON | ||
40 | var objBody = JSON.parse(response.body); | 58 | var objBody = JSON.parse(response.body); |
59 | + // Message 잘 찍히는지 확인 | ||
41 | console.log(objBody.message.result.translatedText); | 60 | console.log(objBody.message.result.translatedText); |
42 | - | 61 | + |
62 | + // Kakao Message API | ||
43 | let massage = { | 63 | let massage = { |
44 | "message": { | 64 | "message": { |
65 | + // Naver API Translate 결과를 Kakao Message | ||
45 | "text": objBody.message.result.translatedText | 66 | "text": objBody.message.result.translatedText |
46 | }, | 67 | }, |
47 | }; | 68 | }; |
48 | - | 69 | + |
70 | + // Kakao Message API 전송 | ||
49 | res.set({ | 71 | res.set({ |
50 | 'content-type': 'application/json' | 72 | 'content-type': 'application/json' |
51 | }).send(JSON.stringify(massage)); | 73 | }).send(JSON.stringify(massage)); |
52 | }else{ | 74 | }else{ |
75 | + // Naver Message Error 발생 | ||
53 | res.status(response.statusCode).end(); | 76 | res.status(response.statusCode).end(); |
54 | console.log('error = ' + response.statusCode); | 77 | console.log('error = ' + response.statusCode); |
55 | 78 | ||
... | @@ -59,6 +82,7 @@ app.post('/message', function(req, res) { | ... | @@ -59,6 +82,7 @@ app.post('/message', function(req, res) { |
59 | }, | 82 | }, |
60 | }; | 83 | }; |
61 | 84 | ||
85 | + // Kakao에 Error Message | ||
62 | res.set({ | 86 | res.set({ |
63 | 'content-type': 'application/json' | 87 | 'content-type': 'application/json' |
64 | }).send(JSON.stringify(massage)); | 88 | }).send(JSON.stringify(massage)); |
... | @@ -66,5 +90,6 @@ app.post('/message', function(req, res) { | ... | @@ -66,5 +90,6 @@ app.post('/message', function(req, res) { |
66 | }); | 90 | }); |
67 | }); | 91 | }); |
68 | 92 | ||
93 | +// whatsupdevelop.com 실행 | ||
69 | app.listen(80, function(){ | 94 | app.listen(80, function(){ |
70 | }); | 95 | }); | ... | ... |
-
Please register or login to post a comment