1

I have this code :

const { Logger } = require ("telegram/extensions");
const { TelegramClient } = require ("telegram");
const { StringSession } = require ("telegram/sessions");
const { NewMessage } = require ("telegram/events");
const { NewMessageEvent } = require ("telegram/events/NewMessage");
const { Message } = require ("telegram/tl/custom/message");
const input = require('input'); // npm i input
const puppeteer = require('puppeteer-extra');


async function eventHandler(event, browser) {
    //get event.message, ....
    const page = await browser.newPage();

}


const client = new TelegramClient(
  new StringSession(stringSession),
  apiId,
  apiHash,
  { connectionRetries: 5 }
);


(async () => {
  console.log('Loading interactive example...')
  const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--shm-size=2gb', '--start-maximized', '--disable-features=IsolateOrigins,site-per-process', '--disable-web-security'], headless: true});
  await client.start({
      phoneNumber: "+33...",
      password: async () => await input.text('password?'),
      phoneCode: async () => await input.text('Code ?'),
      onError: (err) => console.log(err),
  });
  console.log('Telegram bot connected');
  console.log(client.session.save());
  client.addEventHandler(eventHandler, new NewMessage({}), browser);

})();

I want to pass the browser variable to the eventHandler function. I try like that, but it does not work, browser came "undefined" in eventHandler.

How pass the browser variable to my eventHandler?

user2178964
  • 124
  • 6
  • 16
  • 40

1 Answers1

2

Not sure what is the signature of client.addEventHandler but assuming it takes a single param event, you could try replacing your last line with something like:

client.addEventHandler(
  (event) => eventHandler(event, browser),
  new NewMessage({}),
);
painor
  • 1,119
  • 1
  • 8
  • 25
Gpack
  • 1,878
  • 3
  • 18
  • 45
  • Thanks. The "event" variable does not really exist, I don't understand how it is passed without writing it. I try your suggestion, but I had : "ReferenceError: event is not defined" on your second line. – user2178964 Jun 11 '21 at 13:54
  • But do you really need the `event` variable? You could just do `() => eventHandler(browser, test)`, no? – Gpack Jun 11 '21 at 13:59
  • Yes I need it. In the event there is "event.message" with all the info of the new message posted on telegram – user2178964 Jun 11 '21 at 14:00
  • Mmm actually that's weird because reading at the source code of the repo, the callback function should be called with an event as a parameter... See [here](https://github.com/gram-js/gramjs/blob/89e62a66f00415358ab504fd32118641c2417f0c/gramjs/client/updates.ts#L128) – Gpack Jun 11 '21 at 14:06
  • Sorry I just realised I made a mistake in my code... I forgot to add the `event` as a param of the callback function. I've updated my answer – Gpack Jun 11 '21 at 14:07
  • 1
    the `client.addEventHandler` takes a callback function in the first arg. which it would then call like `callback(event)`. (so your code is correct minus the the browser in addEventHandler arg) – painor Jun 11 '21 at 14:15