new style with commands

This commit is contained in:
karl.hudgell 2020-06-29 22:43:41 +01:00
parent dd607e15c4
commit 0b9b3b411c
7 changed files with 106 additions and 68 deletions

View File

@ -1,47 +1,48 @@
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) => {
const footballToday = "football today";
if (msg.text && msg.text.toString().toLowerCase().includes(footballToday)) {
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 :(')
}
})
}
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 :(')
}
})
}
}
}

View File

@ -1,17 +1,20 @@
const decache = require('decache');
module.exports = {
help: (bot, msg, logger) => {
bot.on('message', (msg) => {
const updatesGroup = "/sub_status";
const checkName = updatesGroup + name
if (msg.text && msg.text.toString() === checkName) {
// logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " Got the updates group info");
bot.sendMessage(msg.chat.id, "The updates groups can be found here")
.then(() => {
return bot.sendMessage(msg.chat.id, "https://t.me/joinchat/AAAAAFDxZ2rzgAW5acahxA").then(() => {
return bot.sendMessage(msg.chat.id, "Please join for BBLBTV updates")
})
})
commands: async (bot, msg, input) => {
const userMention = "<a href=\"tg:\/\/user?id=" + msg.from.id + "\">@" + msg.from.first_name + "<\/a> "
let commands = require('../../group/commands')
for (const x of commands) {
if (x.NAME === input) {
let n = x.NAME
let t = x.TEXT
for (const m of t) {
let a = userMention.concat(m)
await bot.sendMessage(msg.chat.id, a, { parse_mode: "HTML" })
}
decache('../../group/commands');
commands = require('../../group/commands')
}
})
}
}
};

View File

@ -17,7 +17,6 @@ module.exports = {
let string = ""
const subStatus = "sub status";
if (msg.text && msg.text.toString().toLowerCase().includes(subStatus)) {
logger.info("ID - " + msg.from.id + " First Name - " + msg.from.first_name + " Asked how to get a subscription");
subs.forEach(element => {
if (element.STATUS != "X") {
@ -34,5 +33,4 @@ module.exports = {
// console.log(string)
bot.sendMessage(msg.chat.id, string, { parse_mode: "HTML" })
}
}
}
}

View File

@ -2,8 +2,12 @@ const commands = [
{
"NAME": "/help",
"TEXT": [
"Hello " + userMention + " please PM me @" + process.env.BOT_USERNAME + " and i'll try to help.",
"please PM me @" + process.env.BOT_USERNAME + " and i'll try to help.",
]
},
{
"NAME": "/sub_status",
"TEXT": []
}
]

15
package-lock.json generated
View File

@ -83,6 +83,12 @@
"mkdirp": "^0.5.1"
}
},
"callsite": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz",
"integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA=",
"dev": true
},
"caseless": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
@ -117,6 +123,15 @@
"ms": "^2.1.1"
}
},
"decache": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/decache/-/decache-4.6.0.tgz",
"integrity": "sha512-PppOuLiz+DFeaUvFXEYZjLxAkKiMYH/do/b/MxpDe/8AgKBi5GhZxridoVIbBq72GDbL36e4p0Ce2jTGUwwU+w==",
"dev": true,
"requires": {
"callsite": "^1.0.0"
}
},
"define-properties": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",

View File

@ -22,5 +22,8 @@
"request": "^2.88.2",
"require-dir": "^1.2.0",
"throttled-request": "^0.1.1"
},
"devDependencies": {
"decache": "^4.6.0"
}
}

34
test.js
View File

@ -1,21 +1,35 @@
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const logger = require('perfect-logger');
const token = process.env.BOT_TOKEN;
const name = process.env.BOT_NAME;
// Configure Settings
logger.initialize(name + "_bot", {
logLevelFile: 0, // Log level for file
logLevelConsole: 0, // Log level for STDOUT/STDERR
logDirectory: 'logs/', // Log directory
// customBannerHeaders: 'This is a custom banner' // Custom Log Banner
});
const TelegramBot = require('node-telegram-bot-api');
const common = require('./data/bot/lib/common')
const questions = require('./data/bot/lib/questions')
const football = require('./data/bot/functions/requests/football')
const bot = new TelegramBot(token, { polling: true });
function t(msg, input) {
let commands = require('./data/group/commands')
bot.sendMessage(msg.chat.id, input)
}
bot.on('message', (msg) => {
if (msg.text && msg.text.toString().includes("/sub_status")) {
t(msg, "/sub_status")
questions.subStatus(bot, msg, logger)
}
if (msg.text && msg.text.toString().includes("/help")) {
t(msg, "/help")
common.commands(bot, msg, "/help")
}
})
if (msg.text && msg.text.toString().includes("/football_today")) {
football.gamesToday(bot, msg, logger)
}})