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

@ -864,7 +864,11 @@ export class WhatsappDirectLine extends GBService {
{
type: 'HEADER',
format: 'TEXT',
text: 'General Bots'
text: 'General Bots',
example: {
header_text: [
"General Bots"
]
},
{
type: 'BODY',
@ -1347,70 +1351,91 @@ private async sendButtonList(to: string, buttons: string[]) {
}
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 {
// 1. Save file locally (cache)
const gbaiName = GBUtil.getGBAIPath(min.botId);
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;
}
if (!fileType) {
throw new Error('Unsupported file type');
}
// Helper: Register a public URL with Meta's /media endpoint
private async registerWithMeta(fileUrl, fileExtension) {
const fileType = this.getWhatsAppFileType(fileExtension); // e.g., 'image', 'document'
const response = await fetch(
`https://graph.facebook.com/v20.0/${this.whatsappFBAppId}/media`,
// Step 1: Start an upload session
const startResponse = await fetch(
`https://graph.facebook.com/v20.0/${appId}/uploads?file_name=${fileName}&file_length=${fileSize}&file_type=${fileType}&access_token=${userAccessToken}`,
{
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',
headers: {
'Authorization': `Bearer ${this.whatsappServiceKey}`,
'Content-Type': 'application/json'
'Authorization': `OAuth ${userAccessToken}`,
'file_offset': startOffset.toString(),
'Content-Type': 'application/octet-stream',
'Content-Length': bytesRead.toString()
},
body: JSON.stringify({
url: fileUrl,
type: fileType,
messaging_product: 'whatsapp'
})
body: chunk
});
const uploadData = await uploadResponse.json();
if (!h) {
h = uploadData.h;
}
);
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
if (!uploadResponse.ok) {
throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`);
}
// Helper: Map file extension to WhatsApp file type
private getWhatsAppFileType(ext) {
const types = {
'.jpg': 'image',
'.png': 'image',
'.pdf': 'document',
'.mp4': 'video',
// Add others as needed
};
return types[ext.toLowerCase()] || 'document';
startOffset = endOffset;
}
// 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) {