김대욱

new2

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 kind = eventObj.kind;
21 + var message = eventObj.message;
22 +
23 + // request log
24 + console.log('======================', new Date() ,'======================');
25 + console.log('[request]', req.body);
26 + console.log('[request source] ', eventObj.source);
27 + console.log('[request message]', eventObj.message);
28 + const readline = require('readline');
29 +
30 + const rl = readline.createInterface({
31 + input: process.stdin,
32 + output: process.stdout
33 + });
34 +
35 + rl.on('line', function(line) {
36 + console.log(line);
37 +
38 + rl.close();
39 + }).on("close", function() {
40 + process.exit();
41 + });
42 +
43 + if(line =="영어"){
44 + transen(eventObj.replyToken, eventObj.message.text);
45 + }
46 + else if(line =="프랑스어"){
47 + transfr(eventObj.replyToken, eventObj.message.text);
48 + }
49 + else if(line =="일본어"){
50 + transja(eventObj.replyToken, eventObj.message.text);
51 + }
52 +
53 +
54 +
55 + res.sendStatus(200);
56 +});
57 +
58 +function transen(replyToken, message) {
59 +
60 + request.post(
61 + {
62 + url: PAPAGO_URL,
63 + headers: {
64 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
65 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
66 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
67 + },
68 + body: 'source=ko&target=en&text=' + message,
69 + json:true
70 + },(error, response, body) => {
71 + if(!error && response.statusCode == 200) {
72 + console.log(body.message);
73 + var transMessage = body.message.result.translatedText;
74 + request.post(
75 + {
76 + url: TARGET_URL,
77 + headers: {
78 + 'Authorization': `Bearer ${TOKEN}`
79 + },
80 + json: {
81 + "replyToken":replyToken,
82 + "messages":[
83 + {
84 + "type":"text",
85 + "text":transMessage
86 + }
87 + ]
88 + }
89 + },(error, response, body) => {
90 + console.log(body)
91 + });
92 + }
93 + });
94 +
95 +}
96 +
97 +function transfr(replyToken, message) {
98 +
99 + request.post(
100 + {
101 + url: PAPAGO_URL,
102 + headers: {
103 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
104 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
105 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
106 + },
107 + body: 'source=ko&target=fr&text=' + message,
108 + json:true
109 + },(error, response, body) => {
110 + if(!error && response.statusCode == 200) {
111 + console.log(body.message);
112 + var transMessage = body.message.result.translatedText;
113 + request.post(
114 + {
115 + url: TARGET_URL,
116 + headers: {
117 + 'Authorization': `Bearer ${TOKEN}`
118 + },
119 + json: {
120 + "replyToken":replyToken,
121 + "messages":[
122 + {
123 + "type":"text",
124 + "text":transMessage
125 + }
126 + ]
127 + }
128 + },(error, response, body) => {
129 + console.log(body)
130 + });
131 + }
132 + });
133 +
134 +}
135 +
136 +function transja(replyToken, message) {
137 +
138 + request.post(
139 + {
140 + url: PAPAGO_URL,
141 + headers: {
142 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
143 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
144 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
145 + },
146 + body: 'source=ko&target=ja&text=' + message,
147 + json:true
148 + },(error, response, body) => {
149 + if(!error && response.statusCode == 200) {
150 + console.log(body.message);
151 + var transMessage = body.message.result.translatedText;
152 + request.post(
153 + {
154 + url: TARGET_URL,
155 + headers: {
156 + 'Authorization': `Bearer ${TOKEN}`
157 + },
158 + json: {
159 + "replyToken":replyToken,
160 + "messages":[
161 + {
162 + "type":"text",
163 + "text":transMessage
164 + }
165 + ]
166 + }
167 + },(error, response, body) => {
168 + console.log(body)
169 + });
170 + }
171 + });
172 +
173 +}
174 +
175 +try {
176 + const option = {
177 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
178 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
179 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
180 + };
181 +
182 + HTTPS.createServer(option, app).listen(sslport, () => {
183 + console.log(`[HTTPS] Server is started on port ${sslport}`);
184 + });
185 + } catch (error) {
186 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
187 + console.log(error);
188 + }
189 +
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
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 +}