fix(DialogKeywords): update file writing method and adjust WhatsApp file sending logic
All checks were successful
GBCI / build (push) Successful in 37s

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-04-19 14:27:23 -03:00
parent 7c05a95054
commit 638534e31f
2 changed files with 16 additions and 15 deletions

View file

@ -1555,6 +1555,7 @@ export class DialogKeywords {
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
GBLogEx.info(min, `Exposing ${localName} to ${url}...`);
} }
@ -1608,7 +1609,6 @@ export class DialogKeywords {
const localName = path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.${ext}`); const localName = path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.${ext}`);
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName)); url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
GBLogEx.info(min, `Exposing ${localName} to ${url}...`);
await fs.writeFile(localName, new Uint8Array(buf), { encoding: null }); await fs.writeFile(localName, new Uint8Array(buf), { encoding: null });
} }

View file

@ -734,8 +734,17 @@ export class WhatsappDirectLine extends GBService {
await this.sendImageViewOnce(to, url, caption); await this.sendImageViewOnce(to, url, caption);
} }
else { else {
const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
await driver.sendImage(to, url, { caption: caption }); const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
const fileExtension = path.extname(url).toLowerCase();
if (['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.ods', '.csv'].includes(fileExtension)) {
await driver.sendDocument(to, url, { caption: caption });
} else if (['.mp3', '.wav', '.ogg', '.aac', '.m4a'].includes(fileExtension)) {
await driver.sendAudio(to, url);
} else {
await driver.sendImage(to, url, { caption: caption });
}
} }
break; break;
@ -753,16 +762,8 @@ export class WhatsappDirectLine extends GBService {
await this.customClient.sendMessage(to, attachment, { caption: caption, isViewOnce }); await this.customClient.sendMessage(to, attachment, { caption: caption, isViewOnce });
break; break;
} }
GBLogEx.info(this.min, `File ${url} sent to ${to}.`);
if (options) {
try {
// tslint:disable-next-line: await-promise
const result = await fetch(url, options);
GBLogEx.info(this.min, `File ${url} sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending file to Whatsapp provider ${error.message}`);
}
}
} }
public async sendAudioToDevice(to, url) { public async sendAudioToDevice(to, url) {