2024-05-07 14:16:01 +02:00
|
|
|
import { Server } from "@hapi/hapi";
|
|
|
|
|
import { Service } from "@hapipal/schmervice";
|
|
|
|
|
import makeWASocket, {
|
|
|
|
|
DisconnectReason,
|
|
|
|
|
proto,
|
|
|
|
|
downloadContentFromMessage,
|
|
|
|
|
MediaType,
|
|
|
|
|
fetchLatestBaileysVersion,
|
|
|
|
|
isJidBroadcast,
|
|
|
|
|
isJidStatusBroadcast,
|
|
|
|
|
useMultiFileAuthState,
|
|
|
|
|
} from "@whiskeysockets/baileys";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
|
|
|
|
|
export type AuthCompleteCallback = (error?: string) => void;
|
|
|
|
|
|
|
|
|
|
export default class WhatsappService extends Service {
|
|
|
|
|
connections: { [key: string]: any } = {};
|
|
|
|
|
loginConnections: { [key: string]: any } = {};
|
|
|
|
|
|
|
|
|
|
static browserDescription: [string, string, string] = [
|
|
|
|
|
"Bridge",
|
|
|
|
|
"Chrome",
|
|
|
|
|
"2.0",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
constructor(server: Server, options: never) {
|
|
|
|
|
super(server, options);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 09:20:00 +02:00
|
|
|
getBaseDirectory(): string {
|
|
|
|
|
return `/home/node/baileys`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
getBotDirectory(id: string): string {
|
2024-05-17 09:20:00 +02:00
|
|
|
return `${this.getBaseDirectory()}/${id}`;
|
2024-05-15 14:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAuthDirectory(id: string): string {
|
|
|
|
|
return `${this.getBotDirectory(id)}/auth`;
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async initialize(): Promise<void> {
|
|
|
|
|
this.updateConnections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async teardown(): Promise<void> {
|
|
|
|
|
this.resetConnections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async sleep(ms: number): Promise<void> {
|
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async resetConnections() {
|
|
|
|
|
for (const connection of Object.values(this.connections)) {
|
|
|
|
|
try {
|
|
|
|
|
connection.end(null);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.connections = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async createConnection(
|
2024-05-15 14:39:33 +02:00
|
|
|
botID: string,
|
2024-05-07 14:16:01 +02:00
|
|
|
server: Server,
|
|
|
|
|
options: any,
|
|
|
|
|
authCompleteCallback?: any,
|
|
|
|
|
) {
|
2024-05-15 14:39:33 +02:00
|
|
|
const authDirectory = this.getAuthDirectory(botID);
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState(authDirectory);
|
2024-05-07 14:16:01 +02:00
|
|
|
const msgRetryCounterMap: any = {};
|
|
|
|
|
const socket = makeWASocket({
|
|
|
|
|
...options,
|
|
|
|
|
auth: state,
|
|
|
|
|
msgRetryCounterMap,
|
|
|
|
|
shouldIgnoreJid: (jid) =>
|
|
|
|
|
isJidBroadcast(jid) || isJidStatusBroadcast(jid),
|
|
|
|
|
});
|
|
|
|
|
let pause = 5000;
|
|
|
|
|
|
|
|
|
|
socket.ev.process(async (events) => {
|
|
|
|
|
if (events["connection.update"]) {
|
|
|
|
|
const update = events["connection.update"];
|
|
|
|
|
const {
|
|
|
|
|
connection: connectionState,
|
|
|
|
|
lastDisconnect,
|
|
|
|
|
qr,
|
|
|
|
|
isNewLogin,
|
|
|
|
|
} = update;
|
|
|
|
|
if (qr) {
|
|
|
|
|
console.log("got qr code");
|
2024-05-15 14:39:33 +02:00
|
|
|
const botDirectory = this.getBotDirectory(botID);
|
2024-05-16 18:22:10 +02:00
|
|
|
const qrPath = `${botDirectory}/qr.txt`;
|
|
|
|
|
fs.writeFileSync(qrPath, qr, "utf8");
|
2024-05-15 14:39:33 +02:00
|
|
|
const verifiedFile = `${botDirectory}/verified`;
|
|
|
|
|
if (fs.existsSync(verifiedFile)) {
|
|
|
|
|
fs.rmSync(verifiedFile);
|
|
|
|
|
}
|
2024-05-07 14:16:01 +02:00
|
|
|
} else if (isNewLogin) {
|
|
|
|
|
console.log("got new login");
|
2024-05-15 14:39:33 +02:00
|
|
|
const botDirectory = this.getBotDirectory(botID);
|
|
|
|
|
const verifiedFile = `${botDirectory}/verified`;
|
|
|
|
|
fs.writeFileSync(verifiedFile, "");
|
2024-05-07 14:16:01 +02:00
|
|
|
} else if (connectionState === "open") {
|
|
|
|
|
console.log("opened connection");
|
|
|
|
|
} else if (connectionState === "close") {
|
|
|
|
|
console.log("connection closed due to ", lastDisconnect.error);
|
|
|
|
|
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
|
|
|
|
|
?.statusCode;
|
|
|
|
|
|
|
|
|
|
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
|
|
|
|
console.log("reconnecting after got new login");
|
2024-05-15 14:39:33 +02:00
|
|
|
await this.createConnection(botID, server, options);
|
2024-05-07 14:16:01 +02:00
|
|
|
authCompleteCallback?.();
|
|
|
|
|
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
console.log("reconnecting");
|
|
|
|
|
await this.sleep(pause);
|
|
|
|
|
pause *= 2;
|
2024-05-15 14:39:33 +02:00
|
|
|
this.createConnection(botID, server, options);
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events["creds.update"]) {
|
|
|
|
|
console.log("creds update");
|
|
|
|
|
await saveCreds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (events["messages.upsert"]) {
|
|
|
|
|
console.log("messages upsert");
|
|
|
|
|
const upsert = events["messages.upsert"];
|
|
|
|
|
const { messages } = upsert;
|
|
|
|
|
if (messages) {
|
2024-05-15 14:39:33 +02:00
|
|
|
await this.queueUnreadMessages(botID, messages);
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
this.connections[botID] = { socket, msgRetryCounterMap };
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async updateConnections() {
|
|
|
|
|
this.resetConnections();
|
2024-05-15 14:39:33 +02:00
|
|
|
|
2024-05-17 09:20:00 +02:00
|
|
|
const baseDirectory = this.getBaseDirectory();
|
|
|
|
|
const botIDs = fs.readdirSync(baseDirectory);
|
2024-05-15 14:39:33 +02:00
|
|
|
console.log({ botIDs });
|
|
|
|
|
for await (const botID of botIDs) {
|
|
|
|
|
const directory = this.getBotDirectory(botID);
|
|
|
|
|
const verifiedFile = `${directory}/verified`;
|
|
|
|
|
if (fs.existsSync(verifiedFile)) {
|
2024-05-07 14:16:01 +02:00
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
await this.createConnection(botID, this.server, {
|
2024-05-07 14:16:01 +02:00
|
|
|
browser: WhatsappService.browserDescription,
|
2024-05-15 14:39:33 +02:00
|
|
|
printQRInTerminal: true,
|
2024-05-07 14:16:01 +02:00
|
|
|
version,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async queueMessage(
|
2024-05-15 14:39:33 +02:00
|
|
|
botID: string,
|
2024-05-07 14:16:01 +02:00
|
|
|
webMessageInfo: proto.IWebMessageInfo,
|
|
|
|
|
) {
|
|
|
|
|
const {
|
|
|
|
|
key: { id, fromMe, remoteJid },
|
|
|
|
|
message,
|
|
|
|
|
messageTimestamp,
|
|
|
|
|
} = webMessageInfo;
|
|
|
|
|
if (!fromMe && message && remoteJid !== "status@broadcast") {
|
|
|
|
|
const { audioMessage, documentMessage, imageMessage, videoMessage } =
|
|
|
|
|
message;
|
|
|
|
|
const isMediaMessage =
|
|
|
|
|
audioMessage || documentMessage || imageMessage || videoMessage;
|
|
|
|
|
|
|
|
|
|
const messageContent = Object.values(message)[0];
|
|
|
|
|
let messageType: MediaType;
|
|
|
|
|
let attachment: string;
|
|
|
|
|
let filename: string;
|
|
|
|
|
let mimetype: string;
|
|
|
|
|
if (isMediaMessage) {
|
|
|
|
|
if (audioMessage) {
|
|
|
|
|
messageType = "audio";
|
|
|
|
|
filename = id + "." + audioMessage.mimetype.split("/").pop();
|
|
|
|
|
mimetype = audioMessage.mimetype;
|
|
|
|
|
} else if (documentMessage) {
|
|
|
|
|
messageType = "document";
|
|
|
|
|
filename = documentMessage.fileName;
|
|
|
|
|
mimetype = documentMessage.mimetype;
|
|
|
|
|
} else if (imageMessage) {
|
|
|
|
|
messageType = "image";
|
|
|
|
|
filename = id + "." + imageMessage.mimetype.split("/").pop();
|
|
|
|
|
mimetype = imageMessage.mimetype;
|
|
|
|
|
} else if (videoMessage) {
|
|
|
|
|
messageType = "video";
|
|
|
|
|
filename = id + "." + videoMessage.mimetype.split("/").pop();
|
|
|
|
|
mimetype = videoMessage.mimetype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stream = await downloadContentFromMessage(
|
|
|
|
|
messageContent,
|
|
|
|
|
messageType,
|
|
|
|
|
);
|
|
|
|
|
let buffer = Buffer.from([]);
|
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
|
buffer = Buffer.concat([buffer, chunk]);
|
|
|
|
|
}
|
|
|
|
|
attachment = buffer.toString("base64");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageContent || attachment) {
|
|
|
|
|
const receivedMessage = {
|
|
|
|
|
waMessageId: id,
|
|
|
|
|
waMessage: JSON.stringify(webMessageInfo),
|
|
|
|
|
waTimestamp: new Date((messageTimestamp as number) * 1000),
|
|
|
|
|
attachment,
|
|
|
|
|
filename,
|
|
|
|
|
mimetype,
|
2024-05-15 14:39:33 +02:00
|
|
|
whatsappBotId: botID,
|
2024-05-07 14:16:01 +02:00
|
|
|
};
|
|
|
|
|
|
2024-05-17 09:20:00 +02:00
|
|
|
const message =
|
|
|
|
|
webMessageInfo?.message?.conversation ??
|
|
|
|
|
webMessageInfo?.message?.extendedTextMessage?.text ??
|
|
|
|
|
webMessageInfo?.message?.imageMessage?.caption ??
|
|
|
|
|
webMessageInfo?.message?.videoMessage?.caption;
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
message,
|
|
|
|
|
sender: webMessageInfo.key.remoteJid?.split("@")[0],
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-16 18:22:10 +02:00
|
|
|
await fetch(
|
|
|
|
|
`${process.env.BRIDGE_FRONTEND_URL}/api/whatsapp/bots/${botID}/receive`,
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
2024-05-17 09:20:00 +02:00
|
|
|
body: JSON.stringify(payload),
|
2024-05-15 14:39:33 +02:00
|
|
|
},
|
2024-05-16 18:22:10 +02:00
|
|
|
);
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async queueUnreadMessages(
|
2024-05-15 14:39:33 +02:00
|
|
|
botID: string,
|
2024-05-07 14:16:01 +02:00
|
|
|
messages: proto.IWebMessageInfo[],
|
|
|
|
|
) {
|
|
|
|
|
for await (const message of messages) {
|
2024-05-15 14:39:33 +02:00
|
|
|
await this.queueMessage(botID, message);
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
getBot(botID: string): Record<string, any> {
|
|
|
|
|
const botDirectory = this.getBotDirectory(botID);
|
2024-05-16 18:22:10 +02:00
|
|
|
const qrPath = `${botDirectory}/qr.txt`;
|
2024-05-15 14:39:33 +02:00
|
|
|
const verifiedFile = `${botDirectory}/verified`;
|
2024-05-16 18:22:10 +02:00
|
|
|
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "utf8") : null;
|
2024-05-15 14:39:33 +02:00
|
|
|
const verified = fs.existsSync(verifiedFile);
|
2024-05-07 14:16:01 +02:00
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
return { qr, verified };
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-15 14:39:33 +02:00
|
|
|
async unverify(botID: string): Promise<void> {
|
|
|
|
|
const botDirectory = this.getBotDirectory(botID);
|
|
|
|
|
fs.rmSync(botDirectory, { recursive: true, force: true });
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-17 09:20:00 +02:00
|
|
|
async register(
|
|
|
|
|
botID: string,
|
|
|
|
|
callback?: AuthCompleteCallback,
|
|
|
|
|
): Promise<void> {
|
2024-05-07 14:16:01 +02:00
|
|
|
const { version } = await fetchLatestBaileysVersion();
|
2024-05-17 09:20:00 +02:00
|
|
|
await this.createConnection(
|
|
|
|
|
botID,
|
|
|
|
|
this.server,
|
|
|
|
|
{ version, browser: WhatsappService.browserDescription },
|
|
|
|
|
callback,
|
|
|
|
|
);
|
|
|
|
|
callback?.();
|
2024-05-07 14:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async send(
|
2024-05-15 14:39:33 +02:00
|
|
|
botID: string,
|
2024-05-07 14:16:01 +02:00
|
|
|
phoneNumber: string,
|
|
|
|
|
message: string,
|
|
|
|
|
): Promise<void> {
|
2024-05-15 14:39:33 +02:00
|
|
|
const connection = this.connections[botID]?.socket;
|
2024-05-07 14:16:01 +02:00
|
|
|
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
|
|
|
|
|
await connection.sendMessage(recipient, { text: message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async receive(
|
2024-05-15 14:39:33 +02:00
|
|
|
botID: string,
|
2024-05-07 14:16:01 +02:00
|
|
|
_lastReceivedDate: Date,
|
|
|
|
|
): Promise<proto.IWebMessageInfo[]> {
|
2024-05-15 14:39:33 +02:00
|
|
|
const connection = this.connections[botID]?.socket;
|
2024-05-16 18:22:10 +02:00
|
|
|
console.log({ connection });
|
2024-05-07 14:16:01 +02:00
|
|
|
const messages = await connection.loadAllUnreadMessages();
|
2024-05-15 14:39:33 +02:00
|
|
|
|
2024-05-07 14:16:01 +02:00
|
|
|
return messages;
|
|
|
|
|
}
|
|
|
|
|
}
|