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,21 +259,21 @@ 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}`;
const options = { const options = {
url: url, url: url,
method: 'GET' method: 'GET'
}; };
const res = await fetch(url, options); const res = await fetch(url, options);
const json = await res.json(); const json = await res.json();
return json['accountStatus'] === 'authenticated'; return json['accountStatus'] === 'authenticated';
} }
return true; return true;
} }
} }
@ -735,16 +735,16 @@ export class WhatsappDirectLine extends GBService {
} }
else { else {
const driver = createBot(whatsappServiceNumber, whatsappServiceKey); const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
const fileExtension = path.extname(url).toLowerCase(); const fileExtension = path.extname(url).toLowerCase();
if (['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.ods', '.csv'].includes(fileExtension)) { if (['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.ods', '.csv'].includes(fileExtension)) {
await driver.sendDocument(to, url, { caption: caption }); await driver.sendDocument(to, url, { caption: caption });
} else if (['.mp3', '.wav', '.ogg', '.aac', '.m4a'].includes(fileExtension)) { } else if (['.mp3', '.wav', '.ogg', '.aac', '.m4a'].includes(fileExtension)) {
await driver.sendAudio(to, url); await driver.sendAudio(to, url);
} else { } else {
await driver.sendImage(to, url, { caption: caption }); await driver.sendImage(to, url, { caption: caption });
} }
} }
break; break;
@ -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,52 +931,52 @@ 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`;
const messageData = { const messageData = {
messaging_product: 'whatsapp', messaging_product: 'whatsapp',
recipient_type: 'individual', recipient_type: 'individual',
to: to, to: to,
type: 'interactive', type: 'interactive',
interactive: { interactive: {
type: 'button', type: 'button',
body: { body: {
text: 'Please select an option:' text: 'Please select an option:'
},
action: {
buttons: buttons.map((button, index) => ({
type: 'reply',
reply: {
id: `button_${index + 1}`,
title: button
}
}))
}
}
};
const response = await fetch(sendMessageEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}, },
body: JSON.stringify(messageData) action: {
}); buttons: buttons.map((button, index) => ({
type: 'reply',
if (!response.ok) { reply: {
const errorData = await response.json(); id: `button_${index + 1}`,
throw new Error(`Failed to send button list: ${JSON.stringify(errorData)}`); title: button
}
}))
}
} }
};
const result = await response.json(); const response = await fetch(sendMessageEndpoint, {
GBLogEx.info(this.min, 'Button list sent successfully:' + JSON.stringify(result)); method: 'POST',
return result; 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 button list: ${JSON.stringify(errorData)}`);
} }
const result = await response.json();
GBLogEx.info(this.min, 'Button list sent successfully:' + JSON.stringify(result));
return result;
}
public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) { public async sendToDevice(to: any, msg: string, conversationId, isViewOnce = false) {
try { try {
@ -1181,19 +1173,19 @@ 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);
text = await GBConversationalService.getTextFromAudioBuffer( text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey, this.min.instance.speechKey,
this.min.instance.cloudLocation, this.min.instance.cloudLocation,
buf, buf,
user.locale user.locale
); );
} 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
} }
} }
@ -1519,44 +1501,44 @@ export class WhatsappDirectLine extends GBService {
} }
public async downloadAudio(req, min) { public async downloadAudio(req, min) {
// Extract the audio ID from the request body // Extract the audio ID from the request body
const audioId = req.body.entry[0].changes[0].value.messages[0].audio.id; const audioId = req.body.entry[0].changes[0].value.messages[0].audio.id;
// User access token from min.whatsappServiceKey // User access token from min.whatsappServiceKey
const userAccessToken = GBServer.globals.minBoot.instance.whatsappServiceKey; const userAccessToken = GBServer.globals.minBoot.instance.whatsappServiceKey;
// Meta WhatsApp Business API endpoint for downloading media // Meta WhatsApp Business API endpoint for downloading media
const metaApiUrl = `https://graph.facebook.com/v20.0/${audioId}`; const metaApiUrl = `https://graph.facebook.com/v20.0/${audioId}`;
// Fetch the media URL using the audio ID // Fetch the media URL using the audio ID
const mediaUrlResponse = await fetch(metaApiUrl, { const mediaUrlResponse = await fetch(metaApiUrl, {
headers: { headers: {
Authorization: `Bearer ${userAccessToken}`, Authorization: `Bearer ${userAccessToken}`,
}, },
}); });
if (!mediaUrlResponse.ok) { if (!mediaUrlResponse.ok) {
throw new Error(`Failed to fetch media URL: ${mediaUrlResponse.statusText}`); throw new Error(`Failed to fetch media URL: ${mediaUrlResponse.statusText}`);
} }
const mediaUrlData = await mediaUrlResponse.json(); const mediaUrlData = await mediaUrlResponse.json();
const mediaUrl = mediaUrlData.url; const mediaUrl = mediaUrlData.url;
if (!mediaUrl) { if (!mediaUrl) {
throw new Error('Media URL not found in the response'); throw new Error('Media URL not found in the response');
} }
// Download the audio file // Download the audio file
const res = await fetch(mediaUrl, { const res = await fetch(mediaUrl, {
headers: { headers: {
Authorization: `Bearer ${userAccessToken}`, Authorization: `Bearer ${userAccessToken}`,
}, },
}); });
if (!res.ok) { if (!res.ok) {
throw new Error(`Failed to download audio: ${res.statusText}`); throw new Error(`Failed to download audio: ${res.statusText}`);
} }
let buf: any = Buffer.from(await res.arrayBuffer()); let buf: any = Buffer.from(await res.arrayBuffer());
return buf; return buf;
} }
public async getLatestCampaignReport() { public async getLatestCampaignReport() {
@ -1564,92 +1546,92 @@ export class WhatsappDirectLine extends GBService {
const userAccessToken = this.whatsappServiceKey; const userAccessToken = this.whatsappServiceKey;
if (!(businessAccountId && userAccessToken)) { if (!(businessAccountId && userAccessToken)) {
return 'No statistics available for marketing templates.'; return 'No statistics available for marketing templates.';
} }
try { try {
// Step 1: Fetch templates with edit time ordering // Step 1: Fetch templates with edit time ordering
const statsResponse = await fetch( const statsResponse = await fetch(
`https://graph.facebook.com/v21.0/${businessAccountId}?` + `https://graph.facebook.com/v21.0/${businessAccountId}?` +
`fields=message_templates{id,name,category,language,status,created_time,last_edited_time}&` + `fields=message_templates{id,name,category,language,status,created_time,last_edited_time}&` +
`access_token=${userAccessToken}` `access_token=${userAccessToken}`
); );
let data = await statsResponse.json(); let data = await statsResponse.json();
if (!statsResponse.ok) { if (!statsResponse.ok) {
throw new Error(data.error?.message || 'Failed to fetch templates'); throw new Error(data.error?.message || 'Failed to fetch templates');
} }
console.log(GBUtil.toYAML(data)); console.log(GBUtil.toYAML(data));
data = data.message_templates?.data || []; data = data.message_templates?.data || [];
// Check if message_templates is an array // Check if message_templates is an array
if (!Array.isArray(data)) { if (!Array.isArray(data)) {
console.error('Expected message_templates to be an array, but got:', data.message_templates); console.error('Expected message_templates to be an array, but got:', data.message_templates);
return 'Invalid response format for message templates.'; return 'Invalid response format for message templates.';
} }
if (data.length === 0) { if (data.length === 0) {
throw new Error('No template statistics found'); throw new Error('No template statistics found');
} }
// Filter for marketing templates and get the latest edited one // Filter for marketing templates and get the latest edited one
const marketingTemplates = data const marketingTemplates = data
.filter(template => template.category?.toUpperCase() === 'MARKETING') .filter(template => template.category?.toUpperCase() === 'MARKETING')
.sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime()); .sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
if (marketingTemplates.length === 0) { if (marketingTemplates.length === 0) {
return 'No marketing templates found.'; return 'No marketing templates found.';
} }
const latestTemplate = marketingTemplates[0]; const latestTemplate = marketingTemplates[0];
const templateId = latestTemplate.id; const templateId = latestTemplate.id;
// Step 2: Fetch template analytics for the last 7 days // Step 2: Fetch template analytics for the last 7 days
const startTime = Math.floor(Date.now() / 1000) - 86400 * 7; // Last 7 days const startTime = Math.floor(Date.now() / 1000) - 86400 * 7; // Last 7 days
const endTime = Math.floor(Date.now() / 1000); const endTime = Math.floor(Date.now() / 1000);
const analyticsResponse = await fetch( const analyticsResponse = await fetch(
`https://graph.facebook.com/v21.0/${businessAccountId}?` + `https://graph.facebook.com/v21.0/${businessAccountId}?` +
`fields=template_analytics.start(${startTime}).end(${endTime}).granularity(DAILY).metric_types(sent,delivered,read,clicked).template_ids([${templateId}])&` + `fields=template_analytics.start(${startTime}).end(${endTime}).granularity(DAILY).metric_types(sent,delivered,read,clicked).template_ids([${templateId}])&` +
`access_token=${userAccessToken}` `access_token=${userAccessToken}`
); );
const analyticsData = await analyticsResponse.json(); const analyticsData = await analyticsResponse.json();
console.log(GBUtil.toYAML(analyticsData)); console.log(GBUtil.toYAML(analyticsData));
if (!analyticsResponse.ok) { if (!analyticsResponse.ok) {
throw new Error(analyticsData.error?.message || 'Failed to fetch analytics'); throw new Error(analyticsData.error?.message || 'Failed to fetch analytics');
} }
const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || []; const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || [];
if (dataPoints.length === 0) { if (dataPoints.length === 0) {
return 'No analytics data available for the specified template.'; return 'No analytics data available for the specified template.';
} }
// Aggregate the data points for the latest template and the last 7 days // Aggregate the data points for the latest template and the last 7 days
const aggregatedData = dataPoints.reduce((acc, dataPoint) => { const aggregatedData = dataPoints.reduce((acc, dataPoint) => {
acc.sent += dataPoint.sent || 0; acc.sent += dataPoint.sent || 0;
acc.delivered += dataPoint.delivered || 0; acc.delivered += dataPoint.delivered || 0;
acc.read += dataPoint.read || 0; acc.read += dataPoint.read || 0;
acc.clicked += (dataPoint.clicked?.reduce((sum, item) => sum + item.count, 0)) || 0; acc.clicked += (dataPoint.clicked?.reduce((sum, item) => sum + item.count, 0)) || 0;
return acc; return acc;
}, { sent: 0, delivered: 0, read: 0, clicked: 0 }); }, { sent: 0, delivered: 0, read: 0, clicked: 0 });
// Calculate read rate and click rate // Calculate read rate and click rate
const readRate = aggregatedData.delivered > 0 ? ((aggregatedData.read / aggregatedData.delivered) * 100).toFixed(2) : 0; const readRate = aggregatedData.delivered > 0 ? ((aggregatedData.read / aggregatedData.delivered) * 100).toFixed(2) : 0;
const clickRate = aggregatedData.delivered > 0 ? ((aggregatedData.clicked / aggregatedData.delivered) * 100).toFixed(2) : 0; const clickRate = aggregatedData.delivered > 0 ? ((aggregatedData.clicked / aggregatedData.delivered) * 100).toFixed(2) : 0;
// Format the date // Format the date
const lastEditedDate = latestTemplate.last_edited_time const lastEditedDate = latestTemplate.last_edited_time
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', { ? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
month: 'short', month: 'short',
day: '2-digit', day: '2-digit',
year: 'numeric' year: 'numeric'
}) })
: 'Not available'; : 'Not available';
return `Template Name: *${latestTemplate.name}* return `Template Name: *${latestTemplate.name}*
Category: *${latestTemplate.category?.toUpperCase()}* Category: *${latestTemplate.category?.toUpperCase()}*
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}* Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
Status: *${latestTemplate.status?.toUpperCase()}* Status: *${latestTemplate.status?.toUpperCase()}*
@ -1661,10 +1643,10 @@ Top Block Reason: *${latestTemplate.rejection_reason || ''}*
Last Edited: *${lastEditedDate}*`; Last Edited: *${lastEditedDate}*`;
} catch (error) { } catch (error) {
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}`;
} }
} }
} }