박찬수

push.js, reply.js commit

1 +const request = require('request');
2 +const TARGET_URL = 'https://api.line.me/v2/bot/message/push'
3 +const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
4 +const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 +const TOKEN = 'XOyIf8jsoQKq3b1zqxE4wawAoFU2Hz433AO3w8/ye+i6+2KrXpyfFwY0Dk/xhHQLPgtgPTiEP/m4IRW+SlVhdtzfH6c0Lfdw6nJ95QOugHfNWfviAmn5Uojh8LQJeAy21bvaNMCy11f+qgLSRnXmCgdB04t89/1O/w1cDnyilFU='
6 +const USER_ID = '사Uc4258407a7677769f74ba184ec036651'
7 +
8 +//Single User
9 +// request.post(
10 +// {
11 +// url: TARGET_URL,
12 +// headers: {
13 +// 'Authorization': `Bearer ${TOKEN}`
14 +// },
15 +// json: {
16 +// "to": `${USER_ID}`,
17 +// "messages":[
18 +// {
19 +// "type":"text",
20 +// "text":"Hello, user"
21 +// },
22 +// {
23 +// "type":"text",
24 +// "text":"May I help you?"
25 +// }
26 +// ]
27 +// }
28 +// },(error, response, body) => {
29 +// console.log(body)
30 +// });
31 +
32 +
33 +// Multicast User
34 +request.post(
35 + {
36 + url: MULTI_TARGET_URL,
37 + headers: {
38 + 'Authorization': `Bearer ${TOKEN}`
39 + },
40 + json: {
41 + "to": [`${USER_ID}`],
42 + "messages":[
43 + {
44 + "type":"text",
45 + "text":"Hello, user"
46 + },
47 + {
48 + "type":"text",
49 + "text":"May I help you?"
50 + }
51 + ]
52 + }
53 + },(error, response, body) => {
54 + console.log(body)
55 + });
56 +
57 +
58 +// Broadcast
59 + request.post(
60 + {
61 + url: BROAD_TARGET_URL,
62 + headers: {
63 + 'Authorization': `Bearer ${TOKEN}`
64 + },
65 + json: {
66 + "messages":[
67 + {
68 + "type":"text",
69 + "text":"Hello, user"
70 + },
71 + {
72 + "type":"text",
73 + "text":"May I help you?"
74 + }
75 + ]
76 + }
77 + },(error, response, body) => {
78 + console.log(body)
79 + });
...\ No newline at end of file ...\ No newline at end of file
1 +var express = require('express');
2 +const request = require('request');
3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' // reply api
4 +const TOKEN = 'XOyIf8jsoQKq3b1zqxE4wawAoFU2Hz433AO3w8/ye+i6+2KrXpyfFwY0Dk/xhHQLPgtgPTiEP/m4IRW+SlVhdtzfH6c0Lfdw6nJ95QOugHfNWfviAmn5Uojh8LQJeAy21bvaNMCy11f+qgLSRnXmCgdB04t89/1O/w1cDnyilFU='
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "2018102191.osschatbot2022.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}` // 인증정보 : channel token 값을 통해 인증.
31 + },
32 + json: {
33 + "replyToken":eventObj.replyToken, // reply token : 누구한테 보낼 것인지?를 판별하기 위해!
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 +