我正在开发一个 Electron 应用程序,它使用
@whiskeysocket/baileys
库与 WhatsApp 集成。该应用程序包括用于 WhatsApp Web 登录、消息接收和媒体处理的二维码生成等功能。我还使用 qr-code
库来生成二维码。
应用程序在开发阶段可以正常工作。但是,当我将其构建为可执行文件 (.exe) 时,它会停止正常运行。具体来说,WhatsApp 集成功能无法按预期工作。
这是我的主要流程代码的简化版本:
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const { makeWASocket, useMultiFileAuthState } = require('@whiskeysocket/baileys');
const qrcode = require('qrcode');
const Datastore = require('nedb');
let mainWindow;
let whatsappClient;
const appName = process.env.APPNAME;
const appData = app.getPath('userData');
const dbPath = path.join(appData, appName, "databases", "whatsapp.db");
const mediaPath = path.join(appData, appName, "databases", "whatsapp_media");
let whatsappDB = new Datastore({ filename: dbPath, autoload: true });
async function createWhatsAppClient() {
try {
const authPath = path.join(appData, appName, "whatsapp_auth");
const { state, saveCreds } = await useMultiFileAuthState(authPath);
whatsappClient = makeWASocket({
printQRInTerminal: true,
auth: state,
connectTimeoutMs: 60000,
defaultQueryTimeoutMs: 60000,
});
whatsappClient.ev.on("connection.update", async (update) => {
const { connection, lastDisconnect, qr } = update || {};
if (qr) {
const qrDataURL = await qrcode.toDataURL(qr);
mainWindow.webContents.send('qr-code', qrDataURL);
}
if (connection === 'open') {
mainWindow.webContents.send('whatsapp-ready');
}
});
whatsappClient.ev.on('messages.upsert', async (m) => {
// Message handling logic here
});
whatsappClient.ev.on("creds.update", saveCreds);
} catch (error) {
console.error('Error in createWhatsAppClient: ', error);
}
}
// IPC handlers and other main process code...
这是我的渲染器处理代码的一部分:
const { ipcRenderer } = require('electron');
document.addEventListener('DOMContentLoaded', () => {
const qrContainer = document.getElementById('qr-container');
const connectionStatus = document.getElementById('connection-status');
const messageList = document.getElementById('message-list');
ipcRenderer.on('qr-code', (event, qr) => {
qrContainer.innerHTML = '';
const qrImage = new Image();
qrImage.src = qr;
qrContainer.appendChild(qrImage);
connectionStatus.textContent = 'Scan this QR code with WhatsApp';
});
ipcRenderer.on('whatsapp-ready', () => {
qrContainer.innerHTML = '';
connectionStatus.textContent = 'WhatsApp is connected!';
loadMessages();
});
// Other event listeners and functions...
});
任何见解或建议将不胜感激。谢谢!
app.getPath('userData')
。package.json
中正确列出。不知道你是否已经解决了这个问题。 您可能会遇到的一些问题:
你可能想要这样的东西:
import makeWASocketCont, {
Browsers,
fetchLatestBaileysVersion,
makeCacheableSignalKeyStore,
makeInMemoryStore,
useMultiFileAuthState,
} from "@whiskeysockets/baileys";
const docsFolders = await getDefaultFolder();
const logger = P({ level: "fatal" });
const store = makeInMemoryStore({ logger });
const storeFile = docsFolders + "/baileys_store_multi.json";
const multiFileAuth = docsFolders + "/auth_info_baileys";
try {
JSON.parse(readFileSync(storeFile).toString());
} catch (error) {
try {
unlinkSync(storeFile);
} catch (error) {}
}
store?.readFromFile(storeFile);
setInterval(() => {
log.log({ event: "WRITE_WPP_STORE_FILE", status: "done" });
store?.writeToFile(storeFile);
}, 10_000);
const { state, saveCreds } = await useMultiFileAuthState(multiFileAuth);
const { version } = await fetchLatestBaileysVersion();
let sock = makeWASocket({
version,
browser: Browsers.windows("Desktop"),
printQRInTerminal: false,
auth: {
creds: state.creds,
/** caching makes the store faster to send/recv messages */
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
logger,
});
为了更好地调试它,请添加一个记录器,例如 https://www.npmjs.com/package/electron-log,并检查它生成的文件。 然后,将在应用程序目录的文件夹内创建启动的每个 console.log 和 error.log。此外,您可以为其提供传输以将用户日志发送到您的服务器,以便您可以远程检测错误和问题。