박찬수

reply.js modified.

Showing 1 changed file with 86 additions and 62 deletions
...@@ -12,6 +12,7 @@ const sslport = 23023; ...@@ -12,6 +12,7 @@ const sslport = 23023;
12 var soccer = require('./soccer.js'); 12 var soccer = require('./soccer.js');
13 13
14 const bodyParser = require('body-parser'); 14 const bodyParser = require('body-parser');
15 +const { EventEmitter } = require('stream');
15 var app = express(); 16 var app = express();
16 app.use(bodyParser.json()); 17 app.use(bodyParser.json());
17 18
...@@ -28,8 +29,53 @@ app.post('/hook', function (req, res) { ...@@ -28,8 +29,53 @@ app.post('/hook', function (req, res) {
28 console.log('[request message]', eventObj.message); 29 console.log('[request message]', eventObj.message);
29 console.log("Receive Message : ", eventObj.message.text); 30 console.log("Receive Message : ", eventObj.message.text);
30 31
31 - console.log(soccer.GetPlayerInfo(276, 2019)); 32 + console.log(soccer.GetPlayerInfo(276, 2019, eventObj));
32 33
34 + // request.post(
35 + // {
36 + // url: TARGET_URL,
37 + // headers: {
38 + // 'Authorization': `Bearer ${TOKEN}` // 인증정보 : channel token 값을 통해 인증.
39 + // },
40 + // json: {
41 + // "replyToken":eventObj.replyToken, // reply token : 누구한테 보낼 것인지?를 판별하기 위해!
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 + // res.sendStatus(200);
59 +});
60 +
61 +try {
62 + const option = {
63 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
64 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
65 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
66 + //key : fs.readFileSync('./rootca.key'),
67 + // cert : fs.readFileSync('./rootca.crt')
68 + };
69 +
70 + HTTPS.createServer(option, app).listen(sslport, () => {
71 + console.log(`[HTTPS] Server is started on port ${sslport}`);
72 + });
73 + } catch (error) {
74 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
75 + console.log(error);
76 + }
77 +
78 +function Reply(eventObj){
33 request.post( 79 request.post(
34 { 80 {
35 url: TARGET_URL, 81 url: TARGET_URL,
...@@ -52,70 +98,48 @@ app.post('/hook', function (req, res) { ...@@ -52,70 +98,48 @@ app.post('/hook', function (req, res) {
52 },(error, response, body) => { 98 },(error, response, body) => {
53 console.log(body); 99 console.log(body);
54 }); 100 });
55 -
56 -
57 res.sendStatus(200); 101 res.sendStatus(200);
58 -}); 102 +}
59 103
60 -try { 104 +function GetPlayerInfo(playerID, season, eventObj){
61 - const option = { 105 + var request = require('request');
62 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'), 106 + var options = {
63 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(), 107 + method: 'GET',
64 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(), 108 + url: 'https://v3.football.api-sports.io/players',
65 - //key : fs.readFileSync('./rootca.key'), 109 + qs: {id: playerID, season: season},
66 - // cert : fs.readFileSync('./rootca.crt') 110 + headers: {
111 + 'x-rapidapi-host': 'v3.football.api-sports.io',
112 + 'x-rapidapi-key': '526fc70a2e8b315e9a960ac4b4764191'
113 + }
67 }; 114 };
68 - 115 + request(options, function (error, response) {
69 - HTTPS.createServer(option, app).listen(sslport, () => { 116 + if (error) throw new Error(error);
70 - console.log(`[HTTPS] Server is started on port ${sslport}`); 117 + console.log(response.body);
118 + Reply(eventObj);
71 }); 119 });
72 - } catch (error) { 120 +}
73 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 121 +
74 - console.log(error); 122 +function Emitter(){
75 - } 123 + this.events = {};
76 - 124 +}
77 -module.exports = { 125 +
78 - StartReply : function(){ 126 +Emitter.prototype.on = function(type, listener){
79 - app.post('/hook', function (req, res) { 127 + this.events[type] = this.events[type] || [];
80 - 128 + this.events[type].push(listener);
81 - var eventObj = req.body.events[0]; 129 +}
82 - var source = eventObj.source; 130 +
83 - var message = eventObj.message; 131 +const emitter = new Emitter();
84 - 132 +
85 - // request log 133 +emitter.on('soccer', function(){
86 - console.log('======================', new Date() ,'======================'); 134 + console.log('soccer logged.');
87 - console.log('[request]', req.body); 135 +})
88 - console.log('[request source] ', eventObj.source); 136 +
89 - console.log('[request message]', eventObj.message); 137 +Emitter.prototype.emit = function(type){
90 - console.log("Receive Message : ", eventObj.message.text); 138 + if(this.events[type]){
91 - 139 + this.events[type].forEach(function(listener){
92 - console.log(soccer.GetPlayerInfo(276, 2019)); 140 + listener();
93 -
94 - request.post(
95 - {
96 - url: TARGET_URL,
97 - headers: {
98 - 'Authorization': `Bearer ${TOKEN}` // 인증정보 : channel token 값을 통해 인증.
99 - },
100 - json: {
101 - "replyToken":eventObj.replyToken, // reply token : 누구한테 보낼 것인지?를 판별하기 위해!
102 - "messages":[
103 - {
104 - "type":"text",
105 - "text":"Hello, user"
106 - },
107 - {
108 - "type":"text",
109 - "text":"May I help you?"
110 - }
111 - ]
112 - }
113 - },(error, response, body) => {
114 - console.log(body);
115 - });
116 -
117 -
118 - res.sendStatus(200);
119 }); 141 });
120 } 142 }
121 -}
...\ No newline at end of file ...\ No newline at end of file
143 +}
144 +
145 +emitter.emit('soccer');
...\ No newline at end of file ...\ No newline at end of file
......