mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 04:29:15 +00:00
feat: WA Business Labels support (#407)
* Get all labels * Get chats by label * Get labels assigned to chats Co-authored-by: Pedro S. Lopez <pslamoros@hotmail.com> Co-authored-by: Pedro Lopez <pedroslopez@me.com>
This commit is contained in:
committed by
Pedro S. Lopez
parent
563f73d290
commit
274d24002b
26
index.d.ts
vendored
26
index.d.ts
vendored
@@ -50,6 +50,18 @@ declare namespace WAWebJS {
|
|||||||
/** Get all current contact instances */
|
/** Get all current contact instances */
|
||||||
getContacts(): Promise<Contact[]>
|
getContacts(): Promise<Contact[]>
|
||||||
|
|
||||||
|
/** Get all current Labels */
|
||||||
|
getLabels(): Promise<Label[]>
|
||||||
|
|
||||||
|
/** Get Label instance by ID */
|
||||||
|
getLabelById(labelId: string): Promise<Label>
|
||||||
|
|
||||||
|
/** Get all Labels assigned to a Chat */
|
||||||
|
getChatLabels(chatId: string): Promise<Label[]>
|
||||||
|
|
||||||
|
/** Get all Chats for a specific Label */
|
||||||
|
getChatsByLabelId(labelId: string): Promise<Chat[]>
|
||||||
|
|
||||||
/** Returns the contact ID's profile picture URL, if privacy settings allow it */
|
/** Returns the contact ID's profile picture URL, if privacy settings allow it */
|
||||||
getProfilePicUrl(contactId: string): Promise<string>
|
getProfilePicUrl(contactId: string): Promise<string>
|
||||||
|
|
||||||
@@ -520,6 +532,18 @@ declare namespace WAWebJS {
|
|||||||
longitude: string,
|
longitude: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Label {
|
||||||
|
/** Label name */
|
||||||
|
name: string,
|
||||||
|
/** Label ID */
|
||||||
|
id: string,
|
||||||
|
/** Color assigned to the label */
|
||||||
|
hexColor: string,
|
||||||
|
|
||||||
|
/** Get all chats that have been assigned this Label */
|
||||||
|
getChats: () => Promise<Chat[]>
|
||||||
|
}
|
||||||
|
|
||||||
/** Options for sending a message */
|
/** Options for sending a message */
|
||||||
export interface MessageSendOptions {
|
export interface MessageSendOptions {
|
||||||
/** Show links preview */
|
/** Show links preview */
|
||||||
@@ -731,6 +755,8 @@ declare namespace WAWebJS {
|
|||||||
unmute: () => Promise<void>,
|
unmute: () => Promise<void>,
|
||||||
/** Returns the Contact that corresponds to this Chat. */
|
/** Returns the Contact that corresponds to this Chat. */
|
||||||
getContact: () => Promise<Contact>,
|
getContact: () => Promise<Contact>,
|
||||||
|
/** Returns array of all Labels assigned to this Chat */
|
||||||
|
getLabels: () => Promise<Label[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MessageSearchOptions {
|
export interface MessageSearchOptions {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const { WhatsWebURL, DefaultOptions, Events, WAState } = require('./util/Constan
|
|||||||
const { ExposeStore, LoadUtils } = require('./util/Injected');
|
const { ExposeStore, LoadUtils } = require('./util/Injected');
|
||||||
const ChatFactory = require('./factories/ChatFactory');
|
const ChatFactory = require('./factories/ChatFactory');
|
||||||
const ContactFactory = require('./factories/ContactFactory');
|
const ContactFactory = require('./factories/ContactFactory');
|
||||||
const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification } = require('./structures');
|
const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification , Label } = require('./structures');
|
||||||
/**
|
/**
|
||||||
* Starting point for interacting with the WhatsApp Web API
|
* Starting point for interacting with the WhatsApp Web API
|
||||||
* @extends {EventEmitter}
|
* @extends {EventEmitter}
|
||||||
@@ -749,6 +749,63 @@ class Client extends EventEmitter {
|
|||||||
return { gid: createRes.gid, missingParticipants };
|
return { gid: createRes.gid, missingParticipants };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all current Labels
|
||||||
|
* @returns {Promise<Array<Label>>}
|
||||||
|
*/
|
||||||
|
async getLabels() {
|
||||||
|
const labels = await this.pupPage.evaluate(async () => {
|
||||||
|
return window.WWebJS.getLabels();
|
||||||
|
});
|
||||||
|
|
||||||
|
return labels.map(data => new Label(this , data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Label instance by ID
|
||||||
|
* @param {string} labelId
|
||||||
|
* @returns {Promise<Label>}
|
||||||
|
*/
|
||||||
|
async getLabelById(labelId) {
|
||||||
|
const label = await this.pupPage.evaluate(async (labelId) => {
|
||||||
|
return window.WWebJS.getLabel(labelId);
|
||||||
|
}, labelId);
|
||||||
|
|
||||||
|
return new Label(this, label);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all Labels assigned to a chat
|
||||||
|
* @param {string} chatId
|
||||||
|
* @returns {Promise<Array<Label>>}
|
||||||
|
*/
|
||||||
|
async getChatLabels(chatId){
|
||||||
|
const labels = await this.pupPage.evaluate(async (chatId) => {
|
||||||
|
return window.WWebJS.getChatLabels(chatId);
|
||||||
|
}, chatId);
|
||||||
|
|
||||||
|
return labels.map(data => new Label(this, data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all Chats for a specific Label
|
||||||
|
* @param {string} labelId
|
||||||
|
* @returns {Promise<Array<Chat>>}
|
||||||
|
*/
|
||||||
|
async getChatsByLabelId(labelId){
|
||||||
|
const chatIds = await this.pupPage.evaluate(async (labelId) => {
|
||||||
|
const label = window.Store.Label.get(labelId);
|
||||||
|
const labelItems = label.labelItemCollection.models;
|
||||||
|
return labelItems.reduce((result, item) => {
|
||||||
|
if(item.parentType === 'Chat'){
|
||||||
|
result.push(item.parentId);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},[]);
|
||||||
|
}, labelId);
|
||||||
|
|
||||||
|
return Promise.all(chatIds.map(id => this.getChatById(id)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Client;
|
module.exports = Client;
|
||||||
|
|||||||
@@ -229,6 +229,14 @@ class Chat extends Base {
|
|||||||
async getContact() {
|
async getContact() {
|
||||||
return await this.client.getContactById(this.id._serialized);
|
return await this.client.getContactById(this.id._serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns array of all Labels assigned to this Chat
|
||||||
|
* @returns {Promise<Array<Label>>}
|
||||||
|
*/
|
||||||
|
async getLabels() {
|
||||||
|
return this.client.getChatLabels(this.id._serialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Chat;
|
module.exports = Chat;
|
||||||
|
|||||||
50
src/structures/Label.js
Normal file
50
src/structures/Label.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Base = require('./Base');
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
const Chat = require('./Chat');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WhatsApp Business Label information
|
||||||
|
*/
|
||||||
|
class Label extends Base {
|
||||||
|
/**
|
||||||
|
* @param {Base} client
|
||||||
|
* @param {object} labelData
|
||||||
|
*/
|
||||||
|
constructor(client, labelData){
|
||||||
|
super(client);
|
||||||
|
|
||||||
|
if(labelData) this._patch(labelData);
|
||||||
|
}
|
||||||
|
|
||||||
|
_patch(labelData){
|
||||||
|
/**
|
||||||
|
* Label ID
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
this.id = labelData.id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Label name
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
this.name = labelData.name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Label hex color
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
this.hexColor = labelData.hexColor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get all chats that have been assigned this Label
|
||||||
|
* @returns {Promise<Array<Chat>>>}
|
||||||
|
*/
|
||||||
|
async getChats(){
|
||||||
|
return this.client.getChatsByLabelId(this.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Label;
|
||||||
@@ -10,5 +10,6 @@ module.exports = {
|
|||||||
MessageMedia: require('./MessageMedia'),
|
MessageMedia: require('./MessageMedia'),
|
||||||
PrivateChat: require('./PrivateChat'),
|
PrivateChat: require('./PrivateChat'),
|
||||||
PrivateContact: require('./PrivateContact'),
|
PrivateContact: require('./PrivateContact'),
|
||||||
GroupNotification: require('./GroupNotification')
|
GroupNotification: require('./GroupNotification'),
|
||||||
|
Label: require('./Label.js')
|
||||||
};
|
};
|
||||||
@@ -29,6 +29,7 @@ exports.ExposeStore = (moduleRaidStr) => {
|
|||||||
window.Store.WidFactory = window.mR.findModule('createWid')[0];
|
window.Store.WidFactory = window.mR.findModule('createWid')[0];
|
||||||
window.Store.BlockContact = window.mR.findModule('blockContact')[0];
|
window.Store.BlockContact = window.mR.findModule('blockContact')[0];
|
||||||
window.Store.GroupMetadata = window.mR.findModule((module) => module.default && module.default.handlePendingInvite)[0].default;
|
window.Store.GroupMetadata = window.mR.findModule((module) => module.default && module.default.handlePendingInvite)[0].default;
|
||||||
|
window.Store.Label = window.mR.findModule('LabelCollection')[0].default;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.LoadUtils = () => {
|
exports.LoadUtils = () => {
|
||||||
@@ -363,6 +364,27 @@ exports.LoadUtils = () => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
window.WWebJS.getLabelModel = label => {
|
||||||
|
let res = label.serialize();
|
||||||
|
res.hexColor = label.hexColor;
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
window.WWebJS.getLabels = () => {
|
||||||
|
const labels = window.Store.Label.models;
|
||||||
|
return labels.map(label => window.WWebJS.getLabelModel(label));
|
||||||
|
};
|
||||||
|
|
||||||
|
window.WWebJS.getLabel = (labelId) => {
|
||||||
|
const label = window.Store.Label.get(labelId);
|
||||||
|
return window.WWebJS.getLabelModel(label);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.WWebJS.getChatLabels = async (chatId) => {
|
||||||
|
const chat = await window.WWebJS.getChat(chatId);
|
||||||
|
return (chat.labels || []).map(id => window.WWebJS.getLabel(id));
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.MarkAllRead = () => {
|
exports.MarkAllRead = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user