From 0b9b3b411c336fe66d664d98c1f15acc1128b50d Mon Sep 17 00:00:00 2001 From: "karl.hudgell" Date: Mon, 29 Jun 2020 22:43:41 +0100 Subject: [PATCH] new style with commands --- data/bot/functions/requests/football.js | 83 ++++++++++++------------ data/bot/lib/common.js | 29 +++++---- data/bot/{functions => lib}/questions.js | 4 +- data/group/commands.js | 6 +- package-lock.json | 15 +++++ package.json | 3 + test.js | 34 +++++++--- 7 files changed, 106 insertions(+), 68 deletions(-) rename data/bot/{functions => lib}/questions.js (94%) diff --git a/data/bot/functions/requests/football.js b/data/bot/functions/requests/football.js index 464b990..2a720fe 100644 --- a/data/bot/functions/requests/football.js +++ b/data/bot/functions/requests/football.js @@ -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 :(') + } + }) } -} \ No newline at end of file +} diff --git a/data/bot/lib/common.js b/data/bot/lib/common.js index 1e18f40..9859cf3 100644 --- a/data/bot/lib/common.js +++ b/data/bot/lib/common.js @@ -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 = "@" + 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') } - }) + } } }; \ No newline at end of file diff --git a/data/bot/functions/questions.js b/data/bot/lib/questions.js similarity index 94% rename from data/bot/functions/questions.js rename to data/bot/lib/questions.js index dd1542f..dc23aa3 100644 --- a/data/bot/functions/questions.js +++ b/data/bot/lib/questions.js @@ -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" }) } - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/data/group/commands.js b/data/group/commands.js index b02d665..bbc8378 100644 --- a/data/group/commands.js +++ b/data/group/commands.js @@ -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": [] } ] diff --git a/package-lock.json b/package-lock.json index 86d5139..b54ce4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 15c4d1c..d457f9f 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ "request": "^2.88.2", "require-dir": "^1.2.0", "throttled-request": "^0.1.1" + }, + "devDependencies": { + "decache": "^4.6.0" } } diff --git a/test.js b/test.js index 6c76beb..4b5b8c0 100644 --- a/test.js +++ b/test.js @@ -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") } -}) \ No newline at end of file + + if (msg.text && msg.text.toString().includes("/football_today")) { + football.gamesToday(bot, msg, logger) + }}) \ No newline at end of file