UNPKG

@warriorteam/redai-zalo-sdk

Version:

Comprehensive TypeScript/JavaScript SDK for Zalo APIs - Official Account v3.0, ZNS with Full Type Safety, Consultation Service, Broadcast Service, Group Messaging with List APIs, Social APIs, Enhanced Article Management, Promotion Service v3.0 with Multip

254 lines 9.44 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: { 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 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); } } /** * Lấy thông tin quota tổng quan cho tin nhắn (tổng số asset và số còn khả dụng) * Lưu ý: Zalo hiện cung cấp API chi tiết qua endpoint quota/message theo asset. * Hàm này tổng hợp đơn giản để trả về cấu trúc MessageQuota phục vụ SDK. */ async getMessageQuota(accessToken) { try { // Gọi endpoint quota/message không chỉ định product_type để lấy toàn bộ asset const request = { quota_owner: 'OA', }; const assets = await this.getDetailedQuotaMessage(accessToken, request); // Đếm tổng asset và số asset còn khả dụng const totalAssets = assets.length; const availableAssets = assets.filter((a) => a.status === 'available') .length; // Trả về dạng MessageQuota tối giản const quota = { // Sử dụng tổng số asset làm daily_quota (ước lượng theo asset) daily_quota: totalAssets, // Số còn lại là số asset đang ở trạng thái available remaining_quota: availableAssets, // Quota type tổng hợp vì có thể gồm nhiều loại quota_type: 'mixed', // Không có reset_time từ API asset -> trả 0 reset_time: 0, }; return quota; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to get message quota: ${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