B
B
Belavor2019-10-23 17:26:37
JavaScript
Belavor, 2019-10-23 17:26:37

How to embed puppeteer in node.js telegram bot?

how to embed puppeteer in node.js telegram bot? or debug?
I have a script that opens the site, logs in, takes a screen and closes. on puppeteer
, well, I want to embed this into the bot so that on command I would send a screen
like this on a scribble but it doesn’t work how to be

bot.onText(/\/gr(.+)/, function(msg, match) {
const edua = require('./scrapper');
var chatId = msg.chat.id;
var photo = 'grades.png';
(async() => {
  s
  await edua.initialize();

  await edua.login('login', 'password');

  await edua.grade();

  debugger;

})();
  bot.sendPhoto(chatId, photo, { caption: ' ' });
});

const puppeteer = require('puppeteer');
const eduEnu = 'https://edu.enu.kz/';
const grades = 'https://edu.enu.kz/current_progress_gradebook_student';

const platonus = {
     browser: null,
     page: null,

     initialize: async () => {
          platonus.browser = await puppeteer.launch({
               headless: false
          });
          platonus.page = await platonus.browser.newPage();
          await platonus.page.setViewport({width: 1000, height: 500});
     }, 
     
     login: async (username, password) => { 
          await platonus.page.goto(eduEnu, { waitUntil: 'networkidle2' });
          //await platonus.page.waitFor(1000);
          // let closeButton = await platonus.page.type('div[class="backdrop-close"]');
          // await closeButton[0].click();
          
          
          await platonus.page.type('input[name = "iin"]', username, {delay: 1});
          await platonus.page.type('input[name = "password"]', password, {delay: 1});

          let loginButton = await platonus.page.$x("//button[contains(text(), 'Кіру')]");
          await loginButton[0].click();
          await platonus.page.waitFor(5000);
     },

     grade: async () => {
          await platonus.page.goto(grades, { waitUntil: 'networkidle2' });
          await platonus.page.waitFor(2000);
          await platonus.page.screenshot({path: 'grades.png', fullPage: true});
          platonus.browser.close();

     }
}

module.exports = platonus;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2019-10-27
@you_are_enot

Try this

const edua = require('./scrapper');

bot.onText(/\/gr(.+)/, (msg, match) => {
    const chatId = msg.chat.id;
    const photo = 'grades.png';

    edua('login', 'password').then(() => {
        bot.sendPhoto(chatId, photo, { caption: ' ' });
    }).catch((err) => {
        console.log(err)
    });
});

const puppeteer = require('puppeteer');
const eduEnu = 'https://edu.enu.kz/';
const grades = 'https://edu.enu.kz/current_progress_gradebook_student';

async function platonus(login, password) {
    const browser = await puppeteer.launch({ headless: false });
   const page = await platonus.browser.newPage();
   await page.setViewport({width: 1000, height: 500});

   await page.goto(eduEnu, { waitUntil: 'networkidle2' });

   await page.waitForSelector('input[name = "iin"]')
   await page.type('input[name = "iin"]', username, {delay: 1});

   await page.waitForSelector('input[name = "password"]')
   await page.type('input[name = "password"]', password, {delay: 1});

   const loginButton = await platonus.page.$x("//button[contains(text(), 'Кіру')]");
   await loginButton[0].click();
   await platonus.page.waitFor(5000);

   await page.goto(grades, { waitUntil: 'networkidle2' });
   await page.waitFor(2000);
   await page.screenshot({path: 'grades.png', fullPage: true});

   browser.close();
};

module.exports = platonus

PS Using static timeouts is not a very good idea. It is better to wait for the appearance of the desired element. Plus, instead of waitUntil: 'networkidle2' on page load, it's better to use domcontentloaded/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question