From e1456880ac83d8292ee9cb2d199b7ea1c39b7caa Mon Sep 17 00:00:00 2001 From: Cleiton Carvalho Date: Thu, 1 Jun 2023 21:22:38 -0300 Subject: [PATCH] feat: Added listChats function --- src/api/layers/retriever.layer.ts | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/api/layers/retriever.layer.ts b/src/api/layers/retriever.layer.ts index 65faf974f..501e52a5a 100644 --- a/src/api/layers/retriever.layer.ts +++ b/src/api/layers/retriever.layer.ts @@ -27,6 +27,7 @@ import { Wid, } from '../model'; import { SenderLayer } from './sender.layer'; +import { ChatListOptions } from '@wppconnect/wa-js/dist/chat'; export class RetrieverLayer extends SenderLayer { constructor(public page: Page, session?: string, options?: CreateConfig) { @@ -124,6 +125,50 @@ export class RetrieverLayer extends SenderLayer { } } + /** + * Return list of chats + * * @example + * ```javascript + * // All chats + * const chats = await client.chatList(); + * + * // Some chats + * const chats = client.chatList({count: 20}); + * + * // 20 chats before specific chat + * const chats = client.chatList({count: 20, direction: 'before', id: '[number]@c.us'}); + * + * // Only users chats + * const chats = await client.chatList({onlyUsers: true}); + * + * // Only groups chats + * const chats = await client.chatList({onlyGroups: true}); + * + * // Only with label Text + * const chats = await client.chatList({withLabels: ['Test']}); + * + * // Only with label id + * const chats = await client.chatList({withLabels: ['1']}); + * + * // Only with label with one of text or id + * const chats = await client.chatList({withLabels: ['Alfa','5']}); + * ``` + * @category Chat + * @returns array of [Chat] + */ + public async listChats(options?: ChatListOptions): Promise { + return await evaluateAndReturn( + this.page, + async ({ options }) => { + const chats = await WPP.chat.list(options); + + const serialized = chats.map((c) => WAPI._serializeChatObj(c)); + return serialized; + }, + { options } + ); + } + /** * Checks if a number is a valid WA number * @category Contact