박태권

Update app.js

1 -//http://khuhub.khu.ac.kr/2021105602/menu_recommand_webpage.git 1 +var lenguage = 'not';
2 var express = require('express'); 2 var express = require('express');
3 const request = require('request'); 3 const request = require('request');
4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 4 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
5 -const TOKEN = '채널 토큰' 5 +const TOKEN = 'Ur8nbk2bQAc9KavLSSJv0uzlKWCxCBnOJOKyqbRS1mMHWgJlEOPfb7v+X1DDEHocUmhU7KnPFN+KHt5S3SYd5QmF42aTeQpW2gDVTjyqty1Tn+p6QqEhI/uSz659PBHwczEYn0Plx+akfCNHDPJRpAdB04t89/1O/w1cDnyilFU='
6 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' 6 const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
7 -const PAPAGO_ID = '파파고 ID' 7 +const PAPAGO_ID = '1Z83W3lUGLqnNbrDAk8A'
8 -const PAPAGO_SECRET = '파파고 Client Secret' 8 +const PAPAGO_SECRET = 'ST7ElRPFPP'
9 const fs = require('fs'); 9 const fs = require('fs');
10 const path = require('path'); 10 const path = require('path');
11 const HTTPS = require('https'); 11 const HTTPS = require('https');
12 -const domain = "도메인 명" 12 +const domain = "2021105602.osschatbot2022.tk"
13 const sslport = 23023; 13 const sslport = 23023;
14 const bodyParser = require('body-parser'); 14 const bodyParser = require('body-parser');
15 +const res = require('express/lib/response');
15 var app = express(); 16 var app = express();
16 -app.use(bodyParser.json()); 17 +do
17 -app.post('/hook', function (req, res) { 18 +{
18 - var eventObj = req.body.events[0]; 19 + app.use(bodyParser.json());
19 - var source = eventObj.source; 20 + app.post('/hook', function (req, res) {
20 - var message = eventObj.message; 21 +
21 - // request log 22 + var eventObj = req.body.events[0];
22 - console.log('======================', new Date() ,'======================'); 23 + var source = eventObj.source;
23 - console.log('[request]', req.body); 24 + var message = eventObj.message;
24 - console.log('[request source] ', eventObj.source); 25 +
25 - console.log('[request message]', eventObj.message); 26 + // request log
26 - trans(eventObj.replyToken, eventObj.message.text); 27 + console.log('======================', new Date() ,'======================');
28 + console.log('[request]', req.body);
29 + console.log('[request source] ', eventObj.source);
30 + console.log('[request message]', eventObj.message);
31 +
32 + if(eventObj.message.text=='영어')
33 + {
34 + lenguage='en';
35 + }else if(eventObj.message.text=='일본어')
36 + {
37 + lenguage = 'ja'
38 + }else if(eventObj.message.text=='프랑스어')
39 + {
40 + lenguage = 'fr'
41 + }else
42 + {
43 + if(lenguage == 'en')
44 + {
45 + trans_en(eventObj.replyToken, eventObj.message.text);
46 + }else if(lenguage == 'ja')
47 + {
48 + trans_ja(eventObj.replyToken, eventObj.message.text);
49 + }else if(lenguage == 'fr')
50 + {
51 + trans_fr(eventObj.replyToken, eventObj.message.text);
52 + }else
53 + {
54 + trans_en(eventObj.replyToken, '오류');
55 + }
56 + }
27 57
28 - res.sendStatus(200); 58 +
29 -}); 59 + res.sendStatus(200);
30 -function trans(replyToken, message) { 60 + });
61 +}while(lenguage=='en' | lenguage == 'fr' | lenguage == 'ja');
62 +
63 +function trans_en(replyToken, message) {
64 +
31 request.post( 65 request.post(
32 { 66 {
33 url: PAPAGO_URL, 67 url: PAPAGO_URL,
...@@ -62,7 +96,88 @@ function trans(replyToken, message) { ...@@ -62,7 +96,88 @@ function trans(replyToken, message) {
62 }); 96 });
63 } 97 }
64 }); 98 });
99 +
100 +}
101 +
102 +function trans_ja(replyToken, message) {
103 +
104 + request.post(
105 + {
106 + url: PAPAGO_URL,
107 + headers: {
108 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
109 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
110 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
111 + },
112 + body: 'source=ko&target=ja&text=' + message,
113 + json:true
114 + },(error, response, body) => {
115 + if(!error && response.statusCode == 200) {
116 + console.log(body.message);
117 + var transMessage = body.message.result.translatedText;
118 + request.post(
119 + {
120 + url: TARGET_URL,
121 + headers: {
122 + 'Authorization': `Bearer ${TOKEN}`
123 + },
124 + json: {
125 + "replyToken":replyToken,
126 + "messages":[
127 + {
128 + "type":"text",
129 + "text":transMessage
130 + }
131 + ]
132 + }
133 + },(error, response, body) => {
134 + console.log(body)
135 + });
136 + }
137 + });
138 +
139 +}
140 +
141 +function trans_fr(replyToken, message) {
142 +
143 + request.post(
144 + {
145 + url: PAPAGO_URL,
146 + headers: {
147 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
148 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
149 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
150 + },
151 + body: 'source=ko&target=fr&text=' + message,
152 + json:true
153 + },(error, response, body) => {
154 + if(!error && response.statusCode == 200) {
155 + console.log(body.message);
156 + var transMessage = body.message.result.translatedText;
157 + request.post(
158 + {
159 + url: TARGET_URL,
160 + headers: {
161 + 'Authorization': `Bearer ${TOKEN}`
162 + },
163 + json: {
164 + "replyToken":replyToken,
165 + "messages":[
166 + {
167 + "type":"text",
168 + "text":transMessage
169 + }
170 + ]
171 + }
172 + },(error, response, body) => {
173 + console.log(body)
174 + });
175 + }
176 + });
177 +
65 } 178 }
179 +
180 +
66 try { 181 try {
67 const option = { 182 const option = {
68 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 183 ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
......