-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
group.layer.ts
358 lines (330 loc) · 9.5 KB
/
group.layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import {
evaluateAndReturn,
base64MimeType,
fileToBase64,
downloadFileToBase64,
} from '../helpers';
import { Id } from '../model';
import { GroupProperty } from '../model/enum';
import { RetrieverLayer } from './retriever.layer';
export class GroupLayer extends RetrieverLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
super(page, session, options);
}
/**
* Removes the host device from the group
* @category Group
* @param groupId group id
*/
public async leaveGroup(groupId: string) {
return evaluateAndReturn(
this.page,
(groupId) => WPP.group.leave(groupId),
groupId
);
}
/**
* Retrieves group members as [Id] objects
* @category Group
* @param groupId group id
*/
public async getGroupMembersIds(groupId: string): Promise<Id[]> {
return evaluateAndReturn(
this.page,
(groupId: string) =>
Promise.resolve(WPP.group.getParticipants(groupId)).then(
(participants) => participants.map((p) => p.id as any)
),
groupId
);
}
/**
* Returns group members [Contact] objects
* @category Group
* @param groupId
*/
public async getGroupMembers(groupId: string) {
const membersIds = await this.getGroupMembersIds(groupId);
const actions = membersIds.map((memberId) => {
return this.getContact(memberId._serialized);
});
return Promise.all(actions);
}
/**
* Generates group-invite link
* @category Group
* @param chatId
* @returns Invitation link
*/
public async getGroupInviteLink(chatId: string) {
const code = await evaluateAndReturn(
this.page,
(chatId) => WPP.group.getInviteCode(chatId),
chatId
);
return `https://chat.whatsapp.com/${code}`;
}
/**
* Revokes group-invite link and generate new one.
* @category Group
* @param chatId
* @returns Invitation link
*/
public async revokeGroupInviteLink(chatId: string) {
const code = await evaluateAndReturn(
this.page,
(chatId) => WPP.group.revokeInviteCode(chatId),
chatId
);
return `https://chat.whatsapp.com/${code}`;
}
/**
* Generates group-invite link
* @category Group
* @param inviteCode
* @returns Invite code from group link. Example: CMJYfPFqRyE2GxrnkldYED
*/
public async getGroupInfoFromInviteLink(inviteCode: string) {
inviteCode = inviteCode.replace('chat.whatsapp.com/', '');
inviteCode = inviteCode.replace('invite/', '');
inviteCode = inviteCode.replace('https://', '');
inviteCode = inviteCode.replace('http://', '');
return await evaluateAndReturn(
this.page,
(inviteCode) => WPP.group.getGroupInfoFromInviteCode(inviteCode),
inviteCode
);
}
/**
* Creates a new chat group
* @category Group
* @param groupName Group name
* @param contacts Contacts that should be added.
*/
public async createGroup(groupName: string, contacts: string | string[]) {
return await evaluateAndReturn(
this.page,
({ groupName, contacts }) => WPP.group.create(groupName, contacts),
{ groupName, contacts }
);
}
/**
* Removes participant from group
* @category Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async removeParticipant(
groupId: string,
participantId: string | string[]
) {
await evaluateAndReturn(
this.page,
({ groupId, participantId }) =>
WPP.group.removeParticipants(groupId, participantId),
{ groupId, participantId }
);
return true;
}
/**
* Adds participant to Group
* @category Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async addParticipant(
groupId: string,
participantId: string | string[]
) {
return await evaluateAndReturn(
this.page,
({ groupId, participantId }) =>
WPP.group.addParticipants(groupId, participantId),
{ groupId, participantId }
);
}
/**
* Promotes participant as Admin in given group
* @category Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async promoteParticipant(
groupId: string,
participantId: string | string[]
) {
await evaluateAndReturn(
this.page,
({ groupId, participantId }) =>
WPP.group.promoteParticipants(groupId, participantId),
{ groupId, participantId }
);
return true;
}
/**
* Demotes admin privileges of participant
* @category Group
* @param groupId Chat id ('0000000000-00000000@g.us')
* @param participantId Participant id'000000000000@c.us'
*/
public async demoteParticipant(
groupId: string,
participantId: string | string[]
) {
return await evaluateAndReturn(
this.page,
({ groupId, participantId }) =>
WPP.group.demoteParticipants(groupId, participantId),
{ groupId, participantId }
);
return true;
}
/**
* Retrieves group admins
* @category Group
* @param chatId Group/Chat id ('0000000000-00000000@g.us')
*/
public async getGroupAdmins(chatId: string) {
const participants = await evaluateAndReturn(
this.page,
(chatId) =>
Promise.resolve(WPP.group.getParticipants(chatId)).then(
(participants) => participants.map((p) => p.toJSON())
),
chatId
);
return participants.filter((p) => p.isAdmin).map((p) => p.id);
}
/**
* Join a group with invite code
* @category Group
* @param inviteCode
*/
public async joinGroup(inviteCode: string) {
inviteCode = inviteCode.replace('chat.whatsapp.com/', '');
inviteCode = inviteCode.replace('invite/', '');
inviteCode = inviteCode.replace('https://', '');
inviteCode = inviteCode.replace('http://', '');
return await evaluateAndReturn(
this.page,
(inviteCode) => WPP.group.join(inviteCode),
inviteCode
);
}
/**
* Set group description (if allowed)
* @category Group
* @param groupId Group ID ('000000-000000@g.us')
* @param description New group description
* @returns empty object
*/
public async setGroupDescription(groupId: string, description: string) {
return await evaluateAndReturn(
this.page,
({ groupId, description }) =>
WPP.group.setDescription(groupId, description),
{ groupId, description }
);
}
/**
* Set group subject (if allowed)
* @category Group
* @param groupId Group ID ('000000-000000@g.us')
* @param title New group subject
* @returns empty object
*/
public async setGroupSubject(groupId: string, title: string) {
return await evaluateAndReturn(
this.page,
({ groupId, title }) => WPP.group.setSubject(groupId, title),
{ groupId, title }
);
}
/**
* Enable or disable group properties, see {@link GroupProperty for details}
* @category Group
* @param groupId Group ID ('000000-000000@g.us')
* @param property
* @param value true or false
* @returns empty object
*/
public async setGroupProperty(
groupId: string,
property: GroupProperty,
value: boolean
) {
return await evaluateAndReturn(
this.page,
({ groupId, property, value }) =>
WPP.group.setProperty(groupId, property, value),
{ groupId, property, value }
);
}
/**
* Set group subject (if allowed)
* @category Group
* @param groupId Group ID ('000000-000000@g.us')
* @param base64 Image in base64 ( data:image/jpeg;base64,..... )
* @returns empty object
*/
public async setGroupIcon(groupId: string, pathOrBase64: string) {
let base64: string = '';
if (pathOrBase64.startsWith('data:')) {
base64 = pathOrBase64;
} else {
let fileContent = await downloadFileToBase64(pathOrBase64, [
'image/gif',
'image/png',
'image/jpg',
'image/jpeg',
'image/webp',
]);
if (!fileContent) {
fileContent = await fileToBase64(pathOrBase64);
}
if (fileContent) {
base64 = fileContent;
}
}
if (!base64) {
const error = new Error('Empty or invalid file or base64');
Object.assign(error, {
code: 'empty_file',
});
throw error;
}
const mimeInfo = base64MimeType(base64);
if (!mimeInfo || !mimeInfo.includes('image')) {
const error = new Error(
'Not an image, allowed formats png, jpeg and webp'
);
Object.assign(error, {
code: 'invalid_image',
});
throw error;
}
return await evaluateAndReturn(
this.page,
({ groupId, base64 }) => WPP.group.setIcon(groupId, base64),
{ groupId, base64 }
);
}
}