UNPKG

nestwhats

Version:

A whatsapp-web.js wrapper for NestJS to create WhatsApp bots

81 lines (80 loc) 4.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestWhatsClientService = void 0; const common_1 = require("@nestjs/common"); const qrcode_1 = require("qrcode"); const whatsapp_web_js_1 = require("whatsapp-web.js"); const clients_registry_service_1 = require("./clients-registry.service"); class NestWhatsClientService { constructor(client, options, clientsRegistry, commandsService, listenerRegistry) { this.client = client; this.options = options; this.clientsRegistry = clientsRegistry; this.commandsService = commandsService; this.listenerRegistry = listenerRegistry; this.logger = new common_1.Logger(NestWhatsClientService.name); } onModuleInit() { this.clientsRegistry.add({ name: this.options.name, client: this.client, prefix: this.options.prefix, }); this.client.on(whatsapp_web_js_1.Events.QR_RECEIVED, (qr) => __awaiter(this, void 0, void 0, function* () { const dataUrl = yield (0, qrcode_1.toDataURL)(qr); this.clientsRegistry.updateStatus(this.options.name, clients_registry_service_1.ClientStatus.QrReceived, dataUrl); if (this.options.printQR) { (0, qrcode_1.toString)(qr, { type: "terminal", small: true }, (err, url) => { if (err) { this.logger.error(`[${this.options.name}] Error generating QR code: ${err.message}`); return; } this.logger.verbose(`[${this.options.name}] Scan the QR code below to authenticate:`); console.log(url); }); } })); this.client.on(whatsapp_web_js_1.Events.AUTHENTICATED, () => { this.clientsRegistry.updateStatus(this.options.name, clients_registry_service_1.ClientStatus.Authenticated); }); this.client.on(whatsapp_web_js_1.Events.READY, () => { this.clientsRegistry.updateStatus(this.options.name, clients_registry_service_1.ClientStatus.Ready); this.clientsRegistry.updateInfo(this.options.name, this.client.info.pushname, this.client.info.wid.user); }); this.client.on(whatsapp_web_js_1.Events.DISCONNECTED, () => { this.clientsRegistry.updateStatus(this.options.name, clients_registry_service_1.ClientStatus.Disconnected); }); this.client.on(whatsapp_web_js_1.Events.AUTHENTICATION_FAILURE, () => { this.clientsRegistry.updateStatus(this.options.name, clients_registry_service_1.ClientStatus.Disconnected); }); } onApplicationBootstrap() { for (const listener of this.listenerRegistry.getAll()) { const clients = listener.getClients(); if (clients && !clients.includes(this.options.name)) continue; this.client[listener.getType()](listener.getEvent(), (...args) => listener.execute(args)); } this.client.on(whatsapp_web_js_1.Events.MESSAGE_CREATE, (message) => this.commandsService.handle(message, this.options.name, this.options.prefix)); this.client.initialize().catch((err) => { this.logger.error(`[${this.options.name}] Client initialization failed: ${err instanceof Error ? err.message : String(err)}`); }); } onApplicationShutdown() { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${this.options.name}] Destroying client…`); yield this.client.destroy(); this.logger.log(`[${this.options.name}] Client destroyed`); }); } } exports.NestWhatsClientService = NestWhatsClientService;