@badzz88/baileys 8.2.0 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,541 @@
1
+ const WAProto = require('../../WAProto').proto;
2
+ const Utils_1 = require('../Utils');
3
+ const crypto = require('crypto');
4
+
5
+ class imup {
6
+ constructor(utils, waUploadToServer, relayMessageFn, authState) {
7
+ this.utils = utils;
8
+ this.relayMessage = relayMessageFn
9
+ this.waUploadToServer = waUploadToServer;
10
+ this.authState = authState;
11
+ }
12
+
13
+ detectType(content) {
14
+ if (content.requestPaymentMessage) return 'PAYMENT';
15
+ if (content.productMessage) return 'PRODUCT';
16
+ if (content.interactiveMessage) return 'INTERACTIVE';
17
+ if (content.albumMessage) return 'ALBUM';
18
+ if (content.eventMessage) return 'EVENT';
19
+ if (content.pollResultMessage) return 'POLL_RESULT';
20
+ if (content.orderMessage) return 'ORDER';
21
+ if (content.groupStatus) return 'GROUP_STATUS';
22
+ if (content.groupLabel) return 'GROUP_LABEL';
23
+ return null;
24
+ }
25
+
26
+ async handlePayment(content, quoted) {
27
+ const data = content.requestPaymentMessage;
28
+ let notes = {};
29
+
30
+ if (data.sticker?.stickerMessage) {
31
+ notes = {
32
+ stickerMessage: {
33
+ ...data.sticker.stickerMessage,
34
+ contextInfo: {
35
+ stanzaId: quoted?.key?.id,
36
+ participant: quoted?.key?.participant || content.sender,
37
+ quotedMessage: quoted?.message
38
+ }
39
+ }
40
+ };
41
+ } else if (data.note) {
42
+ notes = {
43
+ extendedTextMessage: {
44
+ text: data.note,
45
+ contextInfo: {
46
+ stanzaId: quoted?.key?.id,
47
+ participant: quoted?.key?.participant || content.sender,
48
+ quotedMessage: quoted?.message
49
+ }
50
+ }
51
+ };
52
+ }
53
+
54
+ return {
55
+ requestPaymentMessage: WAProto.Message.RequestPaymentMessage.fromObject({
56
+ expiryTimestamp: data.expiry || 0,
57
+ amount1000: data.amount || 0,
58
+ currencyCodeIso4217: data.currency || "IDR",
59
+ requestFrom: data.from || "0@s.whatsapp.net",
60
+ noteMessage: notes,
61
+ background: data.background ?? {
62
+ id: "DEFAULT",
63
+ placeholderArgb: 0xFFF0F0F0
64
+ }
65
+ })
66
+ };
67
+ }
68
+
69
+ async handleProduct(content, jid, quoted) {
70
+ const {
71
+ title,
72
+ description,
73
+ thumbnail,
74
+ productId,
75
+ retailerId,
76
+ url,
77
+ body = "",
78
+ footer = "",
79
+ buttons = [],
80
+ priceAmount1000 = null,
81
+ currencyCode = "IDR"
82
+ } = content.productMessage;
83
+
84
+ let productImage;
85
+
86
+ if (Buffer.isBuffer(thumbnail)) {
87
+ const { imageMessage } = await this.utils.generateWAMessageContent(
88
+ { image: thumbnail },
89
+ { upload: this.waUploadToServer }
90
+ );
91
+ productImage = imageMessage;
92
+ } else if (typeof thumbnail === 'object' && thumbnail.url) {
93
+ const { imageMessage } = await this.utils.generateWAMessageContent(
94
+ { image: { url: thumbnail.url }},
95
+ { upload: this.waUploadToServer }
96
+ );
97
+ productImage = imageMessage;
98
+ }
99
+
100
+ return {
101
+ viewOnceMessage: {
102
+ message: {
103
+ interactiveMessage: {
104
+ body: { text: body },
105
+ footer: { text: footer },
106
+ header: {
107
+ title,
108
+ hasMediaAttachment: true,
109
+ productMessage: {
110
+ product: {
111
+ productImage,
112
+ productId,
113
+ title,
114
+ description,
115
+ currencyCode,
116
+ priceAmount1000,
117
+ retailerId,
118
+ url,
119
+ productImageCount: 1
120
+ },
121
+ businessOwnerJid: "0@s.whatsapp.net"
122
+ }
123
+ },
124
+ nativeFlowMessage: { buttons }
125
+ }
126
+ }
127
+ }
128
+ };
129
+ }
130
+
131
+ async handleInteractive(content, jid, quoted) {
132
+ const {
133
+ title,
134
+ footer,
135
+ thumbnail,
136
+ image,
137
+ video,
138
+ document,
139
+ mimetype,
140
+ fileName,
141
+ jpegThumbnail,
142
+ contextInfo,
143
+ externalAdReply,
144
+ buttons = [],
145
+ nativeFlowMessage
146
+ } = content.interactiveMessage;
147
+
148
+ let media = null;
149
+ let mediaType = null;
150
+
151
+ if (thumbnail) {
152
+ media = await this.utils.prepareWAMessageMedia(
153
+ { image: { url: thumbnail } },
154
+ { upload: this.waUploadToServer }
155
+ );
156
+ mediaType = 'image';
157
+ } else if (image) {
158
+ if (typeof image === 'object' && image.url) {
159
+ media = await this.utils.prepareWAMessageMedia(
160
+ { image: { url: image.url } },
161
+ { upload: this.waUploadToServer }
162
+ );
163
+ } else {
164
+ media = await this.utils.prepareWAMessageMedia(
165
+ { image: image },
166
+ { upload: this.waUploadToServer }
167
+ );
168
+ }
169
+ mediaType = 'image';
170
+ } else if (video) {
171
+ if (typeof video === 'object' && video.url) {
172
+ media = await this.utils.prepareWAMessageMedia(
173
+ { video: { url: video.url } },
174
+ { upload: this.waUploadToServer }
175
+ );
176
+ } else {
177
+ media = await this.utils.prepareWAMessageMedia(
178
+ { video: video },
179
+ { upload: this.waUploadToServer }
180
+ );
181
+ }
182
+ mediaType = 'video';
183
+ } else if (document) {
184
+ let documentPayload = { document: document };
185
+
186
+ if (jpegThumbnail) {
187
+ if (typeof jpegThumbnail === 'object' && jpegThumbnail.url) {
188
+ documentPayload.jpegThumbnail = { url: jpegThumbnail.url };
189
+ } else {
190
+ documentPayload.jpegThumbnail = jpegThumbnail;
191
+ }
192
+ }
193
+
194
+ media = await this.utils.prepareWAMessageMedia(
195
+ documentPayload,
196
+ { upload: this.waUploadToServer }
197
+ );
198
+
199
+ if (fileName) {
200
+ media.documentMessage.fileName = fileName;
201
+ }
202
+ if (mimetype) {
203
+ media.documentMessage.mimetype = mimetype;
204
+ }
205
+ mediaType = 'document';
206
+ }
207
+
208
+ let interactiveMessage = {
209
+ body: { text: title || "" },
210
+ footer: { text: footer || "" }
211
+ };
212
+
213
+ if (buttons && buttons.length > 0) {
214
+ interactiveMessage.nativeFlowMessage = {
215
+ buttons: buttons
216
+ };
217
+
218
+ if (nativeFlowMessage) {
219
+ interactiveMessage.nativeFlowMessage = {
220
+ ...interactiveMessage.nativeFlowMessage,
221
+ ...nativeFlowMessage
222
+ };
223
+ }
224
+ } else if (nativeFlowMessage) {
225
+ interactiveMessage.nativeFlowMessage = nativeFlowMessage;
226
+ }
227
+
228
+ if (media) {
229
+ interactiveMessage.header = {
230
+ title: "",
231
+ hasMediaAttachment: true,
232
+ ...media
233
+ };
234
+ } else {
235
+ interactiveMessage.header = {
236
+ title: "",
237
+ hasMediaAttachment: false
238
+ };
239
+ }
240
+
241
+ let finalContextInfo = {};
242
+
243
+ if (contextInfo) {
244
+ finalContextInfo = {
245
+ mentionedJid: contextInfo.mentionedJid || [],
246
+ forwardingScore: contextInfo.forwardingScore || 0,
247
+ isForwarded: contextInfo.isForwarded || false,
248
+ ...contextInfo
249
+ };
250
+ }
251
+
252
+ if (externalAdReply) {
253
+ finalContextInfo.externalAdReply = {
254
+ title: externalAdReply.title || "",
255
+ body: externalAdReply.body || "",
256
+ mediaType: externalAdReply.mediaType || 1,
257
+ thumbnailUrl: externalAdReply.thumbnailUrl || "",
258
+ mediaUrl: externalAdReply.mediaUrl || "",
259
+ sourceUrl: externalAdReply.sourceUrl || "",
260
+ showAdAttribution: externalAdReply.showAdAttribution || false,
261
+ renderLargerThumbnail: externalAdReply.renderLargerThumbnail || false,
262
+ ...externalAdReply
263
+ };
264
+ }
265
+
266
+ if (Object.keys(finalContextInfo).length > 0) {
267
+ interactiveMessage.contextInfo = finalContextInfo;
268
+ }
269
+
270
+ return {
271
+ interactiveMessage: interactiveMessage
272
+ };
273
+ }
274
+
275
+ async handleAlbum(content, jid, quoted) {
276
+ const array = content.albumMessage;
277
+ const album = await this.utils.generateWAMessageFromContent(jid, {
278
+ messageContextInfo: {
279
+ messageSecret: crypto.randomBytes(32),
280
+ },
281
+ albumMessage: {
282
+ expectedImageCount: array.filter((a) => a.hasOwnProperty("image")).length,
283
+ expectedVideoCount: array.filter((a) => a.hasOwnProperty("video")).length,
284
+ },
285
+ }, {
286
+ userJid: this.utils.generateMessageID().split('@')[0] + '@s.whatsapp.net',
287
+ quoted,
288
+ upload: this.waUploadToServer
289
+ });
290
+
291
+ await this.relayMessage(jid, album.message, {
292
+ messageId: album.key.id,
293
+ });
294
+
295
+ for (let content of array) {
296
+ const img = await this.utils.generateWAMessage(jid, content, {
297
+ upload: this.waUploadToServer,
298
+ });
299
+
300
+ img.message.messageContextInfo = {
301
+ messageSecret: crypto.randomBytes(32),
302
+ messageAssociation: {
303
+ associationType: 1,
304
+ parentMessageKey: album.key,
305
+ },
306
+ participant: "0@s.whatsapp.net",
307
+ remoteJid: "status@broadcast",
308
+ forwardingScore: 99999,
309
+ isForwarded: true,
310
+ mentionedJid: [jid],
311
+ starred: true,
312
+ labels: ["Y", "Important"],
313
+ isHighlighted: true,
314
+ businessMessageForwardInfo: {
315
+ businessOwnerJid: jid,
316
+ },
317
+ dataSharingContext: {
318
+ showMmDisclosure: true,
319
+ },
320
+ };
321
+
322
+ img.message.forwardedNewsletterMessageInfo = {
323
+ newsletterJid: "120363400362472743@newsletter",
324
+ serverMessageId: 1,
325
+ newsletterName: `WhatsApp`,
326
+ contentType: 1,
327
+ timestamp: new Date().toISOString(),
328
+ senderName: "badzz-lyoraa",
329
+ contentType: "UPDATE_CARD",
330
+ priority: "high",
331
+ status: "sent",
332
+ };
333
+
334
+ img.message.disappearingMode = {
335
+ initiator: 3,
336
+ trigger: 4,
337
+ initiatorDeviceJid: jid,
338
+ initiatedByExternalService: true,
339
+ initiatedByUserDevice: true,
340
+ initiatedBySystem: true,
341
+ initiatedByServer: true,
342
+ initiatedByAdmin: true,
343
+ initiatedByUser: true,
344
+ initiatedByApp: true,
345
+ initiatedByBot: true,
346
+ initiatedByMe: true,
347
+ };
348
+
349
+ await this.relayMessage(jid, img.message, {
350
+ messageId: img.key.id,
351
+ quoted: {
352
+ key: {
353
+ remoteJid: album.key.remoteJid,
354
+ id: album.key.id,
355
+ fromMe: true,
356
+ participant: this.utils.generateMessageID().split('@')[0] + '@s.whatsapp.net',
357
+ },
358
+ message: album.message,
359
+ },
360
+ });
361
+ }
362
+ return album;
363
+ }
364
+
365
+ async handleEvent(content, jid, quoted) {
366
+ const eventData = content.eventMessage;
367
+
368
+ const msg = await this.utils.generateWAMessageFromContent(jid, {
369
+ viewOnceMessage: {
370
+ message: {
371
+ messageContextInfo: {
372
+ deviceListMetadata: {},
373
+ deviceListMetadataVersion: 2,
374
+ messageSecret: crypto.randomBytes(32),
375
+ supportPayload: JSON.stringify({
376
+ version: 2,
377
+ is_ai_message: true,
378
+ should_show_system_message: true,
379
+ ticket_id: crypto.randomBytes(16).toString('hex')
380
+ })
381
+ },
382
+ eventMessage: {
383
+ contextInfo: {
384
+ mentionedJid: [jid],
385
+ participant: jid,
386
+ remoteJid: "status@broadcast",
387
+ forwardedNewsletterMessageInfo: {
388
+ newsletterName: "𝗔𝗟𝗟-𝗜𝗡𝗙𝗢𝗥𝗠𝗔𝗦𝗜-𝗕𝗔𝗗𝗭𝗭-𝗡𝗘-✓",
389
+ newsletterJid: "120363400362472743@newsletter",
390
+ serverMessageId: 1
391
+ }
392
+ },
393
+ isCanceled: eventData.isCanceled || false,
394
+ name: eventData.name,
395
+ description: eventData.description,
396
+ location: eventData.location || {
397
+ degreesLatitude: 0,
398
+ degreesLongitude: 0,
399
+ name: "Location"
400
+ },
401
+ joinLink: eventData.joinLink || '',
402
+ startTime: typeof eventData.startTime === 'string' ? parseInt(eventData.startTime) : eventData.startTime || Date.now(),
403
+ endTime: typeof eventData.endTime === 'string' ? parseInt(eventData.endTime) : eventData.endTime || Date.now() + 3600000,
404
+ extraGuestsAllowed: eventData.extraGuestsAllowed !== false
405
+ }
406
+ }
407
+ }
408
+ }, { quoted });
409
+
410
+ await this.relayMessage(jid, msg.message, {
411
+ messageId: msg.key.id
412
+ });
413
+ return msg;
414
+ }
415
+
416
+ async handlePollResult(content, jid, quoted) {
417
+ const pollData = content.pollResultMessage;
418
+ const msg = await this.utils.generateWAMessageFromContent(jid, {
419
+ pollResultSnapshotMessage: {
420
+ name: pollData.name,
421
+ pollVotes: pollData.pollVotes.map(vote => ({
422
+ optionName: vote.optionName,
423
+ optionVoteCount: typeof vote.optionVoteCount === 'number'
424
+ ? vote.optionVoteCount.toString()
425
+ : vote.optionVoteCount
426
+ })),
427
+ contextInfo: {
428
+ isForwarded: true,
429
+ forwardingScore: 1,
430
+ forwardedNewsletterMessageInfo: {
431
+ newsletterName: pollData.newsletter.newsletterName || "120363400362472743@newsletter",
432
+ newsletterJid: pollData.newsletter.newsletterJid || "Newsletter",
433
+ serverMessageId: 1000,
434
+ contentType: "UPDATE"
435
+ }
436
+ }
437
+ }
438
+ }, {
439
+ userJid: this.utils.generateMessageID().split('@')[0] + '@s.whatsapp.net',
440
+ quoted
441
+ });
442
+
443
+ await this.relayMessage(jid, msg.message, {
444
+ messageId: msg.key.id
445
+ });
446
+
447
+ return msg;
448
+ }
449
+
450
+ async handleOrderMessage(content, jid, quoted) {
451
+ const orderData = content.orderMessage;
452
+
453
+ const Haha = await this.utils.generateWAMessageFromContent(jid, {
454
+ orderMessage: {
455
+ orderId: "B4DZZN3_0122",
456
+ thumbnail: orderData.thumbnail || null,
457
+ itemCount: orderData.itemCount || 0,
458
+ status: "ACCEPTED",
459
+ surface: "CATALOG",
460
+ message: orderData.message,
461
+ orderTitle: orderData.orderTitle,
462
+ sellerJid: "0@whatsapp.net",
463
+ token: "B4DZZN_0122_TOKEN",
464
+ totalAmount1000: orderData.totalAmount1000 || 0,
465
+ totalCurrencyCode: orderData.totalCurrencyCode || "IDR",
466
+ messageVersion: 2
467
+ }
468
+ }, { quoted:quoted });
469
+
470
+ await this.relayMessage(jid, Haha.message, {});
471
+ return Haha;
472
+ }
473
+
474
+ async handleGroupStory(content, jid, quoted) {
475
+ const storyData = content.groupStatus;
476
+ let messageContent;
477
+
478
+ if (storyData.message) {
479
+ messageContent = storyData;
480
+ } else {
481
+ if (typeof this.utils?.generateWAMessageContent === "function") {
482
+ messageContent = await this.utils.generateWAMessageContent(storyData, {
483
+ upload: this.waUploadToServer
484
+ });
485
+ } else if (typeof this.utils?.generateWAMessageContent === "function") {
486
+ messageContent = await this.utils.generateWAMessageContent(storyData, {
487
+ upload: this.waUploadToServer
488
+ });
489
+ } else if (typeof this.utils?.prepareMessageContent === "function") {
490
+ messageContent = await this.utils.prepareMessageContent(storyData, {
491
+ upload: this.waUploadToServer
492
+ });
493
+ } else {
494
+ messageContent = await Utils_1.generateWAMessageContent(storyData, {
495
+ upload: this.waUploadToServer
496
+ });
497
+ }
498
+ }
499
+
500
+ let msg = {
501
+ message: {
502
+ groupStatusMessageV2: {
503
+ message: messageContent.message || messageContent
504
+ }
505
+ }
506
+ };
507
+
508
+ return await this.relayMessage(jid, msg.message, {
509
+ messageId: this.utils.generateMessageID()
510
+ });
511
+ }
512
+ async handleGbLabel(content, jid) {
513
+ const x = content.groupLabel;
514
+ if (!jid.endsWith('@g.us')) {
515
+ throw new Error('group required!')
516
+ }
517
+
518
+ const msg = this.utils.generateWAMessageFromContent(jid, {
519
+ protocolMessage: {
520
+ type: "GROUP_MEMBER_LABEL_CHANGE",
521
+ memberLabel: {
522
+ label: x.labelText.slice(0, 30)
523
+ }
524
+ }
525
+ }, {});
526
+ await this.relayMessage(jid, msg.message, {
527
+ additionalNodes: [
528
+ {
529
+ tag: 'meta',
530
+ attrs: {
531
+ tag_reason: 'user_update',
532
+ appdata: 'member_tag'
533
+ },
534
+ content: undefined
535
+ }
536
+ ]
537
+ })
538
+ }
539
+ }
540
+
541
+ module.exports = imup;
@@ -75,6 +75,40 @@ export declare const makeMessagesSocket: (config: SocketConfig) => {
75
75
  groupFetchAllParticipating: () => Promise<{
76
76
  [_: string]: import("../Types").GroupMetadata;
77
77
  }>;
78
+ communityMetadata: (jid: string) => Promise<CommunityMetadata>
79
+ communityCreate: (subject: string, body?: string) => Promise<GroupMetadata | null>
80
+ communityCreateGroup: (subject: string, participants: string[], parentCommunityJid: string) => Promise<GroupMetadata | null>
81
+ communityLeave: (id: string) => Promise<void>
82
+ communityUpdateSubject: (jid: string, subject: string) => Promise<void>
83
+ communityLinkGroup: (groupJid: string, parentCommunityJid: string) => Promise<void>
84
+ communityUnlinkGroup: (groupJid: string, parentCommunityJid: string) => Promise<void>
85
+ communityFetchLinkedGroups: (jid: string) => Promise<CommunityLinkedGroups>
86
+ communityRequestParticipantsList: (jid: string) => Promise<{ [key: string]: string }[]>
87
+ communityRequestParticipantsUpdate: (jid: string, participants: string[], action: 'approve' | 'reject') => Promise<{ status: string, jid: string }[]>
88
+ communityParticipantsUpdate: (jid: string, participants: string[], action: ParticipantAction) => Promise<{ status: string, jid: string, content: BinaryNode }[]>
89
+ communityUpdateDescription: (jid: string, description?: string) => Promise<void>
90
+ communityInviteCode: (jid: string) => Promise<string | undefined>
91
+ communityRevokeInvite: (jid: string) => Promise<string | undefined>
92
+ communityAcceptInvite: (code: string) => Promise<string | undefined>
93
+ /**
94
+ * revoke a v4 invite for someone
95
+ * @param communityJid community jid
96
+ * @param invitedJid jid of person you invited
97
+ * @returns true if successful
98
+ */
99
+ communityRevokeInviteV4: (communityJid: string, invitedJid: string) => Promise<boolean>
100
+ /**
101
+ * accept a CommunityInviteMessage
102
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
103
+ * @param inviteMessage the message to accept
104
+ */
105
+ communityAcceptInviteV4: (key: string | proto.IMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => Promise<string>
106
+ communityGetInviteInfo: (code: string) => Promise<CommunityMetadata>
107
+ communityToggleEphemeral: (jid: string, ephemeralExpiration?: number) => Promise<void>
108
+ communitySettingUpdate: (jid: string, setting: 'locked' | 'announcement' | 'not_announcement' | 'unlocked') => Promise<void>
109
+ communityMemberAddMode: (jid: string, mode: 'admin_add' | 'all_member_add') => Promise<void>
110
+ communityJoinApprovalMode: (jid: string, mode: 'on' | 'off') => Promise<void>
111
+ communityFetchAllParticipating: () => Promise<{ [_: string]: CommunityMetadata }>
78
112
  processingMutex: {
79
113
  mutex<T>(code: () => T | Promise<T>): Promise<T>;
80
114
  };