Skip to content

Commit

Permalink
feat: create groups
Browse files Browse the repository at this point in the history
close #131
  • Loading branch information
pedroslopez committed Apr 3, 2020
1 parent d1e357d commit 0a7412c
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Client.js
Expand Up @@ -10,7 +10,7 @@ const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./u
const { ExposeStore, LoadUtils } = require('./util/Injected');
const ChatFactory = require('./factories/ChatFactory');
const ContactFactory = require('./factories/ContactFactory');
const { ClientInfo, Message, MessageMedia, Location, GroupNotification } = require('./structures');
const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification } = require('./structures');
/**
* Starting point for interacting with the WhatsApp Web API
* @extends {EventEmitter}
Expand Down Expand Up @@ -518,6 +518,43 @@ class Client extends EventEmitter {
}, id);
}

/**
* Create a new group
* @param {string} name group title
* @param {Array<Contact|string>} participants an array of Contacts or contact IDs to add to the group
* @returns {Object} createRes
* @returns {string} createRes.gid - ID for the group that was just created
* @returns {Object.<string,string>} createRes.missingParticipants - participants that were not added to the group. Keys represent the ID for participant that was not added and its value is a status code that represents the reason why participant could not be added. This is usually 403 if the user's privacy settings don't allow you to add them to groups.
*/
async createGroup(name, participants) {
if(!Array.isArray(participants) || participants.length == 0) {
throw 'You need to add at least one other participant to the group';
}

if(participants.every(c => c instanceof Contact)) {
participants = participants.map(c => c.id._serialized);
}

const createRes = await this.pupPage.evaluate(async (name, participantIds) => {
const res = await window.Store.Wap.createGroup(name, participantIds);
console.log(res);
if(!res.status === 200) {
throw 'An error occurred while creating the group!';
}

return res;
}, name, participants);

const missingParticipants = createRes.participants.reduce(((missing, c) => {
const id = Object.keys(c)[0];
const statusCode = c[id].code;
if(statusCode != 200) return Object.assign(missing, {[id]: statusCode});
return missing;
}), {});

return { gid: createRes.gid, missingParticipants};
}

}

module.exports = Client;

0 comments on commit 0a7412c

Please sign in to comment.