Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 70 additions & 12 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import pkg from "whatsapp-web.js";
import qrcode from "qrcode-terminal";
import chalk from "chalk";
import { handleMessages } from "./services/messageHandler.service.js";
import { getBanner } from "./utils/banner.js";
import { getStorageDir } from "./storage/configStore.js";
Expand All @@ -11,6 +12,33 @@ type ClientType = import("whatsapp-web.js").Client;

const { Client, LocalAuth } = pkg;

// ==========================================
// GLOBAL SAFETY NET FOR SILENT BACKGROUND CRASHES
// ==========================================
process.on("unhandledRejection", (reason: unknown) => {
console.error('\n' + chalk.red.bold('✕ Critical Application Error (Unhandled Rejection)!'));

if (reason instanceof Error) {
console.error(chalk.dim(`Details: ${reason.message}`));
} else {
console.error(chalk.dim(`Details: ${String(reason)}`));
}

console.error(chalk.yellow('\nPossible Root Causes:'));
console.error(' • Network is completely offline (Wi-Fi turned off).');
console.error(' • Puppeteer hit a silent timeout waiting for WhatsApp Web.');

console.error(chalk.cyan('\n👉 Action: Check your connection and restart with: ') + chalk.green('npm run dev\n'));
process.exit(1);
});

process.on("uncaughtException", (error: Error) => {
console.error('\n' + chalk.red.bold('✕ Unexpected System Exception!'));
console.error(chalk.dim(`Error: ${error.message}`));
process.exit(1);
});
// ==========================================

export class WhatsAppBot {
private client: ClientType;
private username: string;
Expand All @@ -34,43 +62,73 @@ export class WhatsAppBot {

private initializeEvents() {
this.client.on("qr", (qr) => {
console.log("Scan QR to login:");
console.log(chalk.cyan("\n📱 Action Required: Scan the QR code to log in:"));
qrcode.generate(qr, { small: true });
});

this.client.on("ready", async () => {
try {
console.clear();
await getBanner(this.agentName, this.username);
console.log(chalk.green.bold("\n🚀 chat-buddy is live and monitoring messages!"));
} catch (err) {
console.log(err);
console.error(chalk.red("Failed to load welcome banner:"), err);
}
});

this.client.on("auth_failure", (msg) => {
console.log("Auth failed:", msg);
console.error('\n' + chalk.red.bold('✕ Authentication Failed!'));
console.error(chalk.yellow(`Details: ${msg}`));
console.error(chalk.cyan('👉 Solution: Your session might be invalid. Try clearing your local cache or storage directory, then restart.'));
process.exit(1);
});

this.client.on("disconnected", (reason) => {
console.log("Disconnected:", reason);
console.log("Reconnecting...");
this.client.initialize();
console.warn('\n' + chalk.yellow.bold('⚠️ WhatsApp Connection Lost!'));
console.error(chalk.dim(`Reason: ${reason}`));

console.error(chalk.cyan('\n🛠️ How to recover:'));
console.error(` 1. Verify your computer's internet connection.`);
console.error(` 2. Rerun the command to re-authenticate: ${chalk.green('npm run dev')}\n`);

process.exit(0);
});

this.client.on("message", async (message) => {
try {
await handleMessages(message, this.username, this.agentName);
} catch (err) {
console.log("Message error:", err);
console.error(chalk.red("\n❌ Message processing encountered an error:"), err);
}
});
}

public start() {
this.client.initialize().catch((err) => {
console.log(err);
});
/**
* Starts the WhatsApp client and captures initialization failures
*/
public async start(): Promise<void> {
console.log(chalk.blue("⏳ Initializing WhatsApp client connection..."));

try {
await this.client.initialize();
} catch (err: unknown) {
console.error('\n' + chalk.red.bold('✕ Client Initialization Failed!'));

if (err instanceof Error) {
console.error(chalk.dim(`Error Message: ${err.message}`));
} else {
console.error(chalk.dim(`Error Details: ${String(err)}`));
}

console.error(chalk.yellow('\nPossible Root Causes:'));
console.error(' • Network is completely unreachable (Wi-Fi turned off).');
console.error(' • Chromium/Puppeteer timed out trying to reach the WhatsApp server.');

console.error(chalk.cyan('\n👉 Action: Please check your internet connection and rerun the command.\n'));

process.exit(1);
}
}
}

export const botRebootTime = Date.now();
export const botRebootTime = Date.now();