2020-06-29 22:43:41 +01:00

49 lines
2.0 KiB
JavaScript

var request = require("request");
var throttledRequest = require('throttled-request')(request);
throttledRequest.configure({ requests: 8, milliseconds: 60000 }); //send 1 request per second
cachedRequest = require('cached-request')(throttledRequest);
cachedRequest.setCacheDirectory("./cache");
module.exports = {
gamesToday: (bot, msg, logger) => {
logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " asked for football today");
var options = {
method: 'GET',
url: 'https://football-pro.p.rapidapi.com/api/v2.0/livescores',
qs: { tz: 'Europe/London', leagues: '8,9,2,5,23,24', include: 'localTeam,visitorTeam' },
headers: {
'x-rapidapi-host': 'football-pro.p.rapidapi.com',
'x-rapidapi-key': process.env.RAPIDAPI_API_KEY,
useQueryString: true
},
ttl: 18000000 //5 hours
};
cachedRequest(options, function (error, response, body) {
// console.log(response)
if (error) throw new Error(error);
// console.log(body);
let jsonBody = JSON.parse(body)
if (jsonBody.data.length > 0) {
bot.sendMessage(msg.chat.id, 'Yes there is!').then(() => {
return jsonBody.data.forEach(function (entry) {
var homeTeam = entry.localTeam.data.name
var awayTeam = entry.visitorTeam.data.name
var startTime = entry.time.starting_at.time
// console.log(entry);
bot.sendMessage(msg.chat.id, homeTeam + " VS " + awayTeam + " @ " + startTime)
});
}).then(() => {
return bot.sendMessage(msg.chat.id, 'To see what channels the football is streaming on please join @footballontv')
})
} else {
bot.sendMessage(msg.chat.id, 'Sadly there is no football today :(')
}
})
}
}