@fatihjr/wa-wrapper
Version:
Baileys WhatsApp Web API wrapper.
111 lines (110 loc) • 5.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const baileys_1 = __importStar(require("@whiskeysockets/baileys"));
const pino_1 = __importDefault(require("pino"));
const node_events_1 = __importDefault(require("node:events"));
const path_1 = __importDefault(require("path"));
class WaSockets extends node_events_1.default {
constructor({ folderPath, connectionName }) {
super();
this.BaileysWARetry = new Map();
this.connectionName = connectionName;
this.folderAuthPath = path_1.default.join(folderPath, `auth_${connectionName}`);
}
isNullOrEmpty(value) {
return value === null || value === undefined || value === "";
}
pushToMapAndDelete(message) {
this.BaileysWARetry.set(message.id, { message });
setTimeout(() => this.BaileysWARetry.delete(message.id), 20000);
}
async initializeWASocket() {
const { state, saveCreds } = await (0, baileys_1.useMultiFileAuthState)(this.folderAuthPath);
const sock = (0, baileys_1.default)({
printQRInTerminal: false,
auth: state,
logger: (0, pino_1.default)({ level: "silent" }),
markOnlineOnConnect: false,
generateHighQualityLinkPreview: true,
getMessage: async (key) => {
var _a, _b, _c;
const messageFind = (_c = (_b = (_a = this.BaileysWARetry.get(key)) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.key) === null || _c === void 0 ? void 0 : _c.id;
return messageFind == key.id ? messageFind.message : undefined;
},
syncFullHistory: true,
});
return { sock, state, saveCreds };
}
async sockIsReady({ sock, state, saveCreds }) {
return new Promise((resolve, reject) => {
sock.ev.on("creds.update", saveCreds);
sock.ev.on("connection.update", async (update) => {
var _a, _b, _c, _d, _e, _f;
const { connection, lastDisconnect, qr } = update;
if (qr) {
this.emit("qr-string", { connectionName: this.connectionName, qr });
}
if (connection === "close" && lastDisconnect.error.message === "CLOSE SOCKET") {
return;
}
if (connection === "close") {
const shouldReconnect = ((_b = (_a = lastDisconnect.error) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode) ? ((_d = (_c = lastDisconnect.error) === null || _c === void 0 ? void 0 : _c.output) === null || _d === void 0 ? void 0 : _d.statusCode) !== baileys_1.DisconnectReason.loggedOut : false;
console.log("connection closed due to ", lastDisconnect.error, ", reconnecting ", shouldReconnect);
if (((_f = (_e = lastDisconnect.error) === null || _e === void 0 ? void 0 : _e.output) === null || _f === void 0 ? void 0 : _f.payload.error) == "Unauthorized") {
const { rmSync } = await Promise.resolve().then(() => __importStar(require("fs")));
rmSync(this.folderAuthPath, { recursive: true, force: true });
}
if (shouldReconnect) {
reject("RECONNECT");
}
else {
reject(lastDisconnect.error);
}
}
else if (connection === "open") {
console.log("Established Connection");
resolve(sock);
}
});
});
}
async connectToWhatsApp() {
try {
const socket = await this.initializeWASocket();
return await this.sockIsReady({ ...socket });
}
catch (error) {
console.log("ERROR ON BlastBot.ts:", error);
if (error == "RECONNECT") {
return await this.connectToWhatsApp();
}
}
}
}
exports.default = WaSockets;