Showing
3 changed files
with
187 additions
and
0 deletions
trans/app.js
0 → 100644
1 | +var express = require('express'); | ||
2 | +const request = require('request'); | ||
3 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
4 | +const TOKEN = 'QawFJWyymPze0BzAAVSZ7aB/brrf23LVyHguMf1P1mJ5HKAL79uN4IyaoZl4nBZuVlkkjD/Zj/fxxDSy/drfv6J0XVRRojEAKWnk4kiYlZWKZrJeSmI9vK85QlBC/a9gJ8zAbswWnSoR0chjzVdcNwdB04t89/1O/w1cDnyilFU=' | ||
5 | +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | ||
6 | +const PAPAGO_ID = 'JiCuM2e2v51fRTMC1DyE' | ||
7 | +const PAPAGO_SECRET = 'V9PycDDwo8' | ||
8 | +const fs = require('fs'); | ||
9 | +const path = require('path'); | ||
10 | +const HTTPS = require('https'); | ||
11 | +const domain = "www.osstestdw.ml" | ||
12 | +const sslport = 23023; | ||
13 | +const bodyParser = require('body-parser'); | ||
14 | +var app = express(); | ||
15 | +app.use(bodyParser.json()); | ||
16 | +app.post('/hook', function (req, res) { | ||
17 | + | ||
18 | + var eventObj = req.body.events[0]; | ||
19 | + var source = eventObj.source; | ||
20 | + var message = eventObj.message; | ||
21 | + | ||
22 | + // request log | ||
23 | + console.log('======================', new Date() ,'======================'); | ||
24 | + console.log('[request]', req.body); | ||
25 | + console.log('[request source] ', eventObj.source); | ||
26 | + console.log('[request message]', eventObj.message); | ||
27 | + if(message == "영어"){ | ||
28 | + transen(eventObj.replyToken, eventObj.message.text); | ||
29 | + } | ||
30 | + else if(message == "일본어"){ | ||
31 | + transja(eventObj.replyToken, eventObj.message.text); | ||
32 | + } | ||
33 | + else if(message == "프랑스어"){ | ||
34 | + transfr(eventObj.replyToken, eventObj.message.text); | ||
35 | + } | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | + | ||
40 | + res.sendStatus(200); | ||
41 | +}); | ||
42 | + | ||
43 | +function transen(replyToken, message) { | ||
44 | + | ||
45 | + request.post( | ||
46 | + { | ||
47 | + url: PAPAGO_URL, | ||
48 | + headers: { | ||
49 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
50 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
51 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
52 | + }, | ||
53 | + body: 'source=ko&target=en&text=' + message, | ||
54 | + json:true | ||
55 | + },(error, response, body) => { | ||
56 | + if(!error && response.statusCode == 200) { | ||
57 | + console.log(body.message); | ||
58 | + var transMessage = body.message.result.translatedText; | ||
59 | + request.post( | ||
60 | + { | ||
61 | + url: TARGET_URL, | ||
62 | + headers: { | ||
63 | + 'Authorization': `Bearer ${TOKEN}` | ||
64 | + }, | ||
65 | + json: { | ||
66 | + "replyToken":replyToken, | ||
67 | + "messages":[ | ||
68 | + { | ||
69 | + "type":"text", | ||
70 | + "text":transMessage | ||
71 | + } | ||
72 | + ] | ||
73 | + } | ||
74 | + },(error, response, body) => { | ||
75 | + console.log(body) | ||
76 | + }); | ||
77 | + } | ||
78 | + }); | ||
79 | + | ||
80 | +} | ||
81 | +function transfr(replyToken, message) { | ||
82 | + | ||
83 | + request.post( | ||
84 | + { | ||
85 | + url: PAPAGO_URL, | ||
86 | + headers: { | ||
87 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
88 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
89 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
90 | + }, | ||
91 | + body: 'source=ko&target=fr&text=' + message, | ||
92 | + json:true | ||
93 | + },(error, response, body) => { | ||
94 | + if(!error && response.statusCode == 200) { | ||
95 | + console.log(body.message); | ||
96 | + var transMessage = body.message.result.translatedText; | ||
97 | + request.post( | ||
98 | + { | ||
99 | + url: TARGET_URL, | ||
100 | + headers: { | ||
101 | + 'Authorization': `Bearer ${TOKEN}` | ||
102 | + }, | ||
103 | + json: { | ||
104 | + "replyToken":replyToken, | ||
105 | + "messages":[ | ||
106 | + { | ||
107 | + "type":"text", | ||
108 | + "text":transMessage | ||
109 | + } | ||
110 | + ] | ||
111 | + } | ||
112 | + },(error, response, body) => { | ||
113 | + console.log(body) | ||
114 | + }); | ||
115 | + } | ||
116 | + }); | ||
117 | + | ||
118 | +} | ||
119 | +function transja(replyToken, message) { | ||
120 | + | ||
121 | + request.post( | ||
122 | + { | ||
123 | + url: PAPAGO_URL, | ||
124 | + headers: { | ||
125 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
126 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
127 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
128 | + }, | ||
129 | + body: 'source=ko&target=ja&text=' + message, | ||
130 | + json:true | ||
131 | + },(error, response, body) => { | ||
132 | + if(!error && response.statusCode == 200) { | ||
133 | + console.log(body.message); | ||
134 | + var transMessage = body.message.result.translatedText; | ||
135 | + request.post( | ||
136 | + { | ||
137 | + url: TARGET_URL, | ||
138 | + headers: { | ||
139 | + 'Authorization': `Bearer ${TOKEN}` | ||
140 | + }, | ||
141 | + json: { | ||
142 | + "replyToken":replyToken, | ||
143 | + "messages":[ | ||
144 | + { | ||
145 | + "type":"text", | ||
146 | + "text":transMessage | ||
147 | + } | ||
148 | + ] | ||
149 | + } | ||
150 | + },(error, response, body) => { | ||
151 | + console.log(body) | ||
152 | + }); | ||
153 | + } | ||
154 | + }); | ||
155 | + | ||
156 | +} | ||
157 | + | ||
158 | +try { | ||
159 | + const option = { | ||
160 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
161 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
162 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
163 | + }; | ||
164 | + | ||
165 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
166 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
167 | + }); | ||
168 | + } catch (error) { | ||
169 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
170 | + console.log(error); | ||
171 | + } | ||
172 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
trans/package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
trans/package.json
0 → 100644
1 | +{ | ||
2 | + "name": "trans", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "app.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "author": "", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "express": "^4.17.1", | ||
13 | + "request": "^2.88.2" | ||
14 | + } | ||
15 | +} |
-
Please register or login to post a comment