Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Community #2531

Draft
wants to merge 75 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
76b7595
leave method is now compatible with community
Aug 24, 2023
b900024
added setNonAdminSubGroupCreation
Aug 24, 2023
2c47406
docs updated
Aug 24, 2023
ae2bf17
added usage example
Aug 24, 2023
a4482b4
leave method fixed
Aug 27, 2023
085c773
small update to improve the performance of findImpl fix
Aug 23, 2023
2ac0ac4
window.compareWwebVersions fixed
Aug 23, 2023
bf6b9b5
added defaultSubgroupId to groupMetadata
Sep 23, 2023
e802746
ChatFactory extended to create a Community
Sep 23, 2023
725904f
added getSubgroups
Sep 23, 2023
c5e20c8
Merge branch 'main' into community
Sep 23, 2023
8afbeae
added isCommunity property
Sep 23, 2023
2ad217d
usage example fixed
Sep 23, 2023
1f8f834
added createCommunity method
Sep 24, 2023
1c18fe0
error to be returned as number
Sep 24, 2023
90f5a75
default error message was missing
Sep 25, 2023
06681f3
separate logic of linking subgroups
Sep 25, 2023
96b12f8
jsdocs fixed
Sep 25, 2023
6177438
added linkSubgroups method
Sep 25, 2023
1842db0
added unlinkSubgroups method
Sep 27, 2023
d6381aa
fixed getting community
Sep 27, 2023
07a4b29
added deactivateCommunity method
Sep 27, 2023
5c75a10
added removeParticipants method for the community
Sep 30, 2023
262a30c
fixed Community instance creation
Oct 2, 2023
7e00434
fixed methods
Oct 2, 2023
50d33ab
added getParticipants method
Oct 5, 2023
3337194
added promote/demoteParticipants methods
Oct 6, 2023
c3f45af
update the group_admin_changed event
Oct 9, 2023
f5618c8
Merge branch 'main' into community
Oct 10, 2023
784ae69
Merge branch 'main' into community
Oct 11, 2023
9c93cba
general fix
Oct 16, 2023
6fcb661
updated usage example
Oct 16, 2023
9883718
Merge branch 'main' into community
alechkos Oct 18, 2023
c37a639
Merge branch 'main' into community
alechkos Oct 19, 2023
6f20d36
Merge branch 'main' into community
alechkos Oct 19, 2023
ced38cd
Merge branch 'main' into community
alechkos Oct 21, 2023
9fe0f7e
Merge branch 'main' into community
alechkos Oct 22, 2023
cb98169
docs fixed
alechkos Oct 26, 2023
2ed2dda
window.injectToFunction clarifications
alechkos Nov 13, 2023
1d2da36
createCommunity: minor fixes
alechkos Nov 13, 2023
b5e845b
fixed group_join event
alechkos Nov 2, 2023
e246e94
createGroup: added createdAtTs prop to the resulting object
alechkos Nov 2, 2023
324afbf
createCommunity: added createdAtTs prop to the resulting object
alechkos Nov 2, 2023
fdd42b3
exports updated
alechkos Nov 3, 2023
ea6b506
Merge branch 'main' into community
alechkos Nov 24, 2023
df3d143
Merge branch 'main' into community
alechkos Nov 26, 2023
cbcfe3a
feat: added property to create community result
alechkos Nov 30, 2023
77e29f3
feat: added getJoinedSubgroups and getUnjoinedSubgroups
alechkos Nov 30, 2023
bd42e1f
feat: added joinSubgroup method
alechkos Nov 30, 2023
650e859
refactor: usage example updated
alechkos Nov 30, 2023
3226a52
Merge branch 'main' into community
alechkos Dec 1, 2023
a887648
fix: typo
alechkos Dec 1, 2023
5138bae
Merge branch 'main' into community
alechkos Dec 1, 2023
586cbc7
Merge branch 'main' into community
alechkos Dec 5, 2023
c40d1a1
Merge branch 'main' into community
alechkos Dec 7, 2023
790de1a
Merge branch 'main' into community
alechkos Dec 12, 2023
e787a14
Merge branch 'main' into community
alechkos Dec 12, 2023
3e14fbd
Merge branch 'main' into community
alechkos Dec 16, 2023
09486b5
Merge branch 'main' into community
alechkos Dec 17, 2023
d160e09
Merge branch 'main' into community
alechkos Jan 2, 2024
e30cd8b
Merge branch 'main' into community
alechkos Jan 11, 2024
4ea59ba
Merge branch 'main' into community
alechkos Jan 11, 2024
c8ac15b
Merge branch 'main' into community
alechkos Jan 14, 2024
852a7a2
Merge branch 'main' into community
alechkos Jan 18, 2024
38d151d
Merge branch 'main' into community
alechkos Mar 1, 2024
a66a4cc
Merge branch 'main' into community
alechkos Apr 3, 2024
2469853
Merge branch 'main' into community
alechkos Apr 22, 2024
eb8e12c
style: fix broken link in readme file
alechkos Apr 22, 2024
9b68769
Merge branch 'main' into community
alechkos Apr 27, 2024
906771b
Merge branch 'main' into community
alechkos Apr 28, 2024
8b59a54
Merge branch 'main' into community
alechkos Apr 30, 2024
c1b3eed
Merge branch 'main' into community
alechkos Apr 30, 2024
76b031c
Merge branch 'main' into community
alechkos May 1, 2024
ba28101
Merge branch 'main' into community
alechkos Jun 6, 2024
07c508e
Merge branch 'main' into community
alechkos Jun 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 152 additions & 28 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@ client.on('message', async msg => {
} else {
msg.reply('This command can only be used in a group!');
}
} else if (msg.body === '!memberAddMode') {
const community = await client.getChatById('communityId@g.us');
if (community.isCommunity) {
/** Allows all community members to add groups to the community: */
await community.setNonAdminSubGroupCreation(true);
/** Allows only community admins to add groups to the community: */
await community.setNonAdminSubGroupCreation(false);
}
} else if (msg.body === '!leave') {
// Leave the group
// Leave the group/community announcement group
let chat = await msg.getChat();
if (chat.isGroup) {
chat.leave();
if (chat.isGroup || chat.isCommunity) {
await chat.leave();
} else {
msg.reply('This command can only be used in a group!');
msg.reply('This command can only be used in a group or a community!');
}
} else if (msg.body.startsWith('!join ')) {
const inviteCode = msg.body.split(' ')[1];
Expand All @@ -96,43 +104,34 @@ client.on('message', async msg => {
}
} else if (msg.body.startsWith('!addmembers')) {
const group = await msg.getChat();
const result = await group.addParticipants(['number1@c.us', 'number2@c.us', 'number3@c.us']);
const result = await group.addParticipants(['XXXXXXXXXX@c.us', 'YYYYYYYYYY@c.us']);
/**
* The example of the {@link result} output:
*
* {
* 'number1@c.us': {
* 'XXXXXXXXXX@c.us': {
* code: 200,
* message: 'The participant was added successfully',
* isInviteV4Sent: false
* },
* 'number2@c.us': {
* code: 403,
* message: 'The participant can be added by sending private invitation only',
* isInviteV4Sent: true
* },
* 'number3@c.us': {
* code: 404,
* message: 'The phone number is not registered on WhatsApp',
* isInviteV4Sent: false
* }
* ...
* }
*
* For more usage examples:
* @see https://github.com/pedroslopez/whatsapp-web.js/pull/2344#usage-example1
*/
console.log(result);
} else if (msg.body === '!creategroup') {
const partitipantsToAdd = ['number1@c.us', 'number2@c.us', 'number3@c.us'];
const partitipantsToAdd = ['AAAAAAAAAA@c.us', 'BBBBBBBBBB@c.us'];
const result = await client.createGroup('Group Title', partitipantsToAdd);
/**
* The example of the {@link result} output:
* {
* title: 'Group Title',
* gid: {
* server: 'g.us',
* user: '1111111111',
* _serialized: '1111111111@g.us'
* user: 'XXXXXXXXXX',
* _serialized: 'XXXXXXXXXX@g.us'
* },
* participants: {
* 'botNumber@c.us': {
Expand All @@ -141,25 +140,21 @@ client.on('message', async msg => {
* isGroupCreator: true,
* isInviteV4Sent: false
* },
* 'number1@c.us': {
* 'AAAAAAAAAA@c.us': {
* statusCode: 200,
* message: 'The participant was added successfully',
* isGroupCreator: false,
* isInviteV4Sent: false
* },
* 'number2@c.us': {
* 'BBBBBBBBBB@c.us': {
* statusCode: 403,
* message: 'The participant can be added by sending private invitation only',
* isGroupCreator: false,
* isInviteV4Sent: true
* },
* 'number3@c.us': {
* statusCode: 404,
* message: 'The phone number is not registered on WhatsApp',
* isGroupCreator: false,
* isInviteV4Sent: false
* }
* }
* ...
* },
* createdAtTs: timestamp
* }
*
* For more usage examples:
Expand All @@ -180,6 +175,135 @@ client.on('message', async msg => {
} else {
msg.reply('This command can only be used in a group!');
}
} else if (msg.body === '!createcommunity') {
let createdCommunity;
createdCommunity = await client.createCommunity('CommunityName');
/**
* The example of the {@link createdCommunity}:
* {
* title: 'CommunityName',
* cid: {
* server: 'g.us',
* user: 'XXXXXXXXXX',
* _serialized: 'XXXXXXXXXX@g.us'
* },
* defaultSubgroup: {
* server: 'g.us',
* user: 'WWWWWWWWW',
* _serialized: 'WWWWWWWWW@g.us'
* },
* createdAtTs: timestamp
* }
*/
console.log(createdCommunity);

// You can also provide optional parametes:
createdCommunity = await client.createCommunity('CommunityName', {
description: 'Description',
subGroupIds: ['YYYYYYYYYY@g.us', 'ZZZZZZZZZZ@g.us'], // group IDs to link to the community
membershipApprovalMode: true, // false by default
allowNonAdminSubGroupCreation: true // false by default
});
} else if (msg.body === '!linksubgroups') {
const community = await client.getChatById('XXXXXXXXXX@g.us');
/**
* The example output of the method execution:
* {
* linkedGroupIds: [ 'ZZZZZZZZZZ@g.us' ],
* failedGroups: [
* {
* groupId: 'YYYYYYYYYY@g.us',
* code: 409,
* message: 'SubGroupConflictError'
* }
* ]
* }
*/
console.log(await community.linkSubgroups(['YYYYYYYYYY@g.us', 'ZZZZZZZZZZ@g.us']));
} else if (msg.body === '!unlinksubgroups') {
const community = await client.getChatById('communityId@g.us');
/**
* The example output of the unlinking result is as the linking one
* but also you can provide an optional parameter 'removeOrphanMembers':
*/
console.log(
await community.unlinkSubgroups(
['YYYYYYYYYY@g.us', 'ZZZZZZZZZZ@g.us'],
{ removeOrphanMembers: true } // false by default
)
);
} else if (msg.body.startsWith('!joinsubgroup')) {
const args = msg.body.split(' ');
let result;

result = await client.joinSubgroup(args[1], args[2]);
// OR
const community = await client.getChatById(args[1]);
result = await community.joinSubgroup(args[2]);

/**
* Example of the {@link result} object:
*
* {
* gid: {
* server: 'g.us',
* user: 'XXXXXXXXXX',
* _serialized: 'XXXXXXXXXX@g.us'
* },
* code: 200,
* message: 'The membership request was sent or you joined the subgroup successfully'
* }
*/
console.log(result);
} else if (msg.body === '!getsubgroups') {
const community = await client.getChatById('XXXXXXXXXX@g.us');
if (community.isCommunity) {
const subGroups = await community.getSubgroups();
/**
* The example of {@link subGroups}:
* [
* {
* server: 'g.us',
* user: 'ZZZZZZZZZZ',
* _serialized: 'ZZZZZZZZZZ@g.us'
* },
* ...
* ]
*/
console.log(subGroups);
}
} else if (msg.body === '!getjoinedsubgroups') {
const community = await client.getChatById('XXXXXXXXXX@g.us');
if (community.isCommunity) {
const joinedSubgroups = await community.getJoinedSubgroups();
console.log(joinedSubgroups);
}
} else if (msg.body === '!communitymembers') {
const community = await client.getChatById('XXXXXXXXXX@g.us');
if (community.isCommunity) {
const members = await community.getParticipants();
/**
* The example of {@link members}:
* [
* {
* id: {
* server: 'c.us',
* user: 'AAAAAAAAAA',
* _serialized: 'AAAAAAAAAA@c.us'
* },
* isAdmin: false,
* isSuperAdmin: false
* },
* ...
* ]
*/
console.log(members);
}
} else if (msg.body === '!deactivatecommunity') {
await client.deactivateCommunity('XXXXXXXXXX@g.us');
// OR
const community = await client.getChatById('XXXXXXXXXX@g.us');
await community.deactivate();
} else if (msg.body === '!chats') {
const chats = await client.getChats();
client.sendMessage(msg.from, `The bot has ${chats.length} chats open.`);
Expand Down
Loading
Loading