Node Web Scrapper with Telegram Bot - Timeout/Callback Issue

Viewed 22

I have coded a node script to check a particular sporting event resale website. The bot checks for tickets for 'Team 1' and then should ping a message to a Telegram group.

scrapper.js

const axios = require("axios");
const cheerio = require("cheerio");
const options = require("./options");

const ticketsAvailable = require("./bot");

async function checkForTickets(callback) {
  try {
    const siteURL = options.url;
    const { data } = await axios({
      method: "GET",
      url: siteURL,
    });
    // set cheerio to $
    const $ = cheerio.load(data);

    // set team class
    const teamFinder = ".team";

    // set team
    const team = "Team 1";

    // set button class
    const buttonDiv = ".actions-wrapper";

    // set match day class
    const match = ".commerce_product";

    // basic implamentation based on match count to start
    let team1MatchCount = 0;

    // check each of the match divs
    $(match).each((MDIdx, MDElm) => {
      // loop through the games based on the selected team
      $(teamFinder, MDElm).each((parentIdx, parentElm) => {
        // get value of team name span
        let text = $(parentElm).text();

        if (text == team) {
          // check if thckets within this match are "unavailable" (for testing)
          if ($(buttonDiv).children().hasClass("available")) {
            team1MatchCount++;
          }
        }
      });
    });

    // if 1 or more of the selected team has tickets available, call the bot script
    if (team1MatchCount > 0) {
      ticketsAvailable();

      // reset the match count to 0 for the next function call
      team1MatchCount = 0;
    }
  } catch (e) {
    console.error(e);
  }
}
setInterval(checkForTickets, options.delay);

Above is the script that checks the site. The error (TypeError: ticketsAvailable is not a function at Timeout.checkForTickets [as _onTimeout]) is being caused by the ticketsAvailable() which is on the bot script but called on the script above if a match for Team 1

bot.js

require("dotenv").config();
require("./scrapper");

const options = require("./options");

const TelegramBot = require("node-telegram-bot-api");

// replace the value below with the Telegram token you receive from @BotFather
const token = options.telegram.token;

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });

// set the chat ID so it is only for one group
const chatId = options.telegram.chatId;

function ticketsAvailable() {
  let msg =
    "Tickets available. Run RUN RUN!!!!";
  bot.sendMessage(chatId, msg);
}

module.exports = ticketsAvailable;

When i run node bot.js which requires the scrapper script at the top, i get the error. If i run both scripts in separate terminals there is no issue. I need to be able to call both scripts from the one node bot.js call as this is how it will work on Glitch.

I am presuming this is something simple and related to callback functions but I have tried a ton of things and nothing has worked.

0 Answers
Related