fix(WhatsappDirectLine): refactor server status check and improve large file upload process with chunked uploads
Some checks are pending
GBCI / build (push) Waiting to run

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-06-11 09:53:10 -03:00
parent aa606c0d90
commit 215bd1b699

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}`;
@ -864,7 +864,11 @@ export class WhatsappDirectLine extends GBService {
{ {
type: 'HEADER', type: 'HEADER',
format: 'TEXT', format: 'TEXT',
text: 'General Bots' text: 'General Bots',
example: {
header_text: [
"General Bots"
]
}, },
{ {
type: 'BODY', type: 'BODY',
@ -931,7 +935,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`;
@ -975,7 +979,7 @@ private async sendButtonList(to: string, buttons: string[]) {
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) {
@ -1173,7 +1177,7 @@ private async sendButtonList(to: string, buttons: string[]) {
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);
@ -1185,7 +1189,7 @@ private async sendButtonList(to: string, buttons: string[]) {
); );
}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');
@ -1346,72 +1350,93 @@ private async sendButtonList(to: string, buttons: string[]) {
} }
} }
public async uploadLargeFile(min, filePath) { public async uploadLargeFile(min, filePath) {
const CHUNK_SIZE = 4 * 1024 * 1024; // 4MB chunks
let uploadSessionId;
const fileSize = (await fs.stat(filePath)).size;
const fileName = filePath.split('/').pop();
const fileType = mime.lookup(filePath);
const appId = this.whatsappFBAppId;
const userAccessToken = this.whatsappServiceKey;
let h;
try { try {
// 1. Save file locally (cache) if (!fileType) {
const gbaiName = GBUtil.getGBAIPath(min.botId); throw new Error('Unsupported file type');
const localName = path.join(
'work',
gbaiName,
'cache',
`tmp${GBAdminService.getRndReadableIdentifier()}${path.extname(filePath)}`
);
await fs.copyFile(filePath, localName);
// 2. Generate a public URL to the cached file
const publicUrl = urlJoin(
GBServer.globals.publicAddress,
min.botId,
'cache',
path.basename(localName)
);
// 3. Register the public URL with Meta's API to get media_id
const mediaId = await this.registerWithMeta(publicUrl, path.extname(filePath));
return mediaId; // Return Meta's media_id (as original function expected)
} catch (error) {
console.error('Error in uploadLargeFile:', error);
throw error;
} }
}
// Helper: Register a public URL with Meta's /media endpoint // Step 1: Start an upload session
private async registerWithMeta(fileUrl, fileExtension) { const startResponse = await fetch(
const fileType = this.getWhatsAppFileType(fileExtension); // e.g., 'image', 'document' `https://graph.facebook.com/v20.0/${appId}/uploads?file_name=${fileName}&file_length=${fileSize}&file_type=${fileType}&access_token=${userAccessToken}`,
const response = await fetch(
`https://graph.facebook.com/v20.0/${this.whatsappFBAppId}/media`,
{ {
method: 'POST'
}
);
const startData = await startResponse.json();
if (!startResponse.ok) {
throw new Error(startData.error.message);
}
uploadSessionId = startData.id.split(':')[1];
// Step 2: Upload the file in chunks
let startOffset = 0;
while (startOffset < fileSize) {
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
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}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': `Bearer ${this.whatsappServiceKey}`, 'Authorization': `OAuth ${userAccessToken}`,
'Content-Type': 'application/json' 'file_offset': startOffset.toString(),
'Content-Type': 'application/octet-stream',
'Content-Length': bytesRead.toString()
}, },
body: JSON.stringify({ body: chunk
url: fileUrl, });
type: fileType,
messaging_product: 'whatsapp'
})
}
);
const data = await response.json();
if (!response.ok) {
throw new Error(`Failed to register media: ${data.error?.message}`);
}
return response['data'].id; // Meta's media_id
}
// Helper: Map file extension to WhatsApp file type const uploadData = await uploadResponse.json();
private getWhatsAppFileType(ext) { if (!h) {
const types = { h = uploadData.h;
'.jpg': 'image', }
'.png': 'image', if (!uploadResponse.ok) {
'.pdf': 'document', throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`);
'.mp4': 'video', }
// Add others as needed
}; startOffset = endOffset;
return types[ext.toLowerCase()] || 'document'; }
}
// Step 3: Get the file handle
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'GET',
headers: {
'Authorization': `OAuth ${userAccessToken}`
}
});
const finalizeData = await finalizeResponse.json();
if (!finalizeResponse.ok) {
throw new Error(`Finalize failed: ${finalizeData.error?.message || 'Unknown error'}`);
}
console.log('Upload completed successfully with file handle:', finalizeData.h);
return finalizeData.h; // Return the final handle from the response
} catch (error) {
console.error('Error during file upload:', error);
throw error; // Re-throw to allow caller to handle
}
}
public async downloadImage(mediaId, outputPath) { public async downloadImage(mediaId, outputPath) {
const userAccessToken = this.whatsappServiceKey; const userAccessToken = this.whatsappServiceKey;
@ -1635,7 +1660,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}`;
} }
} }
} }