启动机器人时出错:打开浏览器时出错 ...
// src/server.js import venom from 'venom-bot'; import express from 'express';
const app = express(); const PORT = 3000;
// Use JSON app.use(express.json());
let clientInstance = null;
// Initialize Venom venom .create({ session: 'bot', headless: true, }) .then((client) => { clientInstance = client; console.log('Bot initialized successfully'); }) .catch((error) => console.log('Error while initializing the bot:', error));
// Endpoint for sending messages app.post('/send', async (req, res) => { const { number, message } = req.body;
if (!clientInstance) { return res.status(503).json({ error: 'Client not yet initialized' }); }
if (!number || !message) { return res.status(400).json({ error: 'Number and message are required' }); }
try {
const formattedNumber = ${number}@c.us;
await clientInstance.sendText(formattedNumber, message);
res.json({ success: true, message: 'Message sent successfully' });
} catch (err) {
console.error('Error while sending message:', err);
res.status(500).json({ error: 'Error while sending message' });
}
});
// Start the Express server
app.listen(PORT, () => {
console.log(Express server running at http://localhost:${PORT});
});
内容来源: vynect/venom