-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (53 loc) · 1.89 KB
/
server.js
File metadata and controls
66 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const bodyParser = require("body-parser");
const { Client, LocalAuth } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
const app = express();
const PORT = 3001;
// Middleware
app.use(bodyParser.json());
// Initialize WhatsApp client
const client = new Client({
authStrategy: new LocalAuth(),
});
// Generate QR code for login
client.on("qr", (qr) => {
console.log("Scan this QR code to log in:");
qrcode.generate(qr, { small: true });
});
// When the client is ready
client.on("ready", () => {
console.log("WhatsApp client is ready!");
});
// Initialize the client
client.initialize();
// API endpoint for bulk messaging
app.post("/send-bulk-messages", async (req, res) => {
const { contacts, message } = req.body;
if (!contacts || !Array.isArray(contacts) || contacts.length === 0) {
return res.status(400).json({ error: "A list of contacts is required." });
}
if (!message) {
return res.status(400).json({ error: "Message is required." });
}
const results = [];
for (const contactNumber of contacts) {
try {
// Format the contact number
const chatId = `${contactNumber.replace(/\+/g, "")}@c.us`;
// Send the message
await client.sendMessage(chatId, message);
console.log(`Message sent to ${contactNumber}`);
results.push({ contact: contactNumber, status: "success" });
} catch (error) {
console.error(`Failed to send message to ${contactNumber}:`, error.message);
results.push({ contact: contactNumber, status: "error", error: error.message });
}
}
// Return the results for each contact
res.status(200).json({ status: "completed", results });
});
// Start the Express server
app.listen(PORT, () => {
console.log(`API server running at http://localhost:${PORT}`);
});