feat: add or remove labels (#1915)

* add or remove labels

* Strings use singlequote

* add event chat change labels

* Revert "add event chat change labels"
This commit is contained in:
tofers 2023-06-25 22:25:49 +03:00 • committed by GitHub
commit 8fc6e6098c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

View file

@ -210,6 +210,18 @@ client.on('message', async msg => {
msg.reply('I can only edit my own messages');
}
}
} else if (msg.body === '!updatelabels') {
const chat = await msg.getChat();
await chat.changeLabels([0, 1]);
} else if (msg.body === '!addlabels') {
const chat = await msg.getChat();
let labels = (await chat.getLabels()).map(l => l.id);
labels.push('0');
labels.push('1');
await chat.changeLabels(labels);
} else if (msg.body === '!removelabels') {
const chat = await msg.getChat();
await chat.changeLabels([]);
}
});

5
index.d.ts vendored
View file

@ -72,6 +72,9 @@ declare namespace WAWebJS {
/** Get all current Labels */
getLabels(): Promise<Label[]>
/** Change labels in chats */
addOrRemoveLabels(labelIds: Array<number|string>, chatIds: Array<string>): Promise<void>
/** Get Label instance by ID */
getLabelById(labelId: string): Promise<Label>
@ -1162,6 +1165,8 @@ declare namespace WAWebJS {
markUnread: () => Promise<void>
/** Returns array of all Labels assigned to this Chat */
getLabels: () => Promise<Label[]>
/** Add or remove labels to this Chat */
changeLabels: (labelIds: Array<string | number>) => Promise<void>
}
export interface MessageSearchOptions {

View file

@ -1332,6 +1332,35 @@ class Client extends EventEmitter {
return success;
}
/**
* Change labels in chats
* @param {Array<number|string>} labelIds
* @param {Array<string>} chatIds
* @returns {Promise<void>}
*/
async addOrRemoveLabels(labelIds, chatIds) {
return this.pupPage.evaluate(async (labelIds, chatIds) => {
if (['smba', 'smbi'].indexOf(window.Store.Conn.platform) === -1) {
throw '[LT01] Only Whatsapp business';
}
const labels = window.WWebJS.getLabels().filter(e => labelIds.find(l => l == e.id) !== undefined);
const chats = window.Store.Chat.filter(e => chatIds.includes(e.id._serialized));
let actions = labels.map(label => ({id: label.id, type: 'add'}));
chats.forEach(chat => {
(chat.labels || []).forEach(n => {
if (!actions.find(e => e.id == n)) {
actions.push({id: n, type: 'remove'});
}
});
});
return await window.Store.Label.addOrRemoveLabels(actions, chats);
}, labelIds, chatIds);
}
}
module.exports = Client;

View file

@ -261,6 +261,15 @@ class Chat extends Base {
async getLabels() {
return this.client.getChatLabels(this.id._serialized);
}
/**
* Add or remove labels to this Chat
* @param {Array<number|string>} labelIds
* @returns {Promise<void>}
*/
async changeLabels(labelIds) {
return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]);
}
}
module.exports = Chat;