Showing
1 changed file
with
80 additions
and
0 deletions
CHATBOT/app.js
0 → 100644
1 | +//http://khuhub.khu.ac.kr/2021105602/menu_recommand_webpage.git | ||
2 | +var express = require('express'); | ||
3 | +const request = require('request'); | ||
4 | +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' | ||
5 | +const TOKEN = '채널 토큰' | ||
6 | +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | ||
7 | +const PAPAGO_ID = '파파고 ID' | ||
8 | +const PAPAGO_SECRET = '파파고 Client Secret' | ||
9 | +const fs = require('fs'); | ||
10 | +const path = require('path'); | ||
11 | +const HTTPS = require('https'); | ||
12 | +const domain = "도메인 명" | ||
13 | +const sslport = 23023; | ||
14 | +const bodyParser = require('body-parser'); | ||
15 | +var app = express(); | ||
16 | +app.use(bodyParser.json()); | ||
17 | +app.post('/hook', function (req, res) { | ||
18 | + var eventObj = req.body.events[0]; | ||
19 | + var source = eventObj.source; | ||
20 | + var message = eventObj.message; | ||
21 | + // request log | ||
22 | + console.log('======================', new Date() ,'======================'); | ||
23 | + console.log('[request]', req.body); | ||
24 | + console.log('[request source] ', eventObj.source); | ||
25 | + console.log('[request message]', eventObj.message); | ||
26 | + trans(eventObj.replyToken, eventObj.message.text); | ||
27 | + | ||
28 | + res.sendStatus(200); | ||
29 | +}); | ||
30 | +function trans(replyToken, message) { | ||
31 | + request.post( | ||
32 | + { | ||
33 | + url: PAPAGO_URL, | ||
34 | + headers: { | ||
35 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
36 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
37 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
38 | + }, | ||
39 | + body: 'source=ko&target=en&text=' + message, | ||
40 | + json:true | ||
41 | + },(error, response, body) => { | ||
42 | + if(!error && response.statusCode == 200) { | ||
43 | + console.log(body.message); | ||
44 | + var transMessage = body.message.result.translatedText; | ||
45 | + request.post( | ||
46 | + { | ||
47 | + url: TARGET_URL, | ||
48 | + headers: { | ||
49 | + 'Authorization': `Bearer ${TOKEN}` | ||
50 | + }, | ||
51 | + json: { | ||
52 | + "replyToken":replyToken, | ||
53 | + "messages":[ | ||
54 | + { | ||
55 | + "type":"text", | ||
56 | + "text":transMessage | ||
57 | + } | ||
58 | + ] | ||
59 | + } | ||
60 | + },(error, response, body) => { | ||
61 | + console.log(body) | ||
62 | + }); | ||
63 | + } | ||
64 | + }); | ||
65 | +} | ||
66 | +try { | ||
67 | + const option = { | ||
68 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), | ||
69 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), | ||
70 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), | ||
71 | + }; | ||
72 | + | ||
73 | + HTTPS.createServer(option, app).listen(sslport, () => { | ||
74 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
75 | + }); | ||
76 | + } catch (error) { | ||
77 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
78 | + console.log(error); | ||
79 | + } | ||
80 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment