节点js传递参数给函数

Node js pass parameter to function

我有这个代码:

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);

})();

我想将浏览器变量传递给 eventHandler 函数。 我这样尝试,但它不起作用,浏览器在 eventHandler.

中出现“未定义”

如何将浏览器变量传递给我 eventHandler

不确定 client.addEventHandler 的签名是什么,但假设它采用单个参数 event,您可以尝试将最后一行替换为:

client.addEventHandler(
  (event) => eventHandler(event, browser),
  new NewMessage({}),
);