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.39k stars 3.67k forks source link

Error: Evaluation failed: Error: wid error: invalid wid #2987

Closed jonathanrobertoo closed 6 months ago

jonathanrobertoo commented 6 months ago

Is there an existing issue for this?

Describe the bug

at e (https://web.whatsapp.com/:2:4910) at new f (https://web.whatsapp.com/app.febe97f47327a63e0d22.js:306:201531) at Object.c [as createWid] (https://web.whatsapp.com/app.febe97f47327a63e0d22.js:306:207832) at puppeteer_evaluation_script:2:53

I forced an error in the phone property passing a string with letters instead numbers. I'm trying to catch the error in the API like this:

router.post('/message', (req, res) => {
  const { phoneNumber, message } = req.body
  try {
    whatsappClient.sendMessage(`55${phoneNumber}@c.us`, message)
    res.send({ message: 'Message sent' })
  } catch (error) {
    console.error('Error sending message:', error)
    res.status(500).send({ message: 'Error sending message', error: error.message })
  }
})

and in a client in my own API:

export class WhatsappWebClient implements SendWhatsappWeb {
  private readonly api: AxiosInstance

  constructor (private config: WhatsappWebConfig = generalConfigs.clients.whatsapp.whatsappWeb) {
    this.api = axios.create({
      baseURL: this.config.url
    })
  }

  async send ({ phoneNumber, message }: SendWhatsappWeb.Input): Promise<void> {
    try {
      await this.api.post('/message', {
        phoneNumber,
        message
      })

      logger.info('Successfully sent whatsapp message.')
    } catch (error) {
      if (!axios.isAxiosError(error)) {
        // If it's not an AxiosError, we throw a generic error
        throw new Error('An error occurred while sending the whatsapp message.')
      }

      const axiosError = error as AxiosError

      if (axiosError.response) {
        // If there's a response, it means the request was made and the server responded with a status code outside the range of 2xx
        const errorMessage = `Error when calling WhatsApp Web: ${axiosError.response.status} - ${axiosError.response.statusText}`
        logger.error(errorMessage)
        throw new Error(errorMessage)
      } else if (axiosError.request) {
        // If there's a request, it means the request was made but no response was received
        logger.error('No response received from WhatsApp Web.')
        throw new Error('No response received from WhatsApp Web.')
      } else {
        // Otherwise, it's just a generic error
        logger.error('An error occurred while sending the whatsapp message.')
        throw new Error('An error occurred while sending the whatsapp message.')
      }
    }
  }

but doesn't work.

Expected behavior

As a user, I expect that errors been catch in

  } catch (error) {
    console.error('Error sending message:', error)
    res.status(500).send({ message: 'Error sending message', error: error.message })
  }
})

and not in:

Error: Evaluation failed: Error: wid error: invalid wid
    at e (https://web.whatsapp.com/:2:4910)
    at new f (https://web.whatsapp.com/app.febe97f47327a63e0d22.js:306:201531)
    at Object.c [as createWid] (https://web.whatsapp.com/app.febe97f47327a63e0d22.js:306:207832)
    at __puppeteer_evaluation_script__:2:53

Steps to Reproduce the Bug or Issue

  1. run the API
  2. send a request in postman passing a string with leathers instead numbers

Relevant Code

const express = require('express')

const messageRouter = require('./routers/messageRouter')

const whatsappClient = require('./services/WhatsappClient')

whatsappClient.initialize()

const app = express()

app.use(express.json())
app.use(messageRouter)

const PORT = 4000

app.listen(PORT, () => console.log('Server is runing on:', PORT))
FROM node:latest

RUN apt-get update && apt-get install -y \
    curl \
    wget \
    gnupg \
    ca-certificates \
    libnss3 \
    libxss1 \
    libasound2 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgcc1 \
    libgconf-2-4 \
    libgdk-pixbuf2.0-0 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    lsb-release \
    xdg-utils \
    fonts-liberation \
    libappindicator1 \
    libgbm-dev \
    chromium \
&& rm -rf /var/lib/apt/lists/*

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV CHROMIUM_PATH /usr/bin/chromium

WORKDIR /usr/src

COPY package.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD [ "npm", "start" ]

ENV PUPPETEER_SKIP_SANDBOX true
const express = require('express')
const router = new express.Router()
const whatsappClient = require('../services/WhatsappClient')

router.get('/check', (req, res) => {
  res.send(`API service's up!`)
})

router.post('/message', (req, res) => {
  const { phoneNumber, message } = req.body
  try {
    whatsappClient.sendMessage(`55${phoneNumber}@c.us`, message)
    res.send({ message: 'Message sent' })
  } catch (error) {
    console.error('Error sending message:', error)
    res.status(500).send({ message: 'Error sending message', error: error.message })
  }
})

module.exports = router

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Linux Ubuntu

Additional context

No response

alechkos commented 6 months ago

https://github.com/pedroslopez/whatsapp-web.js/issues/1441#issuecomment-1178965390

https://github.com/pedroslopez/whatsapp-web.js/issues/2765#issuecomment-1932729073