More Whatsapp Docker updates

This commit is contained in:
Darren Clarke 2024-05-17 09:20:00 +02:00
parent 3da103c010
commit e26e5832ff
8 changed files with 63 additions and 21 deletions

View file

@ -28,8 +28,12 @@ export default class WhatsappService extends Service {
super(server, options);
}
getBaseDirectory(): string {
return `/home/node/baileys`;
}
getBotDirectory(id: string): string {
return `/baileys/${id}`;
return `${this.getBaseDirectory()}/${id}`;
}
getAuthDirectory(id: string): string {
@ -141,7 +145,8 @@ export default class WhatsappService extends Service {
private async updateConnections() {
this.resetConnections();
const botIDs = fs.readdirSync("/baileys");
const baseDirectory = this.getBaseDirectory();
const botIDs = fs.readdirSync(baseDirectory);
console.log({ botIDs });
for await (const botID of botIDs) {
const directory = this.getBotDirectory(botID);
@ -220,6 +225,17 @@ export default class WhatsappService extends Service {
whatsappBotId: botID,
};
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],
};
await fetch(
`${process.env.BRIDGE_FRONTEND_URL}/api/whatsapp/bots/${botID}/receive`,
{
@ -227,7 +243,7 @@ export default class WhatsappService extends Service {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(receivedMessage),
body: JSON.stringify(payload),
},
);
}
@ -258,10 +274,18 @@ export default class WhatsappService extends Service {
fs.rmSync(botDirectory, { recursive: true, force: true });
}
async register(botID: string, callback: AuthCompleteCallback): Promise<void> {
async register(
botID: string,
callback?: AuthCompleteCallback,
): Promise<void> {
const { version } = await fetchLatestBaileysVersion();
await this.createConnection(botID, this.server, { version }, callback);
callback();
await this.createConnection(
botID,
this.server,
{ version, browser: WhatsappService.browserDescription },
callback,
);
callback?.();
}
async send(