Whatsapp service updates

This commit is contained in:
Darren Clarke 2024-05-16 18:22:10 +02:00
parent e22a8e8d98
commit 3da103c010
16 changed files with 151 additions and 36 deletions

View file

@ -89,8 +89,8 @@ export default class WhatsappService extends Service {
if (qr) {
console.log("got qr code");
const botDirectory = this.getBotDirectory(botID);
const qrPath = `${botDirectory}/qr.png`;
fs.writeFileSync(qrPath, qr, "base64");
const qrPath = `${botDirectory}/qr.txt`;
fs.writeFileSync(qrPath, qr, "utf8");
const verifiedFile = `${botDirectory}/verified`;
if (fs.existsSync(verifiedFile)) {
fs.rmSync(verifiedFile);
@ -220,13 +220,16 @@ export default class WhatsappService extends Service {
whatsappBotId: botID,
};
await fetch(`http://localhost:3000/api/whatsapp/${botID}/receive`, {
method: "POST",
headers: {
"Content-Type": "application/json",
await fetch(
`${process.env.BRIDGE_FRONTEND_URL}/api/whatsapp/bots/${botID}/receive`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(receivedMessage),
},
body: JSON.stringify(receivedMessage),
});
);
}
}
}
@ -242,9 +245,9 @@ export default class WhatsappService extends Service {
getBot(botID: string): Record<string, any> {
const botDirectory = this.getBotDirectory(botID);
const qrPath = `${botDirectory}/qr.png`;
const qrPath = `${botDirectory}/qr.txt`;
const verifiedFile = `${botDirectory}/verified`;
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "base64") : null;
const qr = fs.existsSync(qrPath) ? fs.readFileSync(qrPath, "utf8") : null;
const verified = fs.existsSync(verifiedFile);
return { qr, verified };
@ -276,6 +279,7 @@ export default class WhatsappService extends Service {
_lastReceivedDate: Date,
): Promise<proto.IWebMessageInfo[]> {
const connection = this.connections[botID]?.socket;
console.log({ connection });
const messages = await connection.loadAllUnreadMessages();
return messages;