박찬수

reply.js modified.

Showing 1 changed file with 69 additions and 45 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,33 +29,33 @@ app.post('/hook', function (req, res) { ...@@ -28,33 +29,33 @@ 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 +
33 - request.post( 34 + // request.post(
34 - { 35 + // {
35 - url: TARGET_URL, 36 + // url: TARGET_URL,
36 - headers: { 37 + // headers: {
37 - 'Authorization': `Bearer ${TOKEN}` // 인증정보 : channel token 값을 통해 인증. 38 + // 'Authorization': `Bearer ${TOKEN}` // 인증정보 : channel token 값을 통해 인증.
38 - }, 39 + // },
39 - json: { 40 + // json: {
40 - "replyToken":eventObj.replyToken, // reply token : 누구한테 보낼 것인지?를 판별하기 위해! 41 + // "replyToken":eventObj.replyToken, // reply token : 누구한테 보낼 것인지?를 판별하기 위해!
41 - "messages":[ 42 + // "messages":[
42 - { 43 + // {
43 - "type":"text", 44 + // "type":"text",
44 - "text":"Hello, user" 45 + // "text":"Hello, user"
45 - }, 46 + // },
46 - { 47 + // {
47 - "type":"text", 48 + // "type":"text",
48 - "text":"May I help you?" 49 + // "text":"May I help you?"
49 - } 50 + // }
50 - ] 51 + // ]
51 - } 52 + // }
52 - },(error, response, body) => { 53 + // },(error, response, body) => {
53 - console.log(body); 54 + // console.log(body);
54 - }); 55 + // });
55 - 56 +
56 - 57 +
57 - res.sendStatus(200); 58 + // res.sendStatus(200);
58 }); 59 });
59 60
60 try { 61 try {
...@@ -74,23 +75,7 @@ try { ...@@ -74,23 +75,7 @@ try {
74 console.log(error); 75 console.log(error);
75 } 76 }
76 77
77 -module.exports = { 78 +function Reply(eventObj){
78 - StartReply : function(){
79 - app.post('/hook', function (req, res) {
80 -
81 - var eventObj = req.body.events[0];
82 - var source = eventObj.source;
83 - var message = eventObj.message;
84 -
85 - // request log
86 - console.log('======================', new Date() ,'======================');
87 - console.log('[request]', req.body);
88 - console.log('[request source] ', eventObj.source);
89 - console.log('[request message]', eventObj.message);
90 - console.log("Receive Message : ", eventObj.message.text);
91 -
92 - console.log(soccer.GetPlayerInfo(276, 2019));
93 -
94 request.post( 79 request.post(
95 { 80 {
96 url: TARGET_URL, 81 url: TARGET_URL,
...@@ -113,9 +98,48 @@ module.exports = { ...@@ -113,9 +98,48 @@ module.exports = {
113 },(error, response, body) => { 98 },(error, response, body) => {
114 console.log(body); 99 console.log(body);
115 }); 100 });
101 + res.sendStatus(200);
102 +}
116 103
104 +function GetPlayerInfo(playerID, season, eventObj){
105 + var request = require('request');
106 + var options = {
107 + method: 'GET',
108 + url: 'https://v3.football.api-sports.io/players',
109 + qs: {id: playerID, season: season},
110 + headers: {
111 + 'x-rapidapi-host': 'v3.football.api-sports.io',
112 + 'x-rapidapi-key': '526fc70a2e8b315e9a960ac4b4764191'
113 + }
114 + };
115 + request(options, function (error, response) {
116 + if (error) throw new Error(error);
117 + console.log(response.body);
118 + Reply(eventObj);
119 + });
120 +}
117 121
118 - res.sendStatus(200); 122 +function Emitter(){
123 + this.events = {};
124 +}
125 +
126 +Emitter.prototype.on = function(type, listener){
127 + this.events[type] = this.events[type] || [];
128 + this.events[type].push(listener);
129 +}
130 +
131 +const emitter = new Emitter();
132 +
133 +emitter.on('soccer', function(){
134 + console.log('soccer logged.');
135 +})
136 +
137 +Emitter.prototype.emit = function(type){
138 + if(this.events[type]){
139 + this.events[type].forEach(function(listener){
140 + listener();
119 }); 141 });
120 } 142 }
121 } 143 }
144 +
145 +emitter.emit('soccer');
...\ No newline at end of file ...\ No newline at end of file
......