-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
69 lines (56 loc) · 1.73 KB
/
Copy pathindex.ts
File metadata and controls
69 lines (56 loc) · 1.73 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
67
68
69
/**
* Main entry point for the ChainPaye WhatsApp bot
* This file initializes the bot, connects to MongoDB,
* and sets up the core bot functionality
*/
import { connectDatabase } from "./config/database";
import "./config/init";
import { logger } from "./utils/logger";
import { app } from "./webhooks";
logger.info("Environment variables loaded");
/**
* Main bot initialization function
*/
const { PORT = "3000" } = process.env;
async function initializeBot() {
try {
// Connect to MongoDB
await connectDatabase();
logger.info("Database connected successfully");
logger.info("ChainPaye WhatsApp bot initialized successfully");
// Keep the process running
logger.info("Bot is running and waiting for messages...");
} catch (error) {
logger.error("Failed to initialize bot:", error);
process.exit(1);
}
}
// Handle graceful shutdown
process.on("SIGTERM", () => {
logger.info("SIGTERM received, shutting down gracefully");
process.exit(0);
});
process.on("SIGINT", () => {
logger.info("SIGINT received, shutting down gracefully");
process.exit(0);
});
// Handle uncaught exceptions
process.on("uncaughtException", (error) => {
logger.error("Uncaught Exception:", error);
process.exit(1);
});
// process.on("unhandledRejection", (reason, promise) => {
// logger.error("Unhandled Rejection at:", promise, "reason:", reason);
// process.exit(1);
// });
// Start the bot
initializeBot().then(() => {
// Initialize WhatsApp bot
logger.info("Initializing ChainPaye WhatsApp bot...");
// TODO: Set up webhook listeners
// TODO: Initialize message handlers
app.listen(PORT, (error) => {
if (error) return console.error("Error starting server", error);
console.log("Server online...");
});
});