redai-automation-web-sdk
Version:
TypeScript SDK for RedAI Automation Web API - Zalo Personal automation, messaging, and search. 100% compatible with automation-web backend. Now includes SearchModule for stickers and link parsing.
172 lines • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchService = void 0;
/**
* Zalo Personal Search Service Class
* 100% khớp với automation-web ZaloSearchController
*/
class SearchService {
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Tìm sticker theo từ khóa
* POST /zalo-personal/search/get-stickers
*
* @param request - Search stickers request
* @returns Promise<AutomationWebResponse<GetStickersResponse>>
*
* @example
* ```typescript
* const result = await searchService.getStickers({
* sessionId: "session-123",
* keyword: "hello"
* });
*
* if (result.success) {
* console.log(`Found ${result.data.result.length} stickers`);
* console.log('Sticker IDs:', result.data.result);
* }
* ```
*/
async getStickers(request) {
const response = await this.httpClient.post('/zalo-personal/search/get-stickers', {
sessionId: request.sessionId,
keyword: request.keyword,
});
return response;
}
/**
* Lấy chi tiết sticker theo IDs
* POST /zalo-personal/search/get-stickers-detail
*
* @param request - Get stickers detail request
* @returns Promise<AutomationWebResponse<GetStickersDetailResponse>>
*
* @example
* ```typescript
* const result = await searchService.getStickersDetail({
* sessionId: "session-123",
* stickerIds: [123, 456, 789]
* });
*
* if (result.success) {
* result.data.result.forEach(sticker => {
* console.log(`Sticker ${sticker.id}: ${sticker.name}`);
* console.log(`Image: ${sticker.imageUrl}`);
* });
* }
* ```
*/
async getStickersDetail(request) {
const response = await this.httpClient.post('/zalo-personal/search/get-stickers-detail', {
sessionId: request.sessionId,
stickerIds: request.stickerIds,
});
return response;
}
/**
* Parse thông tin metadata từ URL
* POST /zalo-personal/search/parse-link
*
* @param request - Parse link request
* @returns Promise<AutomationWebResponse<ParseLinkResponse>>
*
* @example
* ```typescript
* const result = await searchService.parseLink({
* sessionId: "session-123",
* url: "https://example.com/article"
* });
*
* if (result.success) {
* const metadata = result.data.result;
* console.log('Title:', metadata.title);
* console.log('Description:', metadata.description);
* console.log('Image:', metadata.image);
* console.log('Site:', metadata.siteName);
* }
* ```
*/
async parseLink(request) {
const response = await this.httpClient.post('/zalo-personal/search/parse-link', {
sessionId: request.sessionId,
url: request.url,
});
return response;
}
// ==================== CONVENIENCE METHODS ====================
/**
* Tìm stickers và lấy chi tiết trong một lần gọi
* Convenience method kết hợp getStickers + getStickersDetail
*
* @param sessionId - Session ID
* @param keyword - Search keyword
* @returns Promise với sticker details
*
* @example
* ```typescript
* const stickers = await searchService.searchStickersWithDetails(
* "session-123",
* "hello"
* );
*
* stickers.forEach(sticker => {
* console.log(`${sticker.name}: ${sticker.imageUrl}`);
* });
* ```
*/
async searchStickersWithDetails(sessionId, keyword) {
// Step 1: Search for stickers
const searchResult = await this.getStickers({ sessionId, keyword });
if (!searchResult.result?.result?.length) {
return [];
}
// Step 2: Get details for found stickers
const detailResult = await this.getStickersDetail({
sessionId,
stickerIds: searchResult.result.result,
});
if (!detailResult.result?.result) {
return [];
}
return detailResult.result.result;
}
/**
* Batch parse multiple links
* Parse nhiều links cùng lúc
*
* @param sessionId - Session ID
* @param urls - Array of URLs to parse
* @returns Promise với array of parsed metadata
*
* @example
* ```typescript
* const results = await searchService.parseMultipleLinks(
* "session-123",
* ["https://example1.com", "https://example2.com"]
* );
*
* results.forEach((metadata, index) => {
* if (metadata) {
* console.log(`Link ${index + 1}: ${metadata.title}`);
* }
* });
* ```
*/
async parseMultipleLinks(sessionId, urls) {
const promises = urls.map(async (url) => {
try {
const result = await this.parseLink({ sessionId, url });
return result.result?.result || null;
}
catch (error) {
console.warn(`Failed to parse link ${url}:`, error);
return null;
}
});
return Promise.all(promises);
}
}
exports.SearchService = SearchService;
//# sourceMappingURL=search.service.js.map