UNPKG

@warriorteam/redai-zalo-sdk

Version:

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

147 lines 6.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeneralMessageService = void 0; const common_1 = require("../types/common"); /** * Service xử lý các API tin nhắn tổng quát của Zalo Official Account * * Bao gồm các loại tin nhắn: * - Tin nhắn phản hồi (Response Message) * - Tin nhắn ẩn danh (Anonymous Message) * - Tin nhắn tự động (Auto Message) * - Tin nhắn hệ thống (System Message) * * ĐIỀU KIỆN GỬI TIN NHẮN TỔNG QUÁT: * * 1. TIN NHẮN PHẢN HỒI: * - Chỉ được gửi trong vòng 24 giờ kể từ khi người dùng gửi tin nhắn đến OA * - Không giới hạn số lượng tin nhắn phản hồi * - Có thể chứa bất kỳ nội dung nào (tư vấn, quảng cáo, thông tin) * * 2. TIN NHẮN ẨN DANH: * - Gửi tin nhắn mà không hiển thị thông tin OA * - Chỉ dành cho các trường hợp đặc biệt * - Cần được Zalo phê duyệt trước khi sử dụng * * 3. TIN NHẮN TỰ ĐỘNG: * - Tin nhắn được gửi tự động dựa trên trigger * - Bao gồm: tin chào mừng, tin cảm ơn, tin nhắc nhở * * 4. TIN NHẮN HỆ THỐNG: * - Tin nhắn thông báo từ hệ thống * - Bao gồm: thông báo bảo trì, cập nhật chính sách */ class GeneralMessageService { constructor(client) { this.client = client; // Zalo API endpoints - organized by functionality this.endpoints = { // Message endpoints message: { send: "https://openapi.zalo.me/v3.0/oa/message/cs", sendAnonymous: "https://openapi.zalo.me/v3.0/oa/message/anonymous", }, }; } /** * Gửi tin nhắn phản hồi hình ảnh * @param accessToken Access token của Official Account * @param recipient Thông tin người nhận * @param message Nội dung tin nhắn hình ảnh * @returns Thông tin tin nhắn đã gửi */ async sendResponseImageMessage(accessToken, recipient, message) { try { if (!message.attachment?.payload?.url) { throw new common_1.ZaloSDKError("URL hình ảnh không được để trống", -1); } const endpoint = this.endpoints.message.send; const request = { recipient, message, messaging_type: "response", }; const result = await this.client.apiPost(endpoint, accessToken, request); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || "Failed to send response image message", result.error, result); } if (!result.data) { throw new common_1.ZaloSDKError("No response data received", -1); } return result.data; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to send response image message: ${error.message}`, -1, error); } } /** * Gửi tin nhắn ẩn danh * @param accessToken Access token của Official Account * @param recipient Thông tin người nhận * @param message Nội dung tin nhắn ẩn danh * @returns Thông tin tin nhắn đã gửi */ async sendAnonymousMessage(accessToken, recipient, message) { try { if (!message.text || message.text.trim().length === 0) { throw new common_1.ZaloSDKError("Nội dung tin nhắn ẩn danh không được để trống", -1); } const endpoint = this.endpoints.message.sendAnonymous; const request = { recipient, message, }; const result = await this.client.apiPost(endpoint, accessToken, request); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || "Failed to send anonymous message", result.error, result); } if (!result.data) { throw new common_1.ZaloSDKError("No response data received", -1); } return result.data; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to send anonymous message: ${error.message}`, -1, error); } } /** * Gửi tin nhắn tổng quát * @param accessToken Access token của Official Account * @param recipient Thông tin người nhận * @param message Nội dung tin nhắn * @param messagingType Loại tin nhắn (response, update, message_tag) * @returns Thông tin tin nhắn đã gửi */ async sendMessage(accessToken, recipient, message, messagingType = "response") { try { const endpoint = this.endpoints.message.send; const request = { recipient, message, messaging_type: messagingType, }; const result = await this.client.apiPost(endpoint, accessToken, request); if (result.error !== 0) { throw new common_1.ZaloSDKError(result.message || "Failed to send message", result.error, result); } if (!result.data) { throw new common_1.ZaloSDKError("No response data received", -1); } return result.data; } catch (error) { if (error instanceof common_1.ZaloSDKError) { throw error; } throw new common_1.ZaloSDKError(`Failed to send message: ${error.message}`, -1, error); } } } exports.GeneralMessageService = GeneralMessageService; //# sourceMappingURL=general-message.service.js.map