message.js
7.92 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var express = require('express');
var request = require('request');
var rssparser = require('rss-parser');
var bodyparser = require('body-parser')
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 translate_url = 'https://openapi.naver.com/v1/language/translate';
var trend_url = 'https://openapi.naver.com/v1/datalab/search';
var source = 'ko';
var target = 'en';
//일본어 ja , 중국어 간체 zh-CN , 중국어 번체 zh-TW
//what kind of task to do
var translate = false;
var trend = 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;
trend = false;
res.json({
"message": {
"text": "번역 언어를 설정합니다"
},
"keyboard": {
"type": "buttons",
"buttons":["한-영", "영-한","한-일","일-한","한-중국어간체","중국어간체-한","한-중국어번체",
"중국어번체-한"]
}
});
}else if (_obj.content == '번역'){
translate = true;
option = false;
trend = false;
res.json({
"message": {
"text": "입력한 텍스트를 번역해드리겠습니다. 자 시작!"
},
"keyboard": {
"type": "text"
}
});
}else if (_obj.content == '트랜드 검색'){
trend = true;
option = false;
translate = false;
res.json({
"message":{
"text": "검색어 트랜드를 시작합니다. 주제를 입력해주세요."
},
"keyboard": {
"type": "text"
}
});
}
else if (translate)
{
// Naver Papago Translate
var options = {
url: translate_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
},"keyboard": {
"type": "buttons",
"buttons":["언어설정", "번역","트랜드 검색"]
}
};
// 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
},
"keyboard": {
"type": "buttons",
"buttons":["언어설정", "번역","트랜드 검색"]
}
};
// Kakao에 Error Message
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(message));
}
});
}else if (trend)
{
var request_body = {
"startDate": "2018-09-01",
"endDate": "2018-12-01",
"timeUnit": "month",
"keywordGroups": [
{
"groupName": req.body.content,
"keywords": [
req.body.content
]
}
],
"device": "pc",
"ages": [
],
"gender": ""
};
request.post({
url: trend_url,
body: JSON.stringify(request_body),
headers: {
'X-Naver-Client-Id': client_id,
'X-Naver-Client-Secret': client_secret,
'Content-Type': 'application/json'
}
},
function (error, response, body) {
console.log(response.statusCode);
console.log(body);
if(!error && response.statusCode == 200){
var objBody = JSON.parse(response.body);
console.log(objBody);
let message = {
"message": {
"text": response.body
},"keyboard": {
"type": "buttons",
"buttons":["언어설정", "번역","트랜드 검색"]
}
};
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(message));
}
else{
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
let message = {
"message": {
"text": response.statusCode
},"keyboard": {
"type": "buttons",
"buttons":["언어설정", "번역","트랜드 검색"]
}
};
res.set({
'content-type': 'application/json'
}).send(JSON.stringify(message));
}
});
}
/*else if (dictionary)
{
var reqURL = 'https://openapi.naver.com/v1/search/encyc.json?query=' + encodeURI(req.query.query)
+ '&display=3&start=1';
var options = {
url: reqURL,
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) {
let parser = new rssparser();
(async () => {
let feed = await parser.parseURL(reqURL);
var title = feed.items.channel.title;
var description = feed.items.channel.items.description;
var link = feed.items.channel.items.link;
}) ();
//var objBody = JSON.parse(response.body);
let message = {
"message": {
"text": title + " : " + description
},
"message_button": {
"label": "백과사전에서 직접보기",
"url": 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";
}
else if (_obj.content == "한-일")
{
source = "ko";
target = "ja";
}
else if (_obj.content == "일-한")
{
source = "ja";
target = "ko";
}
else if (_obj.content == "한-중국어간체")
{
source = "ko";
target = "zh-CN";
}
else if (_obj.content == "중국어간체-한")
{
source = "zh-CN";
target = "ko";
}
else if (_obj.content == "한-중국어번체")
{
source = "ko";
target = "zh-TW";
}
else if (_obj.content == "중국어번체-한")
{
source = "zh-TW";
target = "ko";
}
console.log(source, target);
res.json({
"message": {
"text": source + "에서" + target + "으로 번역합니다."
},
"keyboard": {
"type": "buttons",
"buttons":["언어설정", "번역","트랜드 검색"]
}
});
}
});
module.exports = app;