wlstp8473

line_chatbot_code

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 = 'N6Q05E3VPLmtBdu+9Roi9f5b3pnEohm1T1jG/WIUS6T0kUJndd5osjymcy7qYp8AdWWstcwmG9Av1WhZZdLGH2J2IDqj9NRAY1BZ6RRbYINKL5ei1wA0TskYcIJ2kZZJXfHag5rdnyxwx92phll3gAdB04t89/1O/w1cDnyilFU=' //토큰 변경 바람
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "2020105604.oss2021.tk" //도메인 변경 바람
9 +const sslport = 23023;
10 +
11 +const bodyParser = require('body-parser');
12 +var app = express();
13 +app.use(bodyParser.json());
14 +app.post('/hook', function (req, res) {
15 +
16 + var eventObj = req.body.events[0];
17 + var source = eventObj.source;
18 + var message = eventObj.message;
19 +
20 + // request log
21 + console.log('======================', new Date() ,'======================');
22 + console.log('[request]', req.body);
23 + console.log('[request source] ', eventObj.source);
24 + console.log('[request message]', eventObj.message);
25 +
26 + request.post(
27 + {
28 + url: TARGET_URL,
29 + headers: {
30 + 'Authorization': `Bearer ${TOKEN}`
31 + },
32 + json: {
33 + "replyToken":eventObj.replyToken,
34 + "messages":[
35 + {
36 + "type":"text",
37 + "text":"Hello, user"
38 + },
39 + {
40 + "type":"text",
41 + "text":"May I help you?"
42 + }
43 + ]
44 + }
45 + },(error, response, body) => {
46 + console.log(body)
47 + });
48 +
49 +
50 + res.sendStatus(200);
51 +});
52 +
53 +try {
54 + const option = {
55 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
56 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
57 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
58 + };
59 +
60 + HTTPS.createServer(option, app).listen(sslport, () => {
61 + console.log(`[HTTPS] Server is started on port ${sslport}`);
62 + });
63 + } catch (error) {
64 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
65 + console.log(error);
66 + }
67 +
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "reply",
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 + "body-parser": "^1.19.0",
13 + "express": "^4.17.1",
14 + "request": "^2.88.2"
15 + }
16 +}
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 = 'N6Q05E3VPLmtBdu+9Roi9f5b3pnEohm1T1jG/WIUS6T0kUJndd5osjymcy7qYp8AdWWstcwmG9Av1WhZZdLGH2J2IDqj9NRAY1BZ6RRbYINKL5ei1wA0TskYcIJ2kZZJXfHag5rdnyxwx92phll3gAdB04t89/1O/w1cDnyilFU=' //라인 토큰도 변경 바람
5 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
6 +const PAPAGO_ID = 'V1zfMFP3c5VIxImaaNaQ' //파파고 아이디 변경 바람
7 +const PAPAGO_SECRET = 'd_pJLjO5eR' //파파고 시크릿 키 변경 바람
8 +const fs = require('fs');
9 +const path = require('path');
10 +const HTTPS = require('https');
11 +const domain = "2020105604.oss2021.tk" //도메인명 변경 바람
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 +
35 +//영어 번역
36 +function trans(replyToken, message) {
37 +
38 + request.post(
39 + {
40 + url: PAPAGO_URL,
41 + headers: {
42 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
43 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
44 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
45 + },
46 + body: 'source=ko&target=en&text=' + message,
47 + json:true
48 + },(error, response, body) => {
49 + if(!error && response.statusCode == 200) {
50 + console.log(body.message);
51 + var transMessage = body.message.result.translatedText;
52 + request.post(
53 + {
54 + url: TARGET_URL,
55 + headers: {
56 + 'Authorization': `Bearer ${TOKEN}`
57 + },
58 + json: {
59 + "replyToken":replyToken,
60 + "messages":[
61 + {
62 + "type":"text",
63 + "text":transMessage
64 + }
65 + ]
66 + }
67 + },(error, response, body) => {
68 + console.log(body)
69 + });
70 + }
71 + });
72 +
73 +}
74 +
75 +
76 +
77 +
78 +
79 +try {
80 + const option = {
81 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
82 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
83 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
84 + };
85 +
86 + HTTPS.createServer(option, app).listen(sslport, () => {
87 + console.log(`[HTTPS] Server is started on port ${sslport}`);
88 + });
89 + } catch (error) {
90 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
91 + console.log(error);
92 + }
93 +
...\ 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 +}