2020-06-30 16:25:55 +00: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" ) ;
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 ( ( ) => {
2021-01-20 13:52:05 +00:00
return bot . sendMessage ( msg . chat . id , 'To see what channels the football is streaming on please type https://m.liveonsat.com/ into your browser. Or join @footballontv for a yearly membership fee.' )
2020-06-30 16:25:55 +00:00
} )
} else {
2020-11-07 12:43:52 +00:00
bot . sendMessage ( msg . chat . id , 'Sadly, there is no football I care about on today :(' )
2020-06-30 16:25:55 +00: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 )
}
)
}
}