BBLBTV_Suzie/data/bot/lib/requests.js

66 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-06-25 19:45:38 +01:00
2020-06-29 22:43:41 +01:00
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");
2020-06-25 19:45:38 +01:00
2020-06-29 22:43:41 +01:00
module.exports = {
2020-06-30 10:09:22 +01:00
football_today: async (bot, msg, logger) => {
2020-06-29 22:43:41 +01:00
logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " asked for football today");
2020-06-25 19:45:38 +01:00
2020-06-29 22:43:41 +01:00
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
};
2020-06-25 19:45:38 +01:00
2020-06-30 10:09:22 +01:00
cachedRequest(options, async function (error, response, body) {
2020-06-29 22:43:41 +01:00
if (error) throw new Error(error);
let jsonBody = JSON.parse(body)
if (jsonBody.data.length > 0) {
2020-06-30 10:09:22 +01:00
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)
}
2020-06-29 22:43:41 +01:00
}).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 :(')
}
})
2020-06-30 10:09:22 +01:00
},
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)
}
)
2020-06-25 19:45:38 +01:00
}
2020-06-29 22:43:41 +01:00
}