redai-automation-web-sdk
Version:
TypeScript SDK for RedAI Automation Web API - Zalo Personal automation, messaging, advanced sticker search, and bulk operations. 100% compatible with automation-web backend. v1.8.1: Updated GroupInfo interface to match backend controller with complete gro
172 lines • 5.52 kB
JavaScript
;
/**
* Webhook management service
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebhookService = void 0;
const validation_utils_1 = require("../../utils/validation.utils");
/**
* Webhook management service class
*/
class WebhookService {
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Start webhook listener for a session
*/
async startWebhookListener(request) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(request.sessionId);
const response = await this.httpClient.post('/webhook/start', {
sessionId: request.sessionId,
options: request.options || {},
});
return response;
}
/**
* Stop webhook listener for a session
*/
async stopWebhookListener(request) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(request.sessionId);
const response = await this.httpClient.post('/webhook/stop', {
sessionId: request.sessionId,
});
return response;
}
/**
* Get webhook listener status for a session
*/
async getWebhookListenerStatus(sessionId) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(sessionId);
const response = await this.httpClient.get(`/webhook/status/${sessionId}`);
return response;
}
/**
* Get webhook statistics for a session
*/
async getWebhookStatistics(sessionId) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(sessionId);
const response = await this.httpClient.get(`/webhook/stats/${sessionId}`);
return response;
}
/**
* Get all active webhook listeners
*/
async getActiveWebhookListeners() {
const response = await this.httpClient.get('/webhook/listeners');
return response.data;
}
/**
* Restart webhook listener for a session
*/
async restartWebhookListener(sessionId, options) {
// Stop first
await this.stopWebhookListener({ sessionId });
// Wait a bit
await new Promise(resolve => setTimeout(resolve, 1000));
// Start again
return this.startWebhookListener({ sessionId, options });
}
/**
* Check if webhook listener is active
*/
async isWebhookListenerActive(sessionId) {
try {
const status = await this.getWebhookListenerStatus(sessionId);
return status.status === 'active';
}
catch (error) {
return false;
}
}
/**
* Get webhook health status
*/
async getWebhookHealth() {
const response = await this.httpClient.get('/webhook/health');
return {
status: response.data.status === 'ok' ? 'healthy' : 'unhealthy',
activeListeners: response.data.activeListeners,
totalEvents: response.data.totalEvents,
queueHealth: response.data.queueHealth,
};
}
/**
* Stop all webhook listeners
*/
async stopAllWebhookListeners() {
const response = await this.httpClient.delete('/webhook/listeners/all');
return response.data;
}
/**
* Validate session
* GET /webhook/validate/:sessionId
*/
async validateSession(sessionId) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(sessionId);
const response = await this.httpClient.get(`/webhook/validate/${sessionId}`);
return response;
}
/**
* Validate all sessions
* POST /webhook/validate-all
*/
async validateAllSessions() {
const response = await this.httpClient.post('/webhook/validate-all', {});
return response;
}
/**
* Cleanup expired sessions
* DELETE /webhook/cleanup-expired
*/
async cleanupExpiredSessions() {
const response = await this.httpClient.delete('/webhook/cleanup-expired');
return response;
}
/**
* Request old messages from a thread
* Pattern: Request-Response via WebSocket
*
* How it works:
* 1. Call this API to SEND REQUEST
* 2. Server processes and SENDS RESPONSE via 'old_messages' event
* 3. Results will be received via webhook event handler
*
* @param request - Request old messages parameters
* @returns Response with request confirmation
*
* @example
* ```typescript
* // Request latest messages for a user thread
* await webhookService.requestOldMessages({
* sessionId: 'your-session-id',
* threadType: 'user',
* lastMsgId: null
* });
*
* // Request older messages using last message ID
* await webhookService.requestOldMessages({
* sessionId: 'your-session-id',
* threadType: 'group',
* lastMsgId: 'msg-id-123'
* });
* ```
*/
async requestOldMessages(request) {
// Validate input
validation_utils_1.ValidationUtils.validateSessionId(request.sessionId);
const response = await this.httpClient.post('/webhook/request-old-messages', {
sessionId: request.sessionId,
threadType: request.threadType || 'user',
lastMsgId: request.lastMsgId ?? null,
});
return response;
}
}
exports.WebhookService = WebhookService;
//# sourceMappingURL=webhook.service.js.map