@vinzsocket/baileys 3.2.8 → 3.2.10
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.
- package/README.md +2896 -220
- package/lib/Socket/dugong.d.ts +54 -3
- package/lib/Socket/dugong.js +73 -9
- package/lib/Socket/messages-send.js +3 -2
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/messages.js +43 -1
- package/lib/Utils/story-card.js +107 -0
- package/package.json +5 -5
package/lib/Socket/dugong.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ export interface ExtendedMessageUtils {
|
|
|
22
22
|
generateWAMessageFromContent: (jid: string, content: any, options?: any) => any;
|
|
23
23
|
generateWAMessage: (jid: string, content: any, options?: any) => Promise<any>;
|
|
24
24
|
generateMessageID: () => string;
|
|
25
|
+
/** Optional — kalau ada, dipakai buat kasih hint <biz>/<meta is_vsn2="1"/> node ke relayMessage(). */
|
|
26
|
+
buildInteractiveAdditionalNodes?: (jid: string, m: any) => any[];
|
|
27
|
+
/** Optional — canvas-rendered story card generator, lihat Utils/story-card.js. */
|
|
28
|
+
renderStoryCard?: (options: StoryCardOptions) => Promise<Buffer>;
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
export interface StickerMessage {
|
|
@@ -144,13 +148,60 @@ export interface PollResultMessage {
|
|
|
144
148
|
pollVotes: PollVote[];
|
|
145
149
|
}
|
|
146
150
|
|
|
151
|
+
export interface StoryCardOptions {
|
|
152
|
+
title?: string;
|
|
153
|
+
body?: string;
|
|
154
|
+
footer?: string;
|
|
155
|
+
gradient?: 'sunset' | 'purple' | 'ocean' | 'forest' | 'midnight' | 'fire';
|
|
156
|
+
colors?: [string, string];
|
|
157
|
+
direction?: 'vertical' | 'diagonal';
|
|
158
|
+
width?: number;
|
|
159
|
+
height?: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
147
162
|
export interface GroupStatusMessage {
|
|
163
|
+
/** Mode 3 (advanced): pre-built inner WAMessage — di-wrap langsung dengan messageSecret. */
|
|
148
164
|
message?: any;
|
|
149
|
-
|
|
150
|
-
|
|
165
|
+
/** Mode 1: raw relay / forward story langsung, bypass semua processing lain. */
|
|
166
|
+
raw?: any;
|
|
167
|
+
|
|
168
|
+
/** Teks story. Kalau kosong dan gak ada media lain, fallback text-mode dengan string kosong. */
|
|
151
169
|
text?: string;
|
|
170
|
+
/** Dipakai sebagai caption untuk media, atau fallback text kalau `text` kosong. */
|
|
152
171
|
caption?: string;
|
|
172
|
+
|
|
173
|
+
image?: string | Buffer | { url: string };
|
|
174
|
+
video?: string | Buffer | { url: string };
|
|
175
|
+
/** Shorthand: dikirim sebagai video looping (`gifPlayback: true` otomatis di-set). */
|
|
176
|
+
gif?: string | Buffer | { url: string };
|
|
177
|
+
audio?: string | Buffer | { url: string };
|
|
178
|
+
sticker?: string | Buffer | { url: string };
|
|
153
179
|
document?: string | Buffer | { url: string };
|
|
180
|
+
/** Canvas-rendered story card (gradient + title/body/footer), lihat Utils/story-card.js. */
|
|
181
|
+
card?: StoryCardOptions;
|
|
182
|
+
|
|
183
|
+
/** Teks & audio (ptt) saja — ARGB hex (mis. 0xFF128C7E). */
|
|
184
|
+
backgroundColor?: number;
|
|
185
|
+
/** Teks saja. */
|
|
186
|
+
textArgb?: number;
|
|
187
|
+
/** Teks saja, 0-5. */
|
|
188
|
+
font?: number;
|
|
189
|
+
|
|
190
|
+
/** Video & gif. */
|
|
191
|
+
gifPlayback?: boolean;
|
|
192
|
+
/** Video/audio — durasi dalam detik. */
|
|
193
|
+
seconds?: number;
|
|
194
|
+
|
|
195
|
+
/** Audio: voice note (true, default) atau musik biasa (false). */
|
|
196
|
+
ptt?: boolean;
|
|
197
|
+
/** Audio/document. */
|
|
198
|
+
mimetype?: string;
|
|
199
|
+
/** Document. */
|
|
200
|
+
fileName?: string;
|
|
201
|
+
|
|
202
|
+
/** Didukung di semua mode (text/image/card/gif/video/audio/sticker/document). */
|
|
203
|
+
mentions?: string[];
|
|
204
|
+
|
|
154
205
|
[key: string]: any;
|
|
155
206
|
}
|
|
156
207
|
|
|
@@ -199,7 +250,7 @@ export declare class ExtendedMessageBuilder {
|
|
|
199
250
|
|
|
200
251
|
handlePollResult(content: { pollResultMessage: PollResultMessage }, jid: string, quoted?: proto.IWebMessageInfo | null): Promise<any>;
|
|
201
252
|
|
|
202
|
-
handleGroupStory(content: { groupStatusMessage: GroupStatusMessage }, jid: string
|
|
253
|
+
handleGroupStory(content: { groupStatusMessage: GroupStatusMessage }, jid: string): Promise<any>;
|
|
203
254
|
}
|
|
204
255
|
|
|
205
256
|
export default ExtendedMessageBuilder;
|
package/lib/Socket/dugong.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// dugong.js — Harps-Baileys extended message builders v3.2.
|
|
1
|
+
// dugong.js — Harps-Baileys extended message builders v3.2.10
|
|
2
2
|
// Handles: Payment, Product, Interactive (NF V7 full), Album, Event,
|
|
3
3
|
// Poll Result, Group Status V2, Nested Legacy List, Multi-Select.
|
|
4
4
|
|
|
@@ -337,21 +337,32 @@ export class ExtendedMessageBuilder {
|
|
|
337
337
|
/**
|
|
338
338
|
* Group Status V2 — kirim story/status ke grup.
|
|
339
339
|
* messageSecret wajib ada supaya WA client bisa decrypt & tampilkan konten.
|
|
340
|
+
*
|
|
341
|
+
* PATCH (v3.2.10): sekarang menyertakan `additionalNodes` via
|
|
342
|
+
* buildInteractiveAdditionalNodes() di SETIAP relayMessage() call. Tanpa ini,
|
|
343
|
+
* node <meta is_vsn2="1"/> (yang sudah punya cabang logic-nya sendiri di
|
|
344
|
+
* Utils/messages.js buildInteractiveAdditionalNodes) tidak pernah benar-benar
|
|
345
|
+
* terkirim ke server, sehingga klien penerima tidak tahu payload ini harus
|
|
346
|
+
* dirender sebagai story/status grup — pesan tetap terkirim & ter-enkripsi
|
|
347
|
+
* normal, tapi tidak pernah muncul sebagai ring status untuk member lain.
|
|
340
348
|
*/
|
|
341
349
|
async handleGroupStory(content, jid) {
|
|
342
350
|
const s = content.groupStatusMessage;
|
|
343
351
|
const secret = randomBytes(32);
|
|
352
|
+
const buildNodes = this.utils.buildInteractiveAdditionalNodes;
|
|
344
353
|
|
|
345
354
|
// Mode 1: raw relay langsung
|
|
346
355
|
if (s.raw) {
|
|
356
|
+
const additionalNodes = buildNodes ? buildNodes(jid, s.raw) : [];
|
|
347
357
|
return await this.relayMessage(jid, s.raw, {
|
|
348
|
-
messageId: this.bail.generateMessageID()
|
|
358
|
+
messageId: this.bail.generateMessageID(),
|
|
359
|
+
additionalNodes
|
|
349
360
|
});
|
|
350
361
|
}
|
|
351
362
|
|
|
352
363
|
// Mode 2: inner WAMessage sudah disiapkan — wrap dengan secret
|
|
353
364
|
if (s.message) {
|
|
354
|
-
|
|
365
|
+
const wrapped = {
|
|
355
366
|
groupStatusMessageV2: {
|
|
356
367
|
message: {
|
|
357
368
|
messageContextInfo: {
|
|
@@ -362,20 +373,29 @@ export class ExtendedMessageBuilder {
|
|
|
362
373
|
...s.message
|
|
363
374
|
}
|
|
364
375
|
}
|
|
365
|
-
}
|
|
376
|
+
};
|
|
377
|
+
const additionalNodes = buildNodes ? buildNodes(jid, wrapped) : [];
|
|
378
|
+
return await this.relayMessage(jid, wrapped, {
|
|
379
|
+
messageId: this.bail.generateMessageID(),
|
|
380
|
+
additionalNodes
|
|
381
|
+
});
|
|
366
382
|
}
|
|
367
383
|
|
|
368
384
|
// Mode 3: generate dari media/teks
|
|
369
385
|
let innerMessage = {};
|
|
386
|
+
const mentionCtx = (Array.isArray(s.mentions) && s.mentions.length)
|
|
387
|
+
? { contextInfo: { mentionedJid: s.mentions } }
|
|
388
|
+
: {};
|
|
370
389
|
|
|
371
|
-
if (s.text || (!s.image && !s.video && !s.audio && !s.sticker && !s.document)) {
|
|
390
|
+
if (s.text || (!s.image && !s.video && !s.gif && !s.audio && !s.sticker && !s.document)) {
|
|
372
391
|
// Teks
|
|
373
392
|
innerMessage = {
|
|
374
393
|
extendedTextMessage: {
|
|
375
394
|
text: s.text || s.caption || '',
|
|
376
395
|
...(typeof s.backgroundColor !== 'undefined' ? { backgroundArgb: s.backgroundColor } : {}),
|
|
377
396
|
...(typeof s.textArgb !== 'undefined' ? { textArgb: s.textArgb } : {}),
|
|
378
|
-
...(typeof s.font !== 'undefined' ? { font: s.font } : {})
|
|
397
|
+
...(typeof s.font !== 'undefined' ? { font: s.font } : {}),
|
|
398
|
+
...mentionCtx
|
|
379
399
|
}
|
|
380
400
|
};
|
|
381
401
|
} else if (s.image) {
|
|
@@ -384,6 +404,30 @@ export class ExtendedMessageBuilder {
|
|
|
384
404
|
{ upload: this.waUploadToServer }
|
|
385
405
|
);
|
|
386
406
|
if (s.caption) media.imageMessage.caption = s.caption;
|
|
407
|
+
if (mentionCtx.contextInfo) media.imageMessage.contextInfo = { ...media.imageMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
408
|
+
innerMessage = media;
|
|
409
|
+
} else if (s.card) {
|
|
410
|
+
// Mode: canvas-rendered story card (gradient + title/body/footer text),
|
|
411
|
+
// see lib/Utils/story-card.js. `s.card` accepts the same options as
|
|
412
|
+
// renderStoryCard() — e.g. { title, body, footer, gradient, direction }.
|
|
413
|
+
const cardBuffer = await this.utils.renderStoryCard(s.card);
|
|
414
|
+
const media = await this.utils.prepareWAMessageMedia(
|
|
415
|
+
{ image: cardBuffer },
|
|
416
|
+
{ upload: this.waUploadToServer }
|
|
417
|
+
);
|
|
418
|
+
if (s.caption) media.imageMessage.caption = s.caption;
|
|
419
|
+
if (mentionCtx.contextInfo) media.imageMessage.contextInfo = { ...media.imageMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
420
|
+
innerMessage = media;
|
|
421
|
+
} else if (s.gif) {
|
|
422
|
+
// Shorthand: { gif: Buffer | { url }, caption? } — dikirim sebagai video looping,
|
|
423
|
+
// sama seperti representasi GIF asli WhatsApp.
|
|
424
|
+
const media = await this.utils.prepareWAMessageMedia(
|
|
425
|
+
{ video: s.gif },
|
|
426
|
+
{ upload: this.waUploadToServer }
|
|
427
|
+
);
|
|
428
|
+
if (s.caption) media.videoMessage.caption = s.caption;
|
|
429
|
+
media.videoMessage.gifPlayback = true;
|
|
430
|
+
if (mentionCtx.contextInfo) media.videoMessage.contextInfo = { ...media.videoMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
387
431
|
innerMessage = media;
|
|
388
432
|
} else if (s.video) {
|
|
389
433
|
const media = await this.utils.prepareWAMessageMedia(
|
|
@@ -393,6 +437,7 @@ export class ExtendedMessageBuilder {
|
|
|
393
437
|
if (s.caption) media.videoMessage.caption = s.caption;
|
|
394
438
|
if (s.gifPlayback) media.videoMessage.gifPlayback = true;
|
|
395
439
|
if (s.seconds) media.videoMessage.seconds = s.seconds;
|
|
440
|
+
if (mentionCtx.contextInfo) media.videoMessage.contextInfo = { ...media.videoMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
396
441
|
innerMessage = media;
|
|
397
442
|
} else if (s.audio) {
|
|
398
443
|
const media = await this.utils.prepareWAMessageMedia(
|
|
@@ -402,14 +447,23 @@ export class ExtendedMessageBuilder {
|
|
|
402
447
|
...(s.seconds ? { seconds: s.seconds } : {}),
|
|
403
448
|
...(s.mimetype ? { mimetype: s.mimetype } : {})
|
|
404
449
|
},
|
|
405
|
-
{
|
|
450
|
+
{
|
|
451
|
+
upload: this.waUploadToServer,
|
|
452
|
+
// NOTE: backgroundColor harus lewat `options` (argumen ke-2), bukan payload
|
|
453
|
+
// media — prepareWAMessageMedia() cuma hitung backgroundArgb untuk ptt audio
|
|
454
|
+
// kalau options.backgroundColor keisi. Sebelumnya gak pernah diteruskan di sini,
|
|
455
|
+
// jadi backgroundColor pada audio story selalu diam-diam diabaikan.
|
|
456
|
+
...(typeof s.backgroundColor !== 'undefined' ? { backgroundColor: s.backgroundColor } : {})
|
|
457
|
+
}
|
|
406
458
|
);
|
|
459
|
+
if (mentionCtx.contextInfo) media.audioMessage.contextInfo = { ...media.audioMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
407
460
|
innerMessage = media;
|
|
408
461
|
} else if (s.sticker) {
|
|
409
462
|
const media = await this.utils.prepareWAMessageMedia(
|
|
410
463
|
{ sticker: s.sticker },
|
|
411
464
|
{ upload: this.waUploadToServer }
|
|
412
465
|
);
|
|
466
|
+
if (mentionCtx.contextInfo) media.stickerMessage.contextInfo = { ...media.stickerMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
413
467
|
innerMessage = media;
|
|
414
468
|
} else if (s.document) {
|
|
415
469
|
const media = await this.utils.prepareWAMessageMedia(
|
|
@@ -422,13 +476,14 @@ export class ExtendedMessageBuilder {
|
|
|
422
476
|
);
|
|
423
477
|
if (s.caption && media.documentMessage) media.documentMessage.caption = s.caption;
|
|
424
478
|
if (s.fileName && media.documentMessage) media.documentMessage.fileName = s.fileName;
|
|
479
|
+
if (mentionCtx.contextInfo && media.documentMessage) media.documentMessage.contextInfo = { ...media.documentMessage.contextInfo, ...mentionCtx.contextInfo };
|
|
425
480
|
innerMessage = media;
|
|
426
481
|
} else {
|
|
427
482
|
innerMessage = await this.bail.generateWAMessageContent(s, { upload: this.waUploadToServer });
|
|
428
483
|
}
|
|
429
484
|
|
|
430
485
|
// Wrap dengan messageContextInfo + messageSecret — WAJIB supaya konten bisa ditampilkan
|
|
431
|
-
|
|
486
|
+
const wrapped = {
|
|
432
487
|
groupStatusMessageV2: {
|
|
433
488
|
message: {
|
|
434
489
|
messageContextInfo: {
|
|
@@ -439,7 +494,16 @@ export class ExtendedMessageBuilder {
|
|
|
439
494
|
...innerMessage
|
|
440
495
|
}
|
|
441
496
|
}
|
|
442
|
-
}
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
// Ini bagian yang tadinya hilang: <meta is_vsn2="1"/> baru benar-benar
|
|
500
|
+
// terpasang lewat additionalNodes di bawah ini.
|
|
501
|
+
const additionalNodes = buildNodes ? buildNodes(jid, wrapped) : [];
|
|
502
|
+
|
|
503
|
+
return await this.relayMessage(jid, wrapped, {
|
|
504
|
+
messageId: this.bail.generateMessageID(),
|
|
505
|
+
additionalNodes
|
|
506
|
+
});
|
|
443
507
|
}
|
|
444
508
|
}
|
|
445
509
|
|
|
@@ -2,7 +2,7 @@ import NodeCache from '@cacheable/node-cache';
|
|
|
2
2
|
import { Boom } from '@hapi/boom';
|
|
3
3
|
import { proto } from '../../WAProto/index.js';
|
|
4
4
|
import { DEFAULT_CACHE_TTLS, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
|
|
5
|
-
import { aggregateMessageKeysNotFromMe, assertMediaContent, assertMeId, bindWaitForEvent, buildInteractiveAdditionalNodes, decryptMediaRetryData, DEF_MEDIA_HOST, encodeNewsletterMessage, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, extractDeviceJids, generateMessageID, generateMessageIDV2, generateParticipantHashV2, generateWAMessage, generateWAMessageContent, generateWAMessageFromContent, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, MessageRetryManager, normalizeMessageContent, parseAndInjectE2ESessions, prepareWAMessageMedia, unixTimestampSeconds } from '../Utils/index.js';
|
|
5
|
+
import { aggregateMessageKeysNotFromMe, assertMediaContent, assertMeId, bindWaitForEvent, buildInteractiveAdditionalNodes, decryptMediaRetryData, DEF_MEDIA_HOST, encodeNewsletterMessage, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, extractDeviceJids, generateMessageID, generateMessageIDV2, generateParticipantHashV2, generateWAMessage, generateWAMessageContent, generateWAMessageFromContent, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, MessageRetryManager, normalizeMessageContent, parseAndInjectE2ESessions, prepareWAMessageMedia, renderStoryCard, unixTimestampSeconds } from '../Utils/index.js';
|
|
6
6
|
import { getUrlInfo } from '../Utils/link-preview.js';
|
|
7
7
|
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex.js';
|
|
8
8
|
import { getMessageReportingToken, shouldIncludeReportingToken } from '../Utils/reporting-utils.js';
|
|
@@ -984,7 +984,8 @@ export const makeMessagesSocket = (config) => {
|
|
|
984
984
|
generateMessageID,
|
|
985
985
|
generateWAMessage,
|
|
986
986
|
generateWAMessageFromContent,
|
|
987
|
-
prepareWAMessageMedia
|
|
987
|
+
prepareWAMessageMedia,
|
|
988
|
+
renderStoryCard
|
|
988
989
|
}, waUploadToServer, relayMessage);
|
|
989
990
|
const waitForMsgMediaUpdate = bindWaitForEvent(ev, 'messages.media-update');
|
|
990
991
|
registerSocketEndHandler(() => {
|
package/lib/Utils/index.js
CHANGED
package/lib/Utils/messages.js
CHANGED
|
@@ -393,13 +393,14 @@ export const buildInteractiveAdditionalNodes = (jid, m) => {
|
|
|
393
393
|
content: [{ tag: 'list', attrs: { type: 'product_list', v: '2' } }]
|
|
394
394
|
});
|
|
395
395
|
} else if (inner.interactiveMessage || inner.buttonsMessage || inner.templateMessage || inner.productMessage) {
|
|
396
|
+
const isCarousel = !!inner.interactiveMessage?.carouselMessage;
|
|
396
397
|
nodes.push({
|
|
397
398
|
tag: 'biz',
|
|
398
399
|
attrs: {},
|
|
399
400
|
content: [{
|
|
400
401
|
tag: 'interactive',
|
|
401
402
|
attrs: { type: 'native_flow', v: '1' },
|
|
402
|
-
content: [{ tag: 'native_flow', attrs: { name: 'mixed', v: '9' } }]
|
|
403
|
+
content: [{ tag: 'native_flow', attrs: { name: isCarousel ? 'carousel' : 'mixed', v: '9' } }]
|
|
403
404
|
}]
|
|
404
405
|
});
|
|
405
406
|
} else if (inner.groupStatusMessageV2) {
|
|
@@ -691,6 +692,47 @@ export const generateWAMessageContent = async (message, options) => {
|
|
|
691
692
|
}
|
|
692
693
|
};
|
|
693
694
|
}
|
|
695
|
+
else if (hasNonNullishProperty(message, 'cards')) {
|
|
696
|
+
// Convenience: top-level `cards` shorthand -> interactiveMessage.carouselMessage
|
|
697
|
+
// (horizontal-scroll product/card carousel). Each entry in `cards` may contain:
|
|
698
|
+
// { image | video, title?, body?, footer?, buttons? }
|
|
699
|
+
// `buttons` per card accepts the same shapes as the top-level `buttons` shorthand.
|
|
700
|
+
if (!Array.isArray(message.cards) || !message.cards.length) {
|
|
701
|
+
throw new Boom('cards must be a non-empty array', { statusCode: 400 });
|
|
702
|
+
}
|
|
703
|
+
const cards = await Promise.all(message.cards.map(async (card) => {
|
|
704
|
+
const cardButtons = normalizeToNativeFlowButtons(card.interactiveButtons || card.buttons || card.templateButtons || []);
|
|
705
|
+
const cardInteractive = {
|
|
706
|
+
body: { text: card.text || card.body || card.caption || '' },
|
|
707
|
+
footer: { text: card.footer || '' },
|
|
708
|
+
nativeFlowMessage: { buttons: cardButtons }
|
|
709
|
+
};
|
|
710
|
+
if (card.image || card.video) {
|
|
711
|
+
const mediaContent = await prepareWAMessageMedia(card.image ? { image: card.image } : { video: card.video }, options);
|
|
712
|
+
const mediaType = Object.keys(mediaContent)[0];
|
|
713
|
+
cardInteractive.header = {
|
|
714
|
+
title: card.title || '',
|
|
715
|
+
hasMediaAttachment: true,
|
|
716
|
+
...mediaContent[mediaType]
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
else if (card.title) {
|
|
720
|
+
cardInteractive.header = { title: card.title, hasMediaAttachment: false };
|
|
721
|
+
}
|
|
722
|
+
return cardInteractive;
|
|
723
|
+
}));
|
|
724
|
+
m = {
|
|
725
|
+
interactiveMessage: {
|
|
726
|
+
body: { text: message.text || message.caption || '' },
|
|
727
|
+
footer: { text: message.footer || '' },
|
|
728
|
+
carouselMessage: {
|
|
729
|
+
cards,
|
|
730
|
+
messageVersion: 1,
|
|
731
|
+
carouselCardType: proto.Message.InteractiveMessage.CarouselMessage.CarouselCardType.HSCROLL_CARDS
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
}
|
|
694
736
|
if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
|
|
695
737
|
m = { viewOnceMessage: { message: m } };
|
|
696
738
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
/**
|
|
3
|
+
* Canvas-rendered "story card" — generates a styled status/story image (gradient
|
|
4
|
+
* background, title, body text, optional footer/branding strip) entirely in JS
|
|
5
|
+
* via Jimp (already an optional peer dependency of this library, no native
|
|
6
|
+
* binary required). Useful for visually distinctive WhatsApp Status / group
|
|
7
|
+
* story (`groupStatusMessageV2`) posts without needing a design tool upstream.
|
|
8
|
+
*
|
|
9
|
+
* Lazily imports `jimp` so this module has zero cost / zero hard dependency
|
|
10
|
+
* for consumers who never call it.
|
|
11
|
+
*/
|
|
12
|
+
let _jimpLib;
|
|
13
|
+
const getJimp = async () => {
|
|
14
|
+
if (_jimpLib) return _jimpLib;
|
|
15
|
+
try {
|
|
16
|
+
// @ts-ignore - optional peer dependency
|
|
17
|
+
const [core, fonts] = await Promise.all([import('jimp'), import('jimp/fonts')]);
|
|
18
|
+
_jimpLib = { ...core, fonts };
|
|
19
|
+
return _jimpLib;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Boom('renderStoryCard() requires the optional "jimp" peer dependency. Install it with: npm i jimp', { statusCode: 500 });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const DEFAULT_GRADIENTS = {
|
|
26
|
+
sunset: [0xff7e5fff, 0xfeb47bff],
|
|
27
|
+
purple: [0x8e2de2ff, 0x4a00e0ff],
|
|
28
|
+
ocean: [0x2193b0ff, 0x6dd5edff],
|
|
29
|
+
forest: [0x134e5eff, 0x71b280ff],
|
|
30
|
+
midnight: [0x232526ff, 0x414345ff],
|
|
31
|
+
fire: [0xf12711ff, 0xf5af19ff]
|
|
32
|
+
};
|
|
33
|
+
const lerpChannel = (a, b, t) => Math.round(a + (b - a) * t);
|
|
34
|
+
const lerpColor = (c1, c2, t, jimpLib) => {
|
|
35
|
+
const { r: r1, g: g1, b: b1, a: a1 } = jimpLib.intToRGBA(c1);
|
|
36
|
+
const { r: r2, g: g2, b: b2, a: a2 } = jimpLib.intToRGBA(c2);
|
|
37
|
+
return jimpLib.rgbaToInt(lerpChannel(r1, r2, t), lerpChannel(g1, g2, t), lerpChannel(b1, b2, t), lerpChannel(a1, a2, t));
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Renders a styled story card and returns a PNG buffer.
|
|
41
|
+
*
|
|
42
|
+
* @param {object} opts
|
|
43
|
+
* @param {string} [opts.title] - large headline text
|
|
44
|
+
* @param {string} [opts.body] - smaller body/subtitle text below the title
|
|
45
|
+
* @param {string} [opts.footer] - small branding/footer line pinned to the bottom
|
|
46
|
+
* @param {'sunset'|'purple'|'ocean'|'forest'|'midnight'|'fire'} [opts.gradient='purple'] - preset background gradient
|
|
47
|
+
* @param {[string|number, string|number]} [opts.colors] - explicit [from, to] ARGB hex colors, overrides `gradient`
|
|
48
|
+
* @param {'vertical'|'diagonal'} [opts.direction='vertical'] - gradient direction
|
|
49
|
+
* @param {number} [opts.width=1080] - canvas width (WA status default is 1080x1920)
|
|
50
|
+
* @param {number} [opts.height=1920] - canvas height
|
|
51
|
+
* @returns {Promise<Buffer>} PNG image buffer, ready to pass as `{ image: buffer }`
|
|
52
|
+
*/
|
|
53
|
+
export const renderStoryCard = async (opts = {}) => {
|
|
54
|
+
const jimpLib = await getJimp();
|
|
55
|
+
const { Jimp, HorizontalAlign, VerticalAlign, loadFont, measureTextHeight, fonts } = jimpLib;
|
|
56
|
+
const width = opts.width || 1080;
|
|
57
|
+
const height = opts.height || 1920;
|
|
58
|
+
const direction = opts.direction || 'vertical';
|
|
59
|
+
const [from, to] = opts.colors || DEFAULT_GRADIENTS[opts.gradient] || DEFAULT_GRADIENTS.purple;
|
|
60
|
+
const colorFrom = typeof from === 'string' ? parseInt(from.replace('#', ''), 16) : from;
|
|
61
|
+
const colorTo = typeof to === 'string' ? parseInt(to.replace('#', ''), 16) : to;
|
|
62
|
+
const image = new Jimp({ width, height, color: colorFrom });
|
|
63
|
+
// Paint the gradient pixel-row-by-row (vertical) or via a diagonal blend factor (diagonal)
|
|
64
|
+
for (let y = 0; y < height; y++) {
|
|
65
|
+
for (let x = 0; x < width; x++) {
|
|
66
|
+
const t = direction === 'diagonal' ? (x / width + y / height) / 2 : y / height;
|
|
67
|
+
image.setPixelColor(lerpColor(colorFrom, colorTo, t, jimpLib), x, y);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const titleFont = await loadFont(fonts.SANS_64_WHITE);
|
|
71
|
+
const bodyFont = await loadFont(fonts.SANS_32_WHITE);
|
|
72
|
+
const footerFont = await loadFont(fonts.SANS_16_WHITE);
|
|
73
|
+
const padding = Math.round(width * 0.08);
|
|
74
|
+
const contentWidth = width - padding * 2;
|
|
75
|
+
let cursorY = Math.round(height * 0.38);
|
|
76
|
+
if (opts.title) {
|
|
77
|
+
const titleHeight = measureTextHeight(titleFont, opts.title, contentWidth);
|
|
78
|
+
image.print({
|
|
79
|
+
font: titleFont,
|
|
80
|
+
x: padding,
|
|
81
|
+
y: cursorY,
|
|
82
|
+
text: { text: opts.title, alignmentX: HorizontalAlign.LEFT, alignmentY: VerticalAlign.TOP },
|
|
83
|
+
maxWidth: contentWidth
|
|
84
|
+
});
|
|
85
|
+
cursorY += titleHeight + Math.round(height * 0.03);
|
|
86
|
+
}
|
|
87
|
+
if (opts.body) {
|
|
88
|
+
image.print({
|
|
89
|
+
font: bodyFont,
|
|
90
|
+
x: padding,
|
|
91
|
+
y: cursorY,
|
|
92
|
+
text: { text: opts.body, alignmentX: HorizontalAlign.LEFT, alignmentY: VerticalAlign.TOP },
|
|
93
|
+
maxWidth: contentWidth
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (opts.footer) {
|
|
97
|
+
image.print({
|
|
98
|
+
font: footerFont,
|
|
99
|
+
x: padding,
|
|
100
|
+
y: height - Math.round(height * 0.06),
|
|
101
|
+
text: { text: opts.footer, alignmentX: HorizontalAlign.LEFT, alignmentY: VerticalAlign.TOP },
|
|
102
|
+
maxWidth: contentWidth
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return image.getBuffer('image/png');
|
|
106
|
+
};
|
|
107
|
+
export const STORY_GRADIENTS = Object.keys(DEFAULT_GRADIENTS);
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vinzsocket/baileys",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.2.
|
|
4
|
+
"version": "3.2.10",
|
|
5
5
|
"description": "A WebSockets library for interacting with WhatsApp Web",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"whatsapp",
|
|
8
8
|
"automation"
|
|
9
9
|
],
|
|
10
|
-
"homepage": "https://github.com/
|
|
10
|
+
"homepage": "https://github.com/VinzSocket/baileys/",
|
|
11
11
|
"repository": {
|
|
12
|
-
"url": "git@github.com:
|
|
12
|
+
"url": "git@github.com:VinzSocket/baileys.git"
|
|
13
13
|
},
|
|
14
14
|
"license": "MIT",
|
|
15
|
-
"author": "
|
|
15
|
+
"author": "vinzsocket",
|
|
16
16
|
"main": "lib/index.js",
|
|
17
17
|
"types": "lib/index.d.ts",
|
|
18
18
|
"files": [
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"@types/ws": "^8.0.0",
|
|
62
62
|
"@typescript-eslint/eslint-plugin": "^8",
|
|
63
63
|
"@typescript-eslint/parser": "^8",
|
|
64
|
-
"@
|
|
64
|
+
"@vinzsocket/eslint-config": "^1.0.0",
|
|
65
65
|
"conventional-changelog": "^7.1.1",
|
|
66
66
|
"conventional-changelog-angular": "^8.0.0",
|
|
67
67
|
"esbuild-register": "^3.6.0",
|