UNPKG

@wppconnect-team/wppconnect

Version:

WPPConnect is an open source project developed by the JavaScript community with the aim of exporting functions from WhatsApp Web to the node, which can be used to support the creation of any interaction, such as customer service, media sending, intelligen

465 lines (464 loc) 17.3 kB
"use strict"; /* * This file is part of WPPConnect. * * WPPConnect is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WPPConnect is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with WPPConnect. If not, see <https://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RetrieverLayer = void 0; const helpers_1 = require("../helpers"); const sender_layer_1 = require("./sender.layer"); class RetrieverLayer extends sender_layer_1.SenderLayer { page; constructor(page, session, options) { super(page, session, options); this.page = page; } /** * Returns a list of mute and non-mute users * @category Chat * @param type return type: all, toMute and noMute. * @returns obj */ async getListMutes(type) { return await (0, helpers_1.evaluateAndReturn)(this.page, (type) => WAPI.getListMute(type), type); } /** * Returns browser session token * @category Host * @returns obj [token] */ async getSessionTokenBrowser(removePath) { if (removePath === true) { await (0, helpers_1.evaluateAndReturn)(this.page, () => { window['pathSession'] = true; }); } if (await this.isMultiDevice()) { return await this.page .evaluate(() => { if (window.localStorage) { return { WABrowserId: window.localStorage.getItem('WABrowserId') || 'MultiDevice', WASecretBundle: 'MultiDevice', WAToken1: 'MultiDevice', WAToken2: 'MultiDevice', }; } return null; }) .catch(() => null); } return await this.page .evaluate(() => { if (window.localStorage) { return { WABrowserId: window.localStorage.getItem('WABrowserId'), WASecretBundle: window.localStorage.getItem('WASecretBundle'), WAToken1: window.localStorage.getItem('WAToken1'), WAToken2: window.localStorage.getItem('WAToken2'), }; } return null; }) .catch(() => null); } /** * Receive all blocked contacts * @category Blocklist * @returns array of [0,1,2,3....] */ async getBlockList() { return await (0, helpers_1.evaluateAndReturn)(this.page, () => WPP.blocklist.all().map((b) => b.toString())); } /** * Retrieves all chats * Deprecated in favor of {@link listChats} * * @category Chat * @returns array of [Chat] * @deprecated Deprecated in favor of listChats. */ async getAllChats(withNewMessageOnly = false) { this.logger.warn('Deprecated: This function [getAllChats] is deprecated in favor of the listChats function. Please update your code accordingly.'); if (withNewMessageOnly) { return (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllChatsWithNewMsg()); } else { return (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllChats()); } } /** * Return list of chats * * @example * ```javascript * // All chats * const chats = await client.listChats(); * * // Some chats * const chats = client.listChats({count: 20}); * * // 20 chats before specific chat * const chats = client.listChats({count: 20, direction: 'before', id: '[number]@c.us'}); * * // Only users chats * const chats = await client.listChats({onlyUsers: true}); * * // Only groups chats * const chats = await client.listChats({onlyGroups: true}); * * // Only with label Text * const chats = await client.listChats({withLabels: ['Test']}); * * // Only with label id * const chats = await client.listChats({withLabels: ['1']}); * * // Only with label with one of text or id * const chats = await client.listChats({withLabels: ['Alfa','5']}); * ``` * @category Chat * @returns array of [Chat] */ async listChats(options) { return await (0, helpers_1.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 * @param contactId, you need to include the @c.us at the end. * @returns contact details as promise */ async checkNumberStatus(contactId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (contactId) => WAPI.checkNumberStatus(contactId), contactId); } // TODO: implement usernameExists(username: string, key?: string) wrapping // WPP.contact.queryUsernameExists — added in wa-js v4.0.0, address in a future version. /** * Retrieves all chats with messages * Deprecated in favor of {@link listChats} * * @category Chat * @returns array of [Chat] * @deprecated Deprecated in favor of listChats. */ async getAllChatsWithMessages(withNewMessageOnly = false) { this.logger.warn('Deprecated: This function [getAllChatsWithMessages] is deprecated in favor of the listChats function. Please update your code accordingly.'); return (0, helpers_1.evaluateAndReturn)(this.page, (withNewMessageOnly) => WAPI.getAllChatsWithMessages(withNewMessageOnly), withNewMessageOnly); } /** * Retrieve all groups * Deprecated in favor of {@link listChats} * * @category Group * @returns array of groups * @deprecated Deprecated in favor of listChats. */ async getAllGroups(withNewMessagesOnly = false) { this.logger.warn('Deprecated: This function [getAllGroups] is deprecated in favor of the listChats function. Please update your code accordingly.'); return await (0, helpers_1.evaluateAndReturn)(this.page, async ({ withNewMessagesOnly }) => { const chats = await WPP.chat.list({ onlyGroups: true, onlyWithUnreadMessage: withNewMessagesOnly, }); const groups = await Promise.all(chats.map((c) => WPP.group.ensureGroup(c.id))); return groups.map((g) => WAPI._serializeChatObj(g)); }, { withNewMessagesOnly }); } /** * Retrieve all broadcast list * @category Group * @returns array of broadcast list */ async getAllBroadcastList() { const chats = await (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllChats()); return chats.filter((chat) => chat.isBroadcast && chat.id._serialized !== 'status@broadcast'); } /** * Retrieves contact detail object of given contact id * @category Contact * @param contactId * @returns contact details as promise */ async getContact(contactId) { return (0, helpers_1.evaluateAndReturn)(this.page, (contactId) => WAPI.getContact(contactId), contactId); } /** * Retrieves all contacts * @category Contact * @returns array of [Contact] */ async getAllContacts() { return await (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllContacts()); } /** * Retrieves chat object of given contact id * @category Chat * @param contactId * @returns chat details as promise */ async getChatById(contactId) { return (0, helpers_1.evaluateAndReturn)(this.page, (contactId) => WAPI.getChatById(contactId), contactId); } /** * Retrieves chat object of given contact id * @category Chat * @param contactId * @returns chat details as promise * @deprecated */ async getChat(contactId) { return this.getChatById(contactId); } /** * Retorna dados da imagem do contato * @category Contact * @param chatId Chat id * @returns url of the chat picture or undefined if there is no picture for the chat. */ async getProfilePicFromServer(chatId) { return (0, helpers_1.evaluateAndReturn)(this.page, (chatId) => WAPI._profilePicfunc(chatId), chatId); } /** * Load more messages in chat object from server. Use this in a while loop * Depreciado em favor de {@link getMessages} * * @deprecated Depreciado em favor de getMessages * @category Chat * @param contactId * @returns contact details as promise */ async loadEarlierMessages(contactId) { return (0, helpers_1.evaluateAndReturn)(this.page, (contactId) => WAPI.loadEarlierMessages(contactId), contactId); } /** * Retrieves status of given contact * @category Contact * @param contactId */ async getStatus(contactId) { return await (0, helpers_1.evaluateAndReturn)(this.page, async (contactId) => { const status = await WPP.contact.getStatus(contactId); return { id: contactId, status: status?.status || status, }; }, contactId); } /** * Checks if a number is a valid whatsapp number * * Deprecated in favor of checkNumberStatus * @deprecated Deprecated in favor of checkNumberStatus * @category Contact * @param contactId, you need to include the @c.us at the end. * @returns contact details as promise */ async getNumberProfile(contactId) { this.log('warn', 'The getNumberProfile function is deprecated, please use checkNumberStatus'); return (0, helpers_1.evaluateAndReturn)(this.page, (contactId) => WAPI.getNumberProfile(contactId), contactId); } /** * Retrieves all undread Messages * @category Chat * @param includeMe * @param includeNotifications * @param useUnreadCount * @returns any * @deprecated */ async getUnreadMessages(includeMe, includeNotifications, useUnreadCount) { return await (0, helpers_1.evaluateAndReturn)(this.page, ({ includeMe, includeNotifications, useUnreadCount }) => WAPI.getUnreadMessages(includeMe, includeNotifications, useUnreadCount), { includeMe, includeNotifications, useUnreadCount }); } /** * Retrieves all unread messages (where ack is -1) * @category Chat * @returns list of messages */ async getAllUnreadMessages() { return (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllUnreadMessages()); } /** * Retrieves all new messages (where isNewMsg is true) * @category Chat * @returns List of messages * @deprecated Use getAllUnreadMessages */ async getAllNewMessages() { return await (0, helpers_1.evaluateAndReturn)(this.page, () => WAPI.getAllNewMessages()); } /** * Retrieves all messages already loaded in a chat * For loading every message use loadAndGetAllMessagesInChat * Depreciado em favor de {@link getMessages} * * @deprecated Depreciado em favor de getMessages * * @category Chat * @param chatId, the chat to get the messages from * @param includeMe, include my own messages? boolean * @param includeNotifications * @returns any */ async getAllMessagesInChat(chatId, includeMe, includeNotifications) { return await (0, helpers_1.evaluateAndReturn)(this.page, ({ chatId, includeMe, includeNotifications }) => WAPI.getAllMessagesInChat(chatId, includeMe, includeNotifications), { chatId, includeMe, includeNotifications }); } /** * Loads and Retrieves all Messages in a chat * Depreciado em favor de {@link getMessages} * * @deprecated Depreciado em favor de getMessages * @category Chat * @param chatId, the chat to get the messages from * @param includeMe, include my own messages? boolean * @param includeNotifications * @returns any */ async loadAndGetAllMessagesInChat(chatId, includeMe = false, includeNotifications = false) { return await (0, helpers_1.evaluateAndReturn)(this.page, ({ chatId, includeMe, includeNotifications }) => WAPI.loadAndGetAllMessagesInChat(chatId, includeMe, includeNotifications), { chatId, includeMe, includeNotifications }); } /** * Checks if a CHAT contact is online. * @category Chat * @param chatId chat id: xxxxx@c.us */ async getChatIsOnline(chatId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (chatId) => WAPI.getChatIsOnline(chatId), chatId); } /** * Retrieves the last seen of a CHAT. * @category Chat * @param chatId chat id: xxxxx@c.us */ async getLastSeen(chatId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (chatId) => WAPI.getLastSeen(chatId), chatId); } /** * Get the platform message from message ID * * The platform can be: * android * iphone * web * unknown * @category Chat * @param chatId chat id: xxxxx@c.us */ async getPlatformFromMessage(msgId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (msgId) => WPP.chat.getPlatformFromMessage(msgId), msgId); } /** * Get the reactions of a message * * @category Chat */ async getReactions(msgId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (msgId) => WPP.chat.getReactions(msgId), msgId); } /** * Get the votes of a poll message * * @category Chat */ async getVotes(msgId) { return await (0, helpers_1.evaluateAndReturn)(this.page, (msgId) => WPP.chat.getVotes(msgId), msgId); } /** * Get the max number of participants for a group * * @category Group */ async getGroupSizeLimit() { return await (0, helpers_1.evaluateAndReturn)(this.page, () => WPP.group.getGroupSizeLimit()); } /** * Get info of your sended order * * @example * ```javascript * const orderInfo = await client.getOrder('<orderId>'); * ``` * @category Order * @return Your order */ async getOrder(msgId) { return (0, helpers_1.evaluateAndReturn)(this.page, (msgId) => WPP.order.get(msgId), msgId); } /** * Get all commons groups for the contact * * @example * ```javascript * const groups_ids = await client.getCommonGroups('[number]@c.us'); * ``` * * @category Group * @param groupId Group ID ('000000-000000@g.us') * @returns Promise */ async getCommonGroups(wid) { return await (0, helpers_1.evaluateAndReturn)(this.page, ({ wid }) => WPP.contact.getCommonGroups(wid), { wid }); } /** * Get LID/PhoneNumber mapping and Contact information * * @example * ```javascript * const info = await client.getPnLidEntry('[number]@c.us'); * const info = await client.getPnLidEntry('[number]@lid'); * ``` * * @category Contact * @param phoneOrLid Contact ID (phone number or LID) * @returns Promise with lid, phoneNumber and contact information */ async getPnLidEntry(phoneOrLid) { return await (0, helpers_1.evaluateAndReturn)(this.page, (phoneOrLid) => WPP.contact.getPnLidEntry(phoneOrLid), phoneOrLid); } /** * Get messages from IndexedDB 'model-storage' database starting from a specific rowId * * This function queries the IndexedDB database directly using the rowId index, * retrieving messages with rowId greater than the specified value. * It's useful for pagination or fetching messages in chronological order. * * @example * ```javascript * // Get 1000 messages after rowId 999960610 * const messages = await client.getMessagesFromRowId({ minRowId: 999960610 }); * * // Get 500 messages after rowId 999960610 * const messages = await client.getMessagesFromRowId({ * minRowId: 999960610, * limit: 500 * }); * * // Get all available messages after rowId (use with caution) * const messages = await client.getMessagesFromRowId({ * minRowId: 999960610, * limit: -1 * }); * ``` * @category Chat * @param options Options for fetching messages * @returns Promise that resolves to an array of message objects from IndexedDB */ async getMessagesFromRowId(input) { return await (0, helpers_1.evaluateAndReturn)(this.page, (options) => WPP.indexdb.getMessagesFromRowId(options), input); } } exports.RetrieverLayer = RetrieverLayer;