@jiancodes/baileys 1.7.3 → 2.0.3

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.
@@ -1,620 +0,0 @@
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) {
7
- this.utils = utils;
8
- this.relayMessage = relayMessageFn
9
- this.waUploadToServer = waUploadToServer;
10
- }
11
-
12
- detectType(content) {
13
- if (content.requestPaymentMessage) return 'PAYMENT';
14
- if (content.productMessage) return 'PRODUCT';
15
- if (content.interactiveMessage) return 'INTERACTIVE';
16
- if (content.albumMessage) return 'ALBUM';
17
- if (content.eventMessage) return 'EVENT';
18
- if (content.pollResultMessage) return 'POLL_RESULT';
19
- if (content.statusMentionMessage) return 'STATUS_MENTION';
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: "0@newsletter",
324
- serverMessageId: 1,
325
- newsletterName: `WhatsApp`,
326
- contentType: 1,
327
- timestamp: new Date().toISOString(),
328
- senderName: "JianCoder",
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: "Faa About Information",
389
- newsletterJid: "120363399266188954@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 || "120363399266188954@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 handleStMention(content, jid, quoted) {
451
- const data = content.statusMentionMessage;
452
- const media = null;
453
- const mediaType = null;
454
-
455
- if (data.image) {
456
- if (typeof data.image === 'object' && data.image.url) {
457
- media = await this.utils.prepareWAMessageMedia(
458
- { image: { url: data.image.url } },
459
- { upload: this.waUploadToServer }
460
- );
461
- } else {
462
- media = await this.utils.prepareWAMessageMedia(
463
- { image: data.image },
464
- { upload: this.waUploadToServer }
465
- );
466
- }
467
- mediaType = 'image';
468
- } else if (data.video) {
469
- if (typeof data.video === 'object' && data.video.url) {
470
- media = await this.utils.prepareWAMessageMedia(
471
- { video: { url: data.video.url } },
472
- { upload: this.waUploadToServer }
473
- );
474
- } else {
475
- media = await this.utils.prepareWAMessageMedia(
476
- { video: data.video },
477
- { upload: this.waUploadToServer }
478
- );
479
- }
480
- mediaType = 'video';
481
- };
482
- let msg = await this.relayMessage("status@broadcast", {
483
- ...media
484
- }, {
485
- statusJidList: [data.mentions, this.user.id],
486
- additionalNodes: [{
487
- tag: "meta",
488
- attrs: {},
489
- content: [
490
- {
491
- tag: "mentioned_users",
492
- attrs: {},
493
- content: [
494
- {
495
- tag: "to",
496
- attrs: { jid: target },
497
- content: undefined,
498
- }
499
- ]
500
- }
501
- ],
502
- }]
503
- });
504
-
505
- let xontols = await this.utils.generateWAMessageFromContent(jid, {
506
- statusMentionMessage: {
507
- message: {
508
- protocolMessage: {
509
- messageId: msg.key,
510
- type: "STATUS_MENTION_MESSAGE"
511
- }
512
- }
513
- }
514
- }, {
515
- addtionalNodes: [
516
- {
517
- tag: "meta",
518
- attrs: { "is_status_mention": true },
519
- content: undefined
520
- }
521
- ]
522
- });
523
-
524
- await this.relayMessage(jid, xontols.message, {
525
- messageId: xontols.key.id
526
- })
527
- return xontols
528
- }
529
- async handleOrderMessage(content, jid, quoted) {
530
- const orderData = content.orderMessage;
531
-
532
- const Haha = await this.utils.generateWAMessageFromContent(jid, {
533
- orderMessage: {
534
- orderId: "JIANCODE25022008",
535
- thumbnail: orderData.thumbnail || null,
536
- itemCount: orderData.itemCount || 0,
537
- status: "ACCEPTED",
538
- surface: "CATALOG",
539
- message: orderData.message,
540
- orderTitle: orderData.orderTitle,
541
- sellerJid: "0@whatsapp.net",
542
- token: "JIANCODR_EXAMPLE_TOKEN",
543
- totalAmount1000: orderData.totalAmount1000 || 0,
544
- totalCurrencyCode: orderData.totalCurrencyCode || "IDR",
545
- messageVersion: 2
546
- }
547
- }, { quoted:quoted });
548
-
549
- await this.relayMessage(jid, Haha.message, {});
550
- return Haha;
551
- }
552
-
553
- async handleGroupStory(content, jid, quoted) {
554
- const storyData = content.groupStatus;
555
- let messageContent;
556
-
557
- if (storyData.message) {
558
- messageContent = storyData;
559
- } else {
560
- if (typeof this.utils?.generateWAMessageContent === "function") {
561
- messageContent = await this.utils.generateWAMessageContent(storyData, {
562
- upload: this.waUploadToServer
563
- });
564
- } else if (typeof this.utils?.generateWAMessageContent === "function") {
565
- messageContent = await this.utils.generateWAMessageContent(storyData, {
566
- upload: this.waUploadToServer
567
- });
568
- } else if (typeof this.utils?.prepareMessageContent === "function") {
569
- messageContent = await this.utils.prepareMessageContent(storyData, {
570
- upload: this.waUploadToServer
571
- });
572
- } else {
573
- messageContent = await Utils_1.generateWAMessageContent(storyData, {
574
- upload: this.waUploadToServer
575
- });
576
- }
577
- }
578
-
579
- let msg = {
580
- message: {
581
- groupStatusMessageV2: {
582
- message: messageContent.message || messageContent
583
- }
584
- }
585
- };
586
-
587
- return await this.relayMessage(jid, msg.message, {
588
- messageId: this.utils.generateMessageID()
589
- });
590
- }
591
- async handleGbLabel(content, jid) {
592
- const x = content.groupLabel;
593
- if (!jid.endsWith('@g.us')) {
594
- throw new Error('hanya untuk group')
595
- }
596
-
597
- const msg = this.utils.generateWAMessageFromContent(jid, {
598
- protocolMessage: {
599
- type: "GROUP_MEMBER_LABEL_CHANGE",
600
- memberLabel: {
601
- label: x.labelText.slice(0, 30)
602
- }
603
- }
604
- }, {});
605
- await this.relayMessage(jid, msg.message, {
606
- additionalNodes: [
607
- {
608
- tag: 'meta',
609
- attrs: {
610
- tag_reason: 'user_update',
611
- appdata: 'member_tag'
612
- },
613
- content: undefined
614
- }
615
- ]
616
- })
617
- }
618
- }
619
-
620
- module.exports = imup;