new(basic.gblib): SEND FILE pdf as temporary images.

This commit is contained in:
me@rodrigorodriguez.com 2024-10-02 19:04:20 -03:00
parent 7013d168df
commit 6c0d3ac311
2 changed files with 52 additions and 9 deletions

View file

@ -1463,7 +1463,6 @@ export class DialogKeywords {
await CollectionUtil.asyncForEach(pngs, async png => { await CollectionUtil.asyncForEach(pngs, async png => {
// Prepare a cache to be referenced by Bot Framework. // Prepare a cache to be referenced by Bot Framework.
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName)); url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(png.localName));
@ -1478,7 +1477,7 @@ export class DialogKeywords {
}); });
if (channel === 'omnichannel' || !user) { if (channel === 'omnichannel' || !user) {
await min.conversationalService.sendFile(min, null, mobile, url, caption); await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption);
} else { } else {
await min.conversationalService['sendOnConversation'](min, user, reply); await min.conversationalService['sendOnConversation'](min, user, reply);
} }

View file

@ -693,7 +693,7 @@ export class WhatsappDirectLine extends GBService {
return `${attachment.content.title} - ${attachment.content.text}`; return `${attachment.content.title} - ${attachment.content.text}`;
} }
public async sendFileToDevice(to, url, filename, caption, chatId) { public async sendFileToDevice(to, url, filename, caption, chatId, viewOnce) {
let options; let options;
switch (this.provider) { switch (this.provider) {
case 'meta': case 'meta':
@ -707,9 +707,13 @@ export class WhatsappDirectLine extends GBService {
whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber; whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey; whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
} }
if (viewOnce){
this.sendImageViewOnce(to, url, caption);
}
else{
const driver = createBot(whatsappServiceNumber, whatsappServiceKey); const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
await driver.sendImage(to, url, { caption: caption }); await driver.sendImage(to, url, { caption: caption });
}
break; break;
case 'GeneralBots': case 'GeneralBots':
@ -761,7 +765,47 @@ export class WhatsappDirectLine extends GBService {
public async sendTextAsAudioToDevice(to, msg: string, chatId) { public async sendTextAsAudioToDevice(to, msg: string, chatId) {
const url = await GBConversationalService.getAudioBufferFromText(msg); const url = await GBConversationalService.getAudioBufferFromText(msg);
await this.sendFileToDevice(to, url, 'Audio', msg, chatId); await this.sendFileToDevice(to, url, 'Audio', msg, chatId, false);
}
public async sendImageViewOnce(mobile, imageUrl, caption = '') {
// Define the API base URL and endpoints
const baseUrl = 'https://graph.facebook.com/v20.0'; // API version 20.0
const accessToken = this.whatsappServiceKey;
const sendMessageEndpoint = `${baseUrl}/${this.whatsappBusinessManagerId}/messages`;
const messageData = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
to: mobile,
type: 'image',
image: {
link: imageUrl,
caption: caption
},
view_once: true
};
const response = await fetch(sendMessageEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(messageData)
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`Failed to send image: ${JSON.stringify(errorData)}`);
}
const result = await response.json();
GBLogEx.info(0, 'Image sent successfully:' + result);
return result;
} }
// Function to create or update a template using WhatsApp Business API // Function to create or update a template using WhatsApp Business API