trans.js
2.72 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
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_ID = 'r_kuebFKCqBeL3SI_dFb'
const PAPAGO_SECRET = 'Wdz2tudrzB'
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "www.chatbotshin.tk"
const sslport = 23023;
const bodyParser = require('body-parser');
// language identification & translation
function trans_lng_selector(){
if(eventObj.message.text=='/번역 영어' || eventObj.message.text=='/번역 프랑스어' || eventObj.message.text=='/번역 일본어'){
//trans_trigger = 0 or 1;
trans_change(eventObj.message.text);
trans_trigger = 1;
console.log('[trans_dest]', trans_dest);
}
}
function translating(replyToken, message) {
request.post(
{
url: PAPAGO_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'source=ko&' + trans_dest + '&text=' + message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log(body.message);
var transMessage = body.message.result.translatedText;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":replyToken,
"messages":[
{
"type":"text",
"text":transMessage
}
]
}
},(error, response, body) => {
console.log(body)
});
}
});
}
// language index identification
function trans_change(index){
if(index == '/번역 영어'){
trans_dest = 'target=en';
}
else if(index == '/번역 일본어'){
trans_dest = 'target=ja';
}
else if(index == '/번역 프랑스어'){
trans_dest = 'target=fr';
}
}
module.exports.translating = translating;
module.exports.trans_lng_selector = trans_lng_selector;
module.exports.trans_change = trans_change;