40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
const express = require('express');
|
|
const { Zalo } = require('zca-js');
|
|
const BotController = require('./src/controllers/BotController');
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
const PORT = 3000;
|
|
|
|
// 1. Cấu hình thông tin đăng nhập Zalo (Lấy từ trình duyệt)
|
|
// Mẹo: Bạn có thể đưa vào file .env sau này để bảo mật hơn
|
|
const zaloConfig = {
|
|
cookie: "YOUR_ZALO_COOKIE_HERE", // Thay bằng cookie thật
|
|
imei: "YOUR_IMEI_HERE" // Thay bằng imei thật
|
|
};
|
|
|
|
console.log("Đang khởi tạo Zalo Client...");
|
|
const zalo = new Zalo(zaloConfig);
|
|
|
|
// 2. Khởi tạo Controller với Zalo Client
|
|
const botController = new BotController(zalo);
|
|
|
|
// 3. Đăng nhập và cấu hình API
|
|
zalo.login().then(() => {
|
|
console.log("✅ Đã đăng nhập Zalo thành công!");
|
|
|
|
// Khai báo API Endpoint để n8n gọi vào
|
|
// Định tuyến (Route) POST /api/send sẽ do BotController xử lý
|
|
app.post('/api/send', (req, res) => botController.sendMessage(req, res));
|
|
|
|
// Bật server Express
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 API Server đang chạy tại: http://localhost:${PORT}`);
|
|
console.log(`👉 Bạn có thể cấu hình n8n gửi request về: http://localhost:${PORT}/api/send`);
|
|
});
|
|
|
|
}).catch((err) => {
|
|
console.error("❌ Đăng nhập Zalo thất bại. Vui lòng kiểm tra lại Cookie/IMEI:", err.message);
|
|
});
|