박찬수

Source Commit

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 = 'XOyIf8jsoQKq3b1zqxE4wawAoFU2Hz433AO3w8/ye+i6+2KrXpyfFwY0Dk/xhHQLPgtgPTiEP/m4IRW+SlVhdtzfH6c0Lfdw6nJ95QOugHfNWfviAmn5Uojh8LQJeAy21bvaNMCy11f+qgLSRnXmCgdB04t89/1O/w1cDnyilFU='
5 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
6 +const PAPAGO_ID = 'UZMyxEhDtcZQ4JNNeohy'
7 +const PAPAGO_SECRET = 'J2yR82NwYK'
8 +const fs = require('fs');
9 +const path = require('path');
10 +const HTTPS = require('https');
11 +const domain = "2018102191.osschatbot2022.tk"
12 +const sslport = 23023;
13 +const bodyParser = require('body-parser');
14 +
15 +var language = "en";
16 +
17 +var app = express();
18 +app.use(bodyParser.json());
19 +app.post('/hook', function (req, res) {
20 +
21 + var eventObj = req.body.events[0];
22 + var source = eventObj.source;
23 + var message = eventObj.message;
24 +
25 + // request log
26 + console.log('======================', new Date() ,'======================');
27 + console.log('[request]', req.body);
28 + console.log('[request source] ', eventObj.source);
29 + console.log('[request message]', eventObj.message);
30 +
31 + trans(eventObj.replyToken, eventObj.message.text);
32 +
33 +
34 + res.sendStatus(200);
35 +});
36 +
37 +function trans(replyToken, message) {
38 + console.log(message);
39 + switch(message)
40 + {
41 + case "영어":
42 + language = "en";
43 + console.log("영어로 변경");
44 + return;
45 + break;
46 + case "일본어":
47 + language = "ja";
48 + console.log("일본어로 변경");
49 + return;
50 + break;
51 + case "프랑스어":
52 + language = "fr";
53 + console.log("프랑스어로 변경");
54 + return;
55 + break;
56 + default:
57 + break;
58 + }
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=' + language + '&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 +try {
98 + const option = {
99 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
100 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
101 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
102 + };
103 +
104 + HTTPS.createServer(option, app).listen(sslport, () => {
105 + console.log(`[HTTPS] Server is started on port ${sslport}`);
106 + });
107 + } catch (error) {
108 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
109 + console.log(error);
110 + }
111 +
...\ No newline at end of file ...\ No newline at end of file