육현진

trens/

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 = 'xAcMgunmVCPFumwQa+XlStTaxrW8Jjt3PJbulC/qoG4vDiMaJ4vJDWroCMYLzqDy7svDnUJvfdanKUQDn+yZgLh4axZcjUClOGZBWujleiD9QIMn/7l0YJkxmz1oCPlmIXb/z96Q0h3rINwxx6MxbQdB04t89/1O/w1cDnyilFU='
5 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
6 +const PAPAGO_ID = '0yyRWAtqJot6VF9yf3To'
7 +const PAPAGO_SECRET = 'E7ecJinp0f'
8 +const fs = require('fs');
9 +const path = require('path');
10 +const HTTPS = require('https');
11 +const domain = "www.osstest78910.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 +
28 + trans(eventObj.replyToken, eventObj.message.text);
29 +
30 +
31 + res.sendStatus(200);
32 +});
33 +
34 +function trans(replyToken, message) {
35 +
36 + request.post(
37 + {
38 + url: PAPAGO_URL,
39 + headers: {
40 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
41 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
42 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
43 + },
44 + body: 'source=ko&target=en&text=' + message,
45 + json:true
46 + },(error, response, body) => {
47 + if(!error && response.statusCode == 200) {
48 + console.log(body.message);
49 + var transMessage = body.message.result.translatedText;
50 + request.post(
51 + {
52 + url: TARGET_URL,
53 + headers: {
54 + 'Authorization': `Bearer ${TOKEN}`
55 + },
56 + json: {
57 + "replyToken":replyToken,
58 + "messages":[
59 + {
60 + "type":"text",
61 + "text":transMessage
62 + }
63 + ]
64 + }
65 + },(error, response, body) => {
66 + console.log(body)
67 + });
68 + }
69 + });
70 +
71 +}
72 +
73 +try {
74 + const option = {
75 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
76 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
77 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
78 + };
79 +
80 + HTTPS.createServer(option, app).listen(sslport, () => {
81 + console.log(`[HTTPS] Server is started on port ${sslport}`);
82 + });
83 + } catch (error) {
84 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
85 + console.log(error);
86 + }
87 +
...\ 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 +}