UNPKG

@warriorteam/redai-zalo-sdk

Version:

Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account, ZNS, Consultation Service, Group Messaging, and Social APIs

241 lines 8.6 kB
"use strict"; /** * Official Account (OA) service for Zalo API */ Object.defineProperty(exports, "__esModule", { value: true }); exports.OAService = void 0; const oa_1 = require("../types/oa"); const common_1 = require("../types/common"); /** * Official Account service for managing OA information and settings */ class OAService { constructor(client) { this.client = client; // Zalo API endpoints - organized by functionality this.endpoints = { // OA information info: "https://openapi.zalo.me/v2.0/oa/getoa", // Quota endpoints quota: { overview: "https://openapi.zalo.me/v2.0/oa/quota", message: "https://openapi.zalo.me/v3.0/oa/quota/message", }, // Profile management profile: { update: "https://openapi.zalo.me/v2.0/oa/update", }, // Statistics statistics: { overview: "https://openapi.zalo.me/v2.0/oa/statistics", }, }; } /** * Get Official Account information */ async getOAInfo(accessToken) { try { const endpoint = this.endpoints.info; const result = await this.client.apiGet(endpoint, accessToken); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || 'Failed to get OA information', result.error, result); } if (!result.data) { throw new common_1.ZaloSDKError('No OA data received', -1); } return result.data; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get OA info: ${error.message}`, -1, error); } } /** * Get message quota information */ async getMessageQuota(accessToken) { try { const endpoint = this.endpoints.quota.overview; const result = await this.client.apiGet(endpoint, accessToken); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || 'Failed to get message quota', result.error, result); } if (!result.data) { throw new common_1.ZaloSDKError('No quota data received', -1); } return result.data; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get message quota: ${error.message}`, -1, error); } } /** * Get detailed quota message information */ async getDetailedQuotaMessage(accessToken, request) { try { const endpoint = this.endpoints.quota.message; const result = await this.client.apiPost(endpoint, accessToken, request); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || 'Failed to get detailed quota message', result.error, result); } return result.data || []; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get detailed quota message: ${error.message}`, -1, error); } } /** * Get quota for specific product type */ async getQuotaByProductType(accessToken, productType, quotaType) { const request = { quota_owner: 'OA', product_type: productType, quota_type: quotaType, }; return this.getDetailedQuotaMessage(accessToken, request); } /** * Get available GMF quota */ async getGMFQuota(accessToken, gmfType = oa_1.GMFProductType.GMF10) { return this.getQuotaByProductType(accessToken, gmfType); } /** * Get consultation message quota */ async getConsultationQuota(accessToken) { return this.getQuotaByProductType(accessToken, 'cs'); } /** * Get transaction message quota */ async getTransactionQuota(accessToken) { return this.getQuotaByProductType(accessToken, 'transaction'); } /** * Update OA profile information */ async updateOAProfile(accessToken, profileData) { try { // Note: This endpoint might not be available in all Zalo API versions // This is a placeholder implementation const endpoint = this.endpoints.profile.update; const result = await this.client.apiPost(endpoint, accessToken, profileData); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || 'Failed to update OA profile', result.error, result); } return true; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to update OA profile: ${error.message}`, -1, error); } } /** * Get OA statistics (if available) */ async getOAStatistics(accessToken) { try { // Note: This endpoint might not be available in all Zalo API versions // This is a placeholder implementation const endpoint = this.endpoints.statistics.overview; const result = await this.client.apiGet(endpoint, accessToken); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || 'Failed to get OA statistics', result.error, result); } return result.data || { total_followers: 0, new_followers_today: 0, messages_sent_today: 0, messages_received_today: 0, }; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get OA statistics: ${error.message}`, -1, error); } } /** * Check if OA has sufficient quota for message type */ async hasQuotaForMessageType(accessToken, messageType, requiredCount = 1) { try { let productType; if (messageType === 'promotion') { // Promotion messages might use different quota logic return true; // Placeholder } else { productType = messageType; } const quotaAssets = await this.getQuotaByProductType(accessToken, productType); // Check if there are available assets const availableAssets = quotaAssets.filter(asset => asset.status === 'available'); if (availableAssets.length === 0) { return false; } // For simplicity, assume each available asset can handle the required count return true; } catch (error) { // If we can't check quota, assume it's available to avoid blocking return true; } } /** * Get quota summary for all message types */ async getQuotaSummary(accessToken) { try { const [consultation, transaction, gmf10, gmf50, gmf100] = await Promise.all([ this.getConsultationQuota(accessToken), this.getTransactionQuota(accessToken), this.getGMFQuota(accessToken, oa_1.GMFProductType.GMF10), this.getGMFQuota(accessToken, oa_1.GMFProductType.GMF50), this.getGMFQuota(accessToken, oa_1.GMFProductType.GMF100), ]); return { consultation, transaction, gmf10, gmf50, gmf100, }; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get quota summary: ${error.message}`, -1, error); } } /** * Validate OA access token by getting OA info */ async validateOAToken(accessToken) { try { await this.getOAInfo(accessToken); return true; } catch (error) { return false; } } } exports.OAService = OAService; //# sourceMappingURL=oa.service.js.map