fix(WhatsappDirectLine): streamline media handling and improve template creation
Some checks are pending
GBCI / build (push) Waiting to run

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-06-12 11:31:33 -03:00
parent 2da1e99c0d
commit 2e42c36ecb
2 changed files with 209 additions and 222 deletions

View file

@ -646,14 +646,10 @@ export class GBConversationalService {
text.toLowerCase().endsWith('.png') || text.toLowerCase().endsWith('.png') ||
text.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mp4') ||
text.toLowerCase().endsWith('.mov'); text.toLowerCase().endsWith('.mov');
let mediaFile = !isMedia ? /(.*)\n/gim.exec(text)[0].trim() : text;
let text1= /(.*)\n/gim.exec(text);
let mediaFile = !isMedia ? (text1? text1[0].trim() : text):text;
let mediaType = mediaFile.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image'; let mediaType = mediaFile.toLowerCase().endsWith('.mp4') || text.toLowerCase().endsWith('.mov') ? 'video' : 'image';
// Set folder based on media type. // Set folder based on media type
const folder = mediaType === 'video' ? 'videos' : 'images'; const folder = mediaType === 'video' ? 'videos' : 'images';
const gbaiName = GBUtil.getGBAIPath(min.botId); const gbaiName = GBUtil.getGBAIPath(min.botId);
const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile); const fileUrl = urlJoin(process.env.BOT_URL, 'kb', gbaiName, `${min.botId}.gbkb`, folder, mediaFile);
@ -669,9 +665,18 @@ export class GBConversationalService {
let data: any = { let data: any = {
name: template, name: template,
components: [ ] components: [
{
type: 'header',
parameters: [
{
type: mediaType
}
]
}
]
}; };
//data['components'][0]['parameters'][0][mediaType] = { link: urlMedia }; data['components'][0]['parameters'][0][mediaType] = { link: urlMedia };
await this.sendToMobile(min, mobile, data, null); await this.sendToMobile(min, mobile, data, null);
GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`); GBLogEx.info(min, `Sending answer file to mobile: ${mobile}. Header: ${urlMedia}`);

View file

@ -259,7 +259,7 @@ export class WhatsappDirectLine extends GBService {
default: default:
if (this.whatsappServiceUrl) { if (this.whatsappServiceUrl){
GBLog.verbose(`GBWhatsapp: Checking server...`); GBLog.verbose(`GBWhatsapp: Checking server...`);
let url = urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`; let url = urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`;
@ -834,11 +834,9 @@ export class WhatsappDirectLine extends GBService {
// Function to create or update a template using WhatsApp Business API // Function to create or update a template using WhatsApp Business API
public async createOrUpdateTemplate(min: GBMinInstance, template, text) { public async createOrUpdateTemplate(min: GBMinInstance, template, text) {
template = template.replace('.docx', '');
template = template.replace(/\-/gi, '_'); template = template.replace(/\-/gi, '_');
template = template.replace(/\./gi, '_'); template = template.replace(/\./gi, '_');
// Determine if media is image or video // Determine if media is image or video
let isMedia = let isMedia =
text.toLowerCase().endsWith('.jpg') || text.toLowerCase().endsWith('.jpg') ||
@ -863,17 +861,11 @@ export class WhatsappDirectLine extends GBService {
let data: any = { let data: any = {
name: template, name: template,
components: [ components: [
// { {
// type: 'HEADER', type: 'HEADER',
// format: 'TEXT', format: mediaType.toUpperCase(), // Use IMAGE or VIDEO format
// text: 'General Bots', example: { header_handle: [handleMedia] }
// example: { },
// header_text: [
// "General Bots"
// ]
// }
// },
{ {
type: 'BODY', type: 'BODY',
text: text text: text
@ -939,7 +931,7 @@ export class WhatsappDirectLine extends GBService {
} }
// New method to send button list // New method to send button list
private async sendButtonList(to: string, buttons: string[]) { private async sendButtonList(to: string, buttons: string[]) {
const baseUrl = 'https://graph.facebook.com/v20.0'; const baseUrl = 'https://graph.facebook.com/v20.0';
const accessToken = this.whatsappServiceKey; const accessToken = this.whatsappServiceKey;
const sendMessageEndpoint = `${baseUrl}/${this.whatsappServiceNumber}/messages`; const sendMessageEndpoint = `${baseUrl}/${this.whatsappServiceNumber}/messages`;
@ -983,7 +975,7 @@ export class WhatsappDirectLine extends GBService {
const result = await response.json(); const result = await response.json();
GBLogEx.info(this.min, 'Button list sent successfully:' + JSON.stringify(result)); GBLogEx.info(this.min, 'Button list sent successfully:' + JSON.stringify(result));
return result; return result;
} }
public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) { public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) {
@ -1181,7 +1173,7 @@ export class WhatsappDirectLine extends GBService {
if (process.env.AUDIO_DISABLED !== 'true') { if (process.env.AUDIO_DISABLED !== 'true') {
if (provider === 'meta') { if (provider ==='meta'){
const buf = await this.downloadAudio(req, min); const buf = await this.downloadAudio(req, min);
@ -1193,7 +1185,7 @@ export class WhatsappDirectLine extends GBService {
); );
} else if (req.type === 'ptt') { }else if (req.type === 'ptt') {
const media = await req.downloadMedia(); const media = await req.downloadMedia();
const buf = Buffer.from(media.data, 'base64'); const buf = Buffer.from(media.data, 'base64');
@ -1388,26 +1380,17 @@ export class WhatsappDirectLine extends GBService {
while (startOffset < fileSize) { while (startOffset < fileSize) {
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize); const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
const fileStream = createReadStream(filePath, { start: startOffset, end: endOffset - 1 });
const chunkSize = endOffset - startOffset; const chunkSize = endOffset - startOffset;
// Read the chunk into a buffer to get accurate size
const buffer = new Uint8Array(chunkSize);
const fd = await fs.open(filePath, 'r');
const { bytesRead } = await fd.read(buffer, 0, chunkSize, startOffset);
await fd.close();
// Trim buffer to actual bytes read
const chunk = buffer.subarray(0, bytesRead);
const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, { const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': `OAuth ${userAccessToken}`, Authorization: `OAuth ${userAccessToken}`,
'file_offset': startOffset.toString(), file_offset: startOffset.toString(),
'Content-Type': 'application/octet-stream', 'Content-Length': chunkSize.toString()
'Content-Length': bytesRead.toString()
}, },
body: chunk body: fileStream
}); });
const uploadData = await uploadResponse.json(); const uploadData = await uploadResponse.json();
@ -1415,7 +1398,7 @@ export class WhatsappDirectLine extends GBService {
h = uploadData.h; h = uploadData.h;
} }
if (!uploadResponse.ok) { if (!uploadResponse.ok) {
throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`); throw new Error(uploadData.error.message);
} }
startOffset = endOffset; startOffset = endOffset;
@ -1425,20 +1408,19 @@ export class WhatsappDirectLine extends GBService {
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, { const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Authorization': `OAuth ${userAccessToken}` Authorization: `OAuth ${userAccessToken}`
} }
}); });
const finalizeData = await finalizeResponse.json(); const finalizeData = await finalizeResponse.json();
if (!finalizeResponse.ok) { if (!finalizeResponse.ok) {
throw new Error(`Finalize failed: ${finalizeData.error?.message || 'Unknown error'}`); throw new Error(finalizeData.error.message);
} }
console.log('Upload completed successfully with file handle:', finalizeData.h); console.log('Upload completed successfully with file handle:', finalizeData.h);
return finalizeData.h; // Return the final handle from the response return h;
} catch (error) { } catch (error) {
console.error('Error during file upload:', error); console.error('Error during file upload:', error);
throw error; // Re-throw to allow caller to handle
} }
} }
@ -1664,7 +1646,7 @@ Last Edited: *${lastEditedDate}*`;
console.error('Error fetching WhatsApp template statistics:', error.message); console.error('Error fetching WhatsApp template statistics:', error.message);
return `Error fetching statistics: ${error.message}`; return `Error fetching statistics: ${error.message}`;
} }
} }
} }