Skip to content

Commit

Permalink
hopefully this works :D
Browse files Browse the repository at this point in the history
  • Loading branch information
YazeedAlKhalaf committed Feb 2, 2025
1 parent aa31114 commit c1d841d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
3 changes: 3 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ services:
condition: service_healthy
networks:
- internal
volumes:
- wasapp-wwebjs_auth:/app/.wwebjs_auth

volumes:
postgres_data:
Expand All @@ -139,6 +141,7 @@ volumes:
baikal-config:
baikal-data:
mongodb-data:
wasapp-wwebjs_auth:

networks:
web:
Expand Down
8 changes: 8 additions & 0 deletions wasapp/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ async function main() {
phoneNumber: string;
};

console.log("before initialize");

const pairingCode = await whatsappService.initialize(
customerId,
phoneNumber
);
console.log(`after initialize: ${pairingCode}`);
if (!pairingCode) {
console.log("failed to get pairing code");
throw new Error("Failed to get pairing code");
}

console.log("before sending pairing code");

res.send({ pairingCode: pairingCode });
} catch (error) {
console.log(`we got this error: ${error}`);
Expand Down Expand Up @@ -163,7 +169,9 @@ async function main() {
});
app.log.info(`👂 listening on: ${listenUrl}`);

console.log(`before initialize saved clients`);
await whatsappService.initializeSavedClients();
console.log(`after initialize saved clients`);
}

main();
52 changes: 34 additions & 18 deletions wasapp/src/services/whatsapp/whatsapp_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface ClientDetails {
export class WhatsappService {
private mongooseConn: Mongoose;
private clientsDetails: Map<string, ClientDetails>;
private logger: FastifyBaseLogger;
private makeHeadlessClients: boolean;

constructor(
Expand All @@ -30,7 +29,6 @@ export class WhatsappService {
) {
this.mongooseConn = mongooseConn;
this.clientsDetails = new Map<string, ClientDetails>();
this.logger = logger;
this.makeHeadlessClients = makeHeadlessClients;
}

Expand Down Expand Up @@ -79,7 +77,9 @@ export class WhatsappService {
});
return pairingCode;
} catch (error) {
this.logger.error({ error, customerId }, "Failed to get pairing code");
console.log(
`Failed to get pairing code | customerId: ${customerId} | error: ${error}`
);
this.updateClientDetails(customerId, { status: "DISCONNECTED" });
await this.disconnectClient(customerId);
throw error;
Expand All @@ -95,11 +95,13 @@ export class WhatsappService {
phoneNumber: string | null
) {
client.on("change_state", (state) => {
this.logger.info({ customerId, state }, "WhatsApp state changed");
console.log(
`[Customer: ${customerId}] WhatsApp state changed to ${state}`
);
});

client.on("ready", async () => {
this.logger.info({ customerId }, "WhatsApp client is ready");
console.log(`[Customer: ${customerId}] WhatsApp client is ready`);
this.updateClientDetails(customerId, {
status: "READY",
name: client.info.pushname,
Expand All @@ -108,40 +110,44 @@ export class WhatsappService {
});

client.on("authenticated", (session) => {
this.logger.info(
{ customerId, session },
"WhatsApp client authenticated"
console.log(
`[Customer: ${customerId}] WhatsApp client authenticated successfully`
);
this.updateClientDetails(customerId, { status: "AUTHENTICATED" });
});

client.on("message_create", (msg) => {
this.logger.info({ customerId, msg }, "WhatsApp message created");
console.log(
`[Customer: ${customerId}] New WhatsApp message | From: ${msg.from} | Type: ${msg.type} | Body: ${msg.body}`
);
});

client.on("auth_failure", async (msg) => {
this.logger.error({ customerId, msg }, "WhatsApp authentication failed");
console.error(
`[Customer: ${customerId}] WhatsApp authentication failed - ${msg}`
);
this.updateClientDetails(customerId, { status: "DISCONNECTED" });
await client.destroy();
this.clientsDetails.delete(customerId);
});

client.on("disconnected", async (reason) => {
this.logger.warn({ customerId, reason }, "WhatsApp client disconnected");
console.warn(
`[Customer: ${customerId}] WhatsApp client disconnected - Reason: ${reason}`
);
this.updateClientDetails(customerId, { status: "DISCONNECTED" });
await client.destroy();
this.clientsDetails.delete(customerId);
});

client.on("loading_screen", (percent, msg) => {
this.logger.debug(
{ customerId, percent, msg },
"WhatsApp loading screen update"
);
console.log(`[Customer: ${customerId}] Loading: ${percent}% - ${msg}`);
});

client.on("remote_session_saved", () => {
this.logger.info({ customerId }, "WhatsApp remote session saved");
console.log(
`[Customer: ${customerId}] WhatsApp remote session saved successfully`
);
});
}

Expand All @@ -153,7 +159,9 @@ export class WhatsappService {
private async getSavedCustomerIds(): Promise<string[]> {
const db = this.getMongoDb();
if (!db) {
this.logger.error("MongoDB connection not found");
console.error(
"[MongoDB] Connection not found - Unable to get saved customer IDs"
);
return [];
}

Expand All @@ -174,7 +182,13 @@ export class WhatsappService {
async initializeSavedClients() {
const customerIds = await this.getSavedCustomerIds();
customerIds.forEach(async (customerId) => {
console.log(
`[Customer: ${customerId}] Starting WhatsApp client initialization`
);
await this.initialize(customerId, null);
console.log(
`[Customer: ${customerId}] WhatsApp client initialization completed`
);
});
}

Expand Down Expand Up @@ -209,7 +223,9 @@ export class WhatsappService {

const db = this.getMongoDb();
if (!db) {
this.logger.error("MongoDB connection not found");
console.error(
`[Customer: ${customerId}] MongoDB connection not found - Unable to disconnect client`
);
return;
}
await db.dropCollection(`whatsapp-RemoteAuth-${customerId}.files`);
Expand Down

0 comments on commit c1d841d

Please sign in to comment.