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:
parent
8e8d0c9b3e
commit
8fc6e6098c
4 changed files with 55 additions and 0 deletions
12
example.js
12
example.js
|
|
@ -210,6 +210,18 @@ client.on('message', async msg => {
|
||||||
msg.reply('I can only edit my own messages');
|
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
5
index.d.ts
vendored
|
|
@ -72,6 +72,9 @@ declare namespace WAWebJS {
|
||||||
/** Get all current Labels */
|
/** Get all current Labels */
|
||||||
getLabels(): Promise<Label[]>
|
getLabels(): Promise<Label[]>
|
||||||
|
|
||||||
|
/** Change labels in chats */
|
||||||
|
addOrRemoveLabels(labelIds: Array<number|string>, chatIds: Array<string>): Promise<void>
|
||||||
|
|
||||||
/** Get Label instance by ID */
|
/** Get Label instance by ID */
|
||||||
getLabelById(labelId: string): Promise<Label>
|
getLabelById(labelId: string): Promise<Label>
|
||||||
|
|
||||||
|
|
@ -1162,6 +1165,8 @@ declare namespace WAWebJS {
|
||||||
markUnread: () => Promise<void>
|
markUnread: () => Promise<void>
|
||||||
/** Returns array of all Labels assigned to this Chat */
|
/** Returns array of all Labels assigned to this Chat */
|
||||||
getLabels: () => Promise<Label[]>
|
getLabels: () => Promise<Label[]>
|
||||||
|
/** Add or remove labels to this Chat */
|
||||||
|
changeLabels: (labelIds: Array<string | number>) => Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MessageSearchOptions {
|
export interface MessageSearchOptions {
|
||||||
|
|
|
||||||
|
|
@ -1332,6 +1332,35 @@ class Client extends EventEmitter {
|
||||||
|
|
||||||
return success;
|
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;
|
module.exports = Client;
|
||||||
|
|
|
||||||
|
|
@ -261,6 +261,15 @@ class Chat extends Base {
|
||||||
async getLabels() {
|
async getLabels() {
|
||||||
return this.client.getChatLabels(this.id._serialized);
|
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;
|
module.exports = Chat;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue