pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.54k stars 3.7k forks source link

Unable to allow 2 user logging in #2199

Closed kaihungc1993 closed 1 year ago

kaihungc1993 commented 1 year ago

Is there an existing issue for this?

Describe the bug

I'm running a server to spin out a wechaty bot upon request.

Here's an example code:

app.post('/register', (req: Request, res: Response) => {
  // Create a bot
  var bot = WechatyBuilder.build({name: bot',})

  // onScan
  bot.on('scan', (qrcode, status) => {
    if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
      const qrcodeImageUrl = ['https://wechaty.js.org/qrcode/', encodeURIComponent(qrcode),].join('')
      log.info('[Wechaty]', 'onScan: %s(%s) - qrcodeImageUrl: %s', ScanStatus[status], status, qrcodeImageUrl)
      res.send(`<div><a href="${qrcodeImageUrl}" target="_blank">Scan this QR Code</a></div>`);  // Output the QR link on the webpage
    } else {
      log.info('[Wechaty]', 'onScan: %s(%s)', ScanStatus[status], status)
    }
  })

  // onLogin / onLogout
  bot.on('login', (user: Contact) =>{
    log.info('[Wechaty] ', '%s login', user))
  bot.on('logout', onLogout)

  bot.start()
    .then(() => {log.info('[Wechaty] ', 'Wechaty Bot Started.')})
    .catch(e => log.error('[Wechaty] ', e))
});

Here's the error code when a second account logs in

Server listening on port 3000
////// New User Register Request //////
23:50:56 INFO [Wechaty]  Wechaty Bot Started.
23:51:20 INFO LoginEventHandler 
...
23:51:20 INFO [Wechaty] onScan: Waiting(2) - qrcodeImageUrl: ..
23:51:42 INFO LoginEventHandler onAuthenticated()
...
23:51:50 INFO PuppetWhatsapp READY
////// New User Register Request //////
23:52:10 WARN Manager asystole count: 1
23:52:10 INFO [Wechaty]  Wechaty Bot Started.
Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'getContacts')
    at __puppeteer_evaluation_script__:2:34
    at ExecutionContext._evaluateInternal (C:\Wechaty_v1.2\wechaty\node_modules\puppeteer\src\common\ExecutionContext.ts:273:13)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (C:\Wechaty_v1.2\wechaty\node_modules\puppeteer\src\common\ExecutionContext.ts:140:12)
    at async Client.getContacts (C:\Wechaty_v1.2\wechaty\node_modules\@juzi\whatsapp-web.js\src\Client.js:768:24)
    at async Manager.syncContactOrRoomList (file:///C:/D(Kai)/%E5%89%B5%E6%A5%AD/Wechaty_v1.2/wechaty/node_modules/wechaty-puppet-whatsapp/src/manager.ts:190:44)
    at async Job.job (file:///C:/D(Kai)/%E5%89%B5%E6%A5%AD/Wechaty_v1.2/wechaty/node_modules/wechaty-puppet-whatsapp/src/manager.ts:237:33)

I have tested using different accounts. i.e. account A then B has the same output as B then A

Expected behavior

Allow multiple bots created for multiple user.

Steps to Reproduce the Bug or Issue

  1. Launch the server to start post function
  2. send the first post request, scan QR code and log in
  3. send the second post request

Relevant Code

No response

Browser Type

Google Chrome

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Windows Android whatsapp-web.js 1.19.6

Additional context

No response

kaihungc1993 commented 1 year ago

I also tested running the same code twice on two terminals. I guess if whatsapp-web.js only allows connecting to one account given 1 IP.

themazim commented 1 year ago

Are you sure you posted in the proper repository? This sounds like an issue for: https://github.com/wechaty/wechaty

kaihungc1993 commented 1 year ago

Based on the error log, it looks like the error is related to @juzi\whatsapp-web.js\src\Client.js. I'm guessing that I can get more insights here. Thank you.

themazim commented 1 year ago

getContacts is called on undefined, which implicates the client is undefined. How the client is created is rooted within the wechaty manager from what I can tell (not familiar with the wechaty package).

This is not related to the whatsapp-web.js repo but rather how the wrapper (wechaty / WECHATY PUPPET WHATSAPP) handles this.

whatsapp-web.js supports as many clients as you want if used properly.

Maybe your issue is rooted within using the same bot name bot for each client. in whatsapp-webjs this would lead to an issue since the clients would store their session data in the same folder.

kaihungc1993 commented 1 year ago

You're right. The wrapper is currently set using fixed LocalAuth. According to the documentation, it should support multi-session. Closing this ticket.