BBLBTV_Suzie/data/bot/lib/requests.js
2020-06-30 15:25:00 +01:00

66 lines
2.7 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 = {
football_today: async (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,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, async function (error, response, body) {
if (error) throw new Error(error);
let jsonBody = JSON.parse(body)
if (jsonBody.data.length > 0) {
bot.sendMessage(msg.chat.id, 'Yes there is!').then(async () => {
for (const x of jsonBody.data) {
var homeTeam = x.localTeam.data.name
var awayTeam = x.visitorTeam.data.name
var startTime = x.time.starting_at.time
await 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 :(')
}
})
},
joke: (bot, msg, logger, name) => {
logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " asked for a joke");
var options = {
method: 'GET',
url: 'https://joke3.p.rapidapi.com/v1/joke',
// qs: { tz: 'Europe/London', leagues: '8', include: 'localTeam,visitorTeam' },
headers: {
'x-rapidapi-host': 'joke3.p.rapidapi.com',
'x-rapidapi-key': process.env.RAPIDAPI_API_KEY,
useQueryString: true
},
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
let jsonBody = JSON.parse(body)
bot.sendMessage(msg.chat.id, jsonBody.content)
}
)
}
}