message.js
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var express = require('express');
var request = require('request');
var app = express.Router();
// Naver Auth Key // to do)) get a new Auth key
var client_id = 'is216aNtQ6QLNkWW03bc';
var client_secret = 'Ot0BMdWXOJ';
// Naver API URL
var api_url = 'https://openapi.naver.com/v1/language/translate';
var source = 'ko';
var target = 'en';
//what kind of task to do
var translate = false;
var dictionary = false;
var option = false;
// Kakao Message API
app.post('/', function(req, res) {
var _obj = {
user_key: req.body.user_key,
type: req.body.type,
content: req.body.content
};
if(_obj.content == '설정'){
option = true;
translate = false;
dictionary = false;
res.json({
"message": {
"text": "언어를 설정합니다"
},
"keyboard": {
"type": "buttons",
"buttons":["한-영", "영-한"]
}
});
}else if (_obj.content == '번역'){
translate = true;
option = false;
dictionary = false;
res.json({
"message": {
"text": "입력한 텍스트를 번역해드리겠습니다. 자 시작!"
},
"keyboard": {
"type": "text"
}
});
}else if (_obj.content == '사전'){
dictionary = true;
option = false;
translate = false;
res.json({
"message":{
"text": "백과사전 검색을 시작합니다. 키워드를 입력해주세요."
},
"keyboard": {
"type": "text"
}
});
}
else if (translate)
{
// Naver Papago Translate
var options = {
url: api_url,
// (source : 번역할 대상), (target: 번역후 언어), 카톡에서 받는 메시지(text)
form: {'source':source, 'target':target, 'text':req.body.content},
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
// Naver Post API
request.post(options, function(error, response, body){
// Translate API Sucess
if(!error && response.statusCode == 200){
// JSON
var objBody = JSON.parse(response.body);
// Message 잘 찍히는지 확인
console.log(objBody.message.result.translatedText);
// Kakao Message API
let message = {
"message": {
// Naver API Translate 결과를 Kakao Message
"text": objBody.message.result.translatedText
}
};
// Kakao Message API 전송
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(message));
}
else{
// Naver Message Error 발생
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
let message = {
"message": {
"text": response.statusCode
},
};
// Kakao에 Error Message
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(message));
}
});
}
else if (dictionary)
{
var options = {
url: 'https://openapi.naver.com/v1/search/encyc.json?query=' + encodeURI(req.query.query)
+ '&display=3&start=1',
headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var objBody = JSON.parse(response.body);
let message = {
"message": {
"text": objBody.title + " : " + objBody.description
},
"message_button": {
"label": "백과사전에서 직접보기",
"url": objBody.link
}
};
}
else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
}
else if (option)
{
if (_obj.content == "한-영")
{
source = 'ko';
target = 'en';
}
else if (_obj.content == "영-한")
{
source = "en";
target = "ko";
}
console.log(source, target);
res.json({
"message": {
"text": source + "에서" + target + "으로 번역합니다."
},
"keyboard": {
"type": "buttons",
"buttons":["설정", "번역","사전"]
}
});
}
});
function choiceLanguage(str) {
if (str == "영어")
return "en";
else if (str == "한국어") {
console.log(str);
return "ko";
}
else if (str == "일본어")
return "ja";
else if (str == "중국어(간체)")
return "zh-CN";
else
return "ko";
}
module.exports = app;