new(whatsapp.gblib): FB Analytics.

This commit is contained in:
Rodrigo Rodriguez 2025-02-16 18:28:09 -03:00
parent ffd7b52ade
commit 9978e35f33

View file

@ -1544,7 +1544,7 @@ private async sendButtonList(to: string, buttons: string[]) {
const userAccessToken = this.whatsappServiceKey; const userAccessToken = this.whatsappServiceKey;
if (!(businessAccountId && userAccessToken)) { if (!(businessAccountId && userAccessToken)) {
return 'No statistics available for General Bots WhatsApp templates.'; return 'No statistics available for marketing templates.';
} }
try { try {
@ -1552,10 +1552,7 @@ private async sendButtonList(to: string, buttons: string[]) {
const statsResponse = await fetch( const statsResponse = await fetch(
`https://graph.facebook.com/v20.0/${businessAccountId}/message_templates?` + `https://graph.facebook.com/v20.0/${businessAccountId}/message_templates?` +
`fields=id,name,category,language,status,created_time,last_edited_time,` + `fields=id,name,category,language,status,created_time,last_edited_time,` +
`message_sends_24h,message_sends_7d,message_sends_30d,` + `delivered_24h,read_24h,rejection_reason&` +
`delivered_24h,delivered_7d,delivered_30d,` +
`read_24h,read_7d,read_30d,content,components,` +
`quality_score,rejection_reason&` +
`ordering=[{last_edited_time: 'DESC'}]`, { `ordering=[{last_edited_time: 'DESC'}]`, {
headers: { headers: {
Authorization: `Bearer ${userAccessToken}` Authorization: `Bearer ${userAccessToken}`
@ -1571,36 +1568,41 @@ private async sendButtonList(to: string, buttons: string[]) {
throw new Error('No template statistics found'); throw new Error('No template statistics found');
} }
// Process all templates and format them // Filter for marketing templates and get the latest edited one
const templateReports = data.data.map(template => { const marketingTemplates = data.data
// Calculate delivery and read rates .filter(template => template.category?.toUpperCase() === 'MARKETING')
const delivered = template.delivered_24h || 0; .sort((a, b) => new Date(b.last_edited_time).getTime() - new Date(a.last_edited_time).getTime());
const read = template.read_24h || 0;
if (marketingTemplates.length === 0) {
return 'No marketing templates found.';
}
// Get the most recently edited marketing template
const latestTemplate = marketingTemplates[0];
// Calculate metrics
const delivered = latestTemplate.delivered_24h || 0;
const read = latestTemplate.read_24h || 0;
const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 0; const readRate = delivered > 0 ? ((read / delivered) * 100).toFixed(0) : 0;
const lastEditedDate = new Date(template.last_edited_time || template.created_time); // Format the date
const lastEditedDate = latestTemplate.last_edited_time
return ` ? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
Template Name: **${template.name}**
Category: **${template.category}**
Language: **${template.language}**
Status: **${template.status}**
Messages Delivered: **${delivered.toLocaleString()}**
Message Read Rate: **${readRate}% (${read.toLocaleString()})**
Top Block Reason: **${template.rejection_reason || ''}**
Last Edited: **${lastEditedDate.toLocaleDateString('en-US', {
month: 'short', month: 'short',
day: '2-digit', day: '2-digit',
year: 'numeric' year: 'numeric'
})}** })
${template.content ? `Content: ${template.content}` : ''} : 'Not available';
-------------------`;
});
// Join all template reports with double line breaks // Format the response exactly as requested
return `General Bots WhatsApp Analytics Report return `Template Name: *${latestTemplate.name}*
============================================ Category: *${latestTemplate.category?.toUpperCase()}*
${templateReports.join('\n\n')}`.trim(); Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
Status: *${latestTemplate.status?.toUpperCase()}*
Messages Delivered: *${delivered.toLocaleString()}*
Message Read Rate: *${readRate}% (${read.toLocaleString()})*
Top Block Reason: *${latestTemplate.rejection_reason || ''}*
Last Edited: *${lastEditedDate}*`.trim();
} catch (error) { } catch (error) {
console.error('Error fetching WhatsApp template statistics:', error.message); console.error('Error fetching WhatsApp template statistics:', error.message);
@ -1608,5 +1610,4 @@ ${templateReports.join('\n\n')}`.trim();
} }
} }
} }