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
我使用 electro-builder 进行打包,我没有使用 webpack。
这是我的 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"
}
}
当我使用 npm start 在开发模式下运行时,一切正常,触发器“就绪”工作正常,但在包模式下,我扫描二维码,触发器不起作用。
我阅读了库中的每一个问题https://github.com/pedroslopez/whatsapp-web.js,但我找不到对我的问题有用的东西
我也面临同样的问题,你在什么情况下运行电子应用程序?