refactor(SystemKeywords): comment out unused ID column logic for clarity
Some checks failed
GBCI / build (push) Has been cancelled
Some checks failed
GBCI / build (push) Has been cancelled
This commit is contained in:
parent
a33ed106f7
commit
5c3b24dadd
2 changed files with 241 additions and 197 deletions
|
|
@ -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}`;
|
||||||
|
|
@ -931,7 +931,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 +975,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 +1173,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 +1185,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');
|
||||||
|
|
@ -1541,7 +1541,7 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getLatestCampaignReport() {
|
public async getLatestCampaignReport() {
|
||||||
const businessAccountId = this.whatsappBusinessManagerId;
|
const businessAccountId = this.whatsappBusinessManagerId;
|
||||||
const userAccessToken = this.whatsappServiceKey;
|
const userAccessToken = this.whatsappServiceKey;
|
||||||
|
|
||||||
|
|
@ -1575,41 +1575,47 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
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 15 edited ones
|
||||||
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())
|
||||||
|
.slice(0, 15); // Get only the latest 15 templates
|
||||||
|
|
||||||
if (marketingTemplates.length === 0) {
|
if (marketingTemplates.length === 0) {
|
||||||
return 'No marketing templates found.';
|
return 'No marketing templates found.';
|
||||||
}
|
}
|
||||||
|
|
||||||
const latestTemplate = marketingTemplates[0];
|
// Step 2: Fetch analytics for all templates
|
||||||
const templateId = latestTemplate.id;
|
|
||||||
|
|
||||||
// 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 templateIds = marketingTemplates.map(template => template.id);
|
||||||
|
const templateResults = [];
|
||||||
|
|
||||||
|
// Fetch analytics for each template
|
||||||
|
for (const template of marketingTemplates) {
|
||||||
|
try {
|
||||||
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([${template.id}])&` +
|
||||||
`access_token=${userAccessToken}`
|
`access_token=${userAccessToken}`
|
||||||
);
|
);
|
||||||
|
|
||||||
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');
|
console.warn(`Failed to fetch analytics for template ${template.name}: ${analyticsData.error?.message}`);
|
||||||
|
// Add template with no analytics data
|
||||||
|
templateResults.push({
|
||||||
|
template,
|
||||||
|
analytics: { sent: 0, delivered: 0, read: 0, clicked: 0 }
|
||||||
|
});
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || [];
|
const dataPoints = analyticsData.template_analytics?.data[0]?.data_points || [];
|
||||||
if (dataPoints.length === 0) {
|
|
||||||
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 this template
|
||||||
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;
|
||||||
|
|
@ -1618,35 +1624,73 @@ private async sendButtonList(to: string, buttons: string[]) {
|
||||||
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
|
templateResults.push({
|
||||||
const readRate = aggregatedData.delivered > 0 ? ((aggregatedData.read / aggregatedData.delivered) * 100).toFixed(2) : 0;
|
template,
|
||||||
const clickRate = aggregatedData.delivered > 0 ? ((aggregatedData.clicked / aggregatedData.delivered) * 100).toFixed(2) : 0;
|
analytics: aggregatedData
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`Error fetching analytics for template ${template.name}:`, error.message);
|
||||||
|
// Add template with no analytics data
|
||||||
|
templateResults.push({
|
||||||
|
template,
|
||||||
|
analytics: { sent: 0, delivered: 0, read: 0, clicked: 0 }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the results
|
||||||
|
let report = `*📊 Latest 15 Marketing Templates Report (Last 7 Days)*\n\n`;
|
||||||
|
|
||||||
|
templateResults.forEach((result, index) => {
|
||||||
|
const { template, analytics } = result;
|
||||||
|
|
||||||
|
// Calculate rates
|
||||||
|
const readRate = analytics.delivered > 0 ? ((analytics.read / analytics.delivered) * 100).toFixed(2) : 0;
|
||||||
|
const clickRate = analytics.delivered > 0 ? ((analytics.clicked / analytics.delivered) * 100).toFixed(2) : 0;
|
||||||
|
|
||||||
// Format the date
|
// Format the date
|
||||||
const lastEditedDate = latestTemplate.last_edited_time
|
const lastEditedDate = template.last_edited_time
|
||||||
? new Date(latestTemplate.last_edited_time).toLocaleDateString('en-US', {
|
? new Date(template.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}*
|
report += `*${index + 1}. ${template.name}*
|
||||||
Category: *${latestTemplate.category?.toUpperCase()}*
|
Language: *${template.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
||||||
Language: *${latestTemplate.language?.replace('-', '_').toUpperCase() || 'pt_BR'}*
|
Status: *${template.status?.toUpperCase()}*
|
||||||
Status: *${latestTemplate.status?.toUpperCase()}*
|
📤 Sent: *${analytics.sent.toLocaleString()}*
|
||||||
Messages Sent: *${aggregatedData.sent.toLocaleString()}*
|
📬 Delivered: *${analytics.delivered.toLocaleString()}*
|
||||||
Messages Delivered: *${aggregatedData.delivered.toLocaleString()}*
|
👁️ Read Rate: *${readRate}% (${analytics.read.toLocaleString()})*
|
||||||
Message Read Rate: *${readRate}% (${aggregatedData.read.toLocaleString()})*
|
🔗 Click Rate: *${clickRate}% (${analytics.clicked.toLocaleString()})*
|
||||||
Message Click Rate: *${clickRate}% (${aggregatedData.clicked.toLocaleString()})*
|
📅 Last Edited: *${lastEditedDate}*
|
||||||
Top Block Reason: *${latestTemplate.rejection_reason || '––'}*
|
|
||||||
Last Edited: *${lastEditedDate}*`;
|
`;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add summary statistics
|
||||||
|
const totalSent = templateResults.reduce((sum, result) => sum + result.analytics.sent, 0);
|
||||||
|
const totalDelivered = templateResults.reduce((sum, result) => sum + result.analytics.delivered, 0);
|
||||||
|
const totalRead = templateResults.reduce((sum, result) => sum + result.analytics.read, 0);
|
||||||
|
const totalClicked = templateResults.reduce((sum, result) => sum + result.analytics.clicked, 0);
|
||||||
|
|
||||||
|
const overallReadRate = totalDelivered > 0 ? ((totalRead / totalDelivered) * 100).toFixed(2) : 0;
|
||||||
|
const overallClickRate = totalDelivered > 0 ? ((totalClicked / totalDelivered) * 100).toFixed(2) : 0;
|
||||||
|
|
||||||
|
report += `*📈 Overall Summary*
|
||||||
|
Total Messages Sent: *${totalSent.toLocaleString()}*
|
||||||
|
Total Messages Delivered: *${totalDelivered.toLocaleString()}*
|
||||||
|
Overall Read Rate: *${overallReadRate}%*
|
||||||
|
Overall Click Rate: *${overallClickRate}%*`;
|
||||||
|
|
||||||
|
return report;
|
||||||
|
|
||||||
} 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}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue