new(whatsapp.gblib): FB Analytics.

This commit is contained in:
Rodrigo Rodriguez 2025-02-16 20:53:04 -03:00
parent 592c25492d
commit bb3a6a7a84

View file

@ -1555,25 +1555,20 @@ private async sendButtonList(to: string, buttons: string[]) {
`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}`
); );
const data = await statsResponse.json(); const 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));
// Check if data.data is an array (templates are under "data" not "message_templates")
// Check if message_templates is an array if (!Array.isArray(data.data)) {
if (!Array.isArray(data.message_templates)) { console.error('Expected data to be an array, but got:', data.data);
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.message_templates.length === 0) { // Filter templates in the "data" array
throw new Error('No template statistics found'); const marketingTemplates = data.data
}
// Filter for marketing templates and get the latest edited one
const marketingTemplates = data.message_templates
.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());
@ -1581,6 +1576,7 @@ private async sendButtonList(to: string, buttons: string[]) {
return 'No marketing templates found.'; return 'No marketing templates found.';
} }
console.log(GBUtil.toYAML(marketingTemplates));
const latestTemplate = marketingTemplates[0]; const latestTemplate = marketingTemplates[0];
const templateId = latestTemplate.id; const templateId = latestTemplate.id;
@ -1595,11 +1591,11 @@ private async sendButtonList(to: string, buttons: string[]) {
); );
const analyticsData = await analyticsResponse.json(); const analyticsData = await analyticsResponse.json();
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');
} }
console.log(GBUtil.toYAML(analyticsData));
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.';