0
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js')
const {timer, generateRandomTime} = require('./utils')
const log = require('electron-log')
const puppeteer = require('puppeteer')

const main = async (mensagem, leads, imageInfo) => {
try {
    log.info('starting main function');
    const client = new Client({
        puppeteer: {
            executablePath: puppeteer.executablePath(),
            headless: false,
            args: ['--no-sandbox', '--disable-setuid-sandbox'],
        }
    })

    client.initialize()
    log.info('initialized client')

    client.on('qr', (qr) => {
        console.log('QR RECEIVED', qr)
        log.info('qr', qr)
    })

    client.on('authenticated', (session) => {
        console.log('AUTHENTICATED', session)
    })

    client.on('auth_failure', (msg) => {
        console.error('AUTHENTICATION FAILURE', msg)
        log.info('auth faile', msg)
    })

    client.on('ready', async  () => {
        console.log('READY')
        log.info('ready')
        for (const lead of leads) {
            const phone = lead.telefone
            const name = lead.nome
            const message = mensagem.replace('lead', name)
            const chatId = `55${phone.trim()}@c.us`
            client.sendMessage(chatId, message)
            if(imageInfo.hasImage) {
                for(let i = 0; i < imageInfo.imageCount; i++) {
                    const imageBuffer = imageInfo[`image${i}`]
                    const media = new MessageMedia('image/jpeg', imageBuffer.toString('base64'), `image${i}.jpg`)
                    client.sendMessage(chatId, media)
                }
            }
            await timer(generateRandomTime() * 1000)
        }
    })

    client.on('message', async (msg) => {
        if (msg.hasMedia) {
            const media = await msg.downloadMedia()
            console.log('MEDIA RECEIVED', media)
        }
    })
    } catch (error) {
        log.info(error)
    }

}

module.exports = main

I'm packaging using electron-builder, i'm not using webpack.

This is my package-json

{
  "name": "wppclient",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "dist": "electron-builder"
  },
  "build": {
    "appId": "com.example.app",
    "publish": [
      {
        "provider": "github",
        "owner": "pedrosarkis",
        "repo": "electron-bot"
      }
    ],
    "win": {
      "target": [
        "nsis",
        "portable"
      ]
    }
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "csv-parse": "^5.5.6",
    "csv2json": "^2.0.2",
    "electron-log": "^5.1.5",
    "electron-squirrel-startup": "^1.0.1",
    "electron-updater": "^6.2.1",
    "json2csv": "^6.0.0-alpha.2",
    "puppeteer": "^22.10.0",
    "puppeteer-core": "^22.11.1",
    "puppeteer-in-electron": "^3.0.5",
    "whatsapp-web-electron.js": "^1.23.0-1",
    "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus"
  },
  "devDependencies": {
    "electron": "^31.0.1",
    "electron-builder": "^24.13.3"
  }
}

Everything works fine when I run in dev mode using npm start, the trigger "ready" works right, but in package mode I scan the qr code and the trigger doesn't work.

I read every issue in the lib https://github.com/pedroslopez/whatsapp-web.js, but I couldn't find something useful to my problem

0

Browse other questions tagged or ask your own question.