Initial commit: Telegram Management System
Some checks failed
Deploy / deploy (push) Has been cancelled
Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
118
marketing-agent/services/shared/webhookClient.js
Normal file
118
marketing-agent/services/shared/webhookClient.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import axios from 'axios';
|
||||
import { logger } from './logger.js';
|
||||
|
||||
/**
|
||||
* Webhook client for triggering webhook events from other services
|
||||
*/
|
||||
export class WebhookClient {
|
||||
constructor(webhookServiceUrl = process.env.WEBHOOK_SERVICE_URL || 'http://localhost:3009') {
|
||||
this.webhookServiceUrl = webhookServiceUrl;
|
||||
this.axios = axios.create({
|
||||
baseURL: webhookServiceUrl,
|
||||
timeout: 5000
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a webhook event
|
||||
* @param {string} accountId - The account ID
|
||||
* @param {string} event - The event type (e.g., 'message.sent')
|
||||
* @param {object} payload - The event payload
|
||||
* @param {string} authToken - JWT token for authentication
|
||||
*/
|
||||
async triggerEvent(accountId, event, payload, authToken) {
|
||||
try {
|
||||
const response = await this.axios.post('/api/v1/webhook-events/trigger', {
|
||||
event,
|
||||
payload: {
|
||||
accountId,
|
||||
...payload,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
}, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authToken}`,
|
||||
'X-Account-ID': accountId
|
||||
}
|
||||
});
|
||||
|
||||
logger.debug('Webhook event triggered successfully', {
|
||||
accountId,
|
||||
event,
|
||||
response: response.data
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Don't throw error, just log it - webhook failures shouldn't break main flow
|
||||
logger.error('Failed to trigger webhook event', {
|
||||
accountId,
|
||||
event,
|
||||
error: error.message
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch trigger multiple events
|
||||
* @param {string} accountId - The account ID
|
||||
* @param {Array} events - Array of {event, payload} objects
|
||||
* @param {string} authToken - JWT token for authentication
|
||||
*/
|
||||
async triggerBatchEvents(accountId, events, authToken) {
|
||||
const results = [];
|
||||
|
||||
for (const { event, payload } of events) {
|
||||
const result = await this.triggerEvent(accountId, event, payload, authToken);
|
||||
results.push({ event, success: !!result });
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const webhookClient = new WebhookClient();
|
||||
|
||||
// Helper functions for common events
|
||||
export const webhookEvents = {
|
||||
messageSent: (accountId, messageData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'message.sent', messageData, authToken),
|
||||
|
||||
messageDelivered: (accountId, messageData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'message.delivered', messageData, authToken),
|
||||
|
||||
messageFailed: (accountId, messageData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'message.failed', messageData, authToken),
|
||||
|
||||
messageRead: (accountId, messageData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'message.read', messageData, authToken),
|
||||
|
||||
campaignStarted: (accountId, campaignData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'campaign.started', campaignData, authToken),
|
||||
|
||||
campaignCompleted: (accountId, campaignData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'campaign.completed', campaignData, authToken),
|
||||
|
||||
campaignFailed: (accountId, campaignData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'campaign.failed', campaignData, authToken),
|
||||
|
||||
contactCreated: (accountId, contactData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'contact.created', contactData, authToken),
|
||||
|
||||
contactUpdated: (accountId, contactData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'contact.updated', contactData, authToken),
|
||||
|
||||
contactDeleted: (accountId, contactData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'contact.deleted', contactData, authToken),
|
||||
|
||||
conversionTracked: (accountId, conversionData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'conversion.tracked', conversionData, authToken),
|
||||
|
||||
workflowTriggered: (accountId, workflowData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'workflow.triggered', workflowData, authToken),
|
||||
|
||||
workflowCompleted: (accountId, workflowData, authToken) =>
|
||||
webhookClient.triggerEvent(accountId, 'workflow.completed', workflowData, authToken)
|
||||
};
|
||||
Reference in New Issue
Block a user