@chatbridge-it/baileys 1.0.3 → 1.0.5
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 +48 -62
- package/lib/Socket/groups.js +4 -1
- package/lib/Socket/messages-recv.js +11 -2
- package/lib/Socket/socket.js +2 -4
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/lid-mapping.js +88 -0
- package/lib/Utils/messages-media.js +30 -1
- package/lib/Utils/messages.js +152 -9
- package/lib/WABinary/generic-utils.js +13 -0
- package/lib/index.js +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ A WhatsApp Web Multi-Device API library for Node.js. It connects to WhatsApp dir
|
|
|
19
19
|
- [Authentication](#authentication)
|
|
20
20
|
- [Sending Messages](#sending-messages)
|
|
21
21
|
- [Interactive Messages](#interactive-messages)
|
|
22
|
+
- [Mentions](#mentions)
|
|
22
23
|
- [Groups](#groups)
|
|
23
24
|
- [Newsletters (Channels)](#newsletters-channels)
|
|
24
25
|
- [Contacts, Profiles and Presence](#contacts-profiles-and-presence)
|
|
@@ -347,81 +348,66 @@ Supported presence types: `available`, `unavailable`, `composing`, `recording`,
|
|
|
347
348
|
|
|
348
349
|
## Interactive Messages
|
|
349
350
|
|
|
350
|
-
Interactive messages add a text body, an optional header
|
|
351
|
+
Interactive messages add a text body, an optional header title, a footer and native-flow buttons. Pass the buttons in `interactiveButtons`.
|
|
351
352
|
|
|
352
353
|
```js
|
|
353
354
|
await sock.sendMessage(jid, {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
]
|
|
382
|
-
}
|
|
355
|
+
text: "Your order has shipped.", // body
|
|
356
|
+
title: "Order update", // optional header
|
|
357
|
+
footer: "chatbridge", // optional footer
|
|
358
|
+
interactiveButtons: [
|
|
359
|
+
{
|
|
360
|
+
name: "cta_url",
|
|
361
|
+
buttonParamsJson: JSON.stringify({
|
|
362
|
+
display_text: "Track order",
|
|
363
|
+
url: "https://example.com/track"
|
|
364
|
+
})
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
name: "cta_copy",
|
|
368
|
+
buttonParamsJson: JSON.stringify({
|
|
369
|
+
display_text: "Copy code",
|
|
370
|
+
id: "ORDER-1234",
|
|
371
|
+
copy_code: "ORDER-1234"
|
|
372
|
+
})
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
name: "quick_reply",
|
|
376
|
+
buttonParamsJson: JSON.stringify({
|
|
377
|
+
display_text: "Thanks!",
|
|
378
|
+
id: "thanks"
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
]
|
|
383
382
|
}, { quoted: msg });
|
|
384
383
|
```
|
|
385
384
|
|
|
386
|
-
|
|
385
|
+
- `name` is required. `buttonParamsJson` can be a JSON string or a plain object (it is converted for you).
|
|
386
|
+
- Common button names: `cta_url` (open a link), `cta_copy` (copy a code or link), `cta_call` (call a number), `quick_reply` (send a reply back).
|
|
387
|
+
- `mentions` works on interactive messages as well.
|
|
388
|
+
|
|
389
|
+
Button rendering depends on the recipient's WhatsApp app version and account type, and WhatsApp may change or restrict these message types at any time. Test with your target accounts before relying on them in production.
|
|
390
|
+
|
|
391
|
+
---
|
|
387
392
|
|
|
388
|
-
|
|
389
|
-
|---|---|
|
|
390
|
-
| `header` | Title shown above the body |
|
|
391
|
-
| `title` | Body text |
|
|
392
|
-
| `footer` | Small text at the bottom |
|
|
393
|
-
| `image` | Header image: `{ url }` or a Buffer |
|
|
394
|
-
| `video` | Header video: `{ url }` or a Buffer |
|
|
395
|
-
| `document` | Header document. **Buffer only.** Use with `mimetype`, `fileName` and optionally `jpegThumbnail` |
|
|
396
|
-
| `buttons` | Array of `{ name, buttonParamsJson }` objects |
|
|
397
|
-
| `nativeFlowMessage` | Advanced: raw native flow payload (`buttons`, `messageParamsJson`) |
|
|
398
|
-
| `contextInfo` | Optional context such as `mentionedJid` |
|
|
393
|
+
## Mentions
|
|
399
394
|
|
|
400
|
-
|
|
395
|
+
To tag people, write `@number` in the text. Two ways to make it a real mention:
|
|
401
396
|
|
|
402
397
|
```js
|
|
398
|
+
// 1) explicit list (recommended): exactly these JIDs are mentioned
|
|
403
399
|
await sock.sendMessage(jid, {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
fileName: "invoice.pdf",
|
|
411
|
-
buttons: [
|
|
412
|
-
{
|
|
413
|
-
name: "cta_url",
|
|
414
|
-
buttonParamsJson: JSON.stringify({
|
|
415
|
-
display_text: "Pay online",
|
|
416
|
-
url: "https://example.com/pay"
|
|
417
|
-
})
|
|
418
|
-
}
|
|
419
|
-
]
|
|
420
|
-
}
|
|
421
|
-
}, { quoted: msg });
|
|
400
|
+
text: "Welcome @393331234567!",
|
|
401
|
+
mentions: ["393331234567@s.whatsapp.net"]
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// 2) automatic: without `mentions`, every @number (7-15 digits) in the text or caption is mentioned
|
|
405
|
+
await sock.sendMessage(jid, { text: "Welcome @393331234567!" });
|
|
422
406
|
```
|
|
423
407
|
|
|
424
|
-
|
|
408
|
+
- Use `autoMentions: false` to turn the automatic detection off.
|
|
409
|
+
- Your own `contextInfo` (for example `externalAdReply`) is merged with the mentions, never replaces them.
|
|
410
|
+
- WhatsApp shows the contact name only if the person is in the group or saved in the viewer's contacts. Someone who has just left a group is shown as a plain number.
|
|
425
411
|
|
|
426
412
|
---
|
|
427
413
|
|
package/lib/Socket/groups.js
CHANGED
|
@@ -21,7 +21,9 @@ const makeGroupsSocket = (config) => {
|
|
|
21
21
|
}));
|
|
22
22
|
const groupMetadata = async (jid) => {
|
|
23
23
|
const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
|
|
24
|
-
|
|
24
|
+
const metadata = (0, exports.extractGroupMetadata)(result);
|
|
25
|
+
try { sock.signalRepository?.lidMapping?.learnParticipants(metadata.participants); } catch (_e) { /* ignore */ }
|
|
26
|
+
return metadata;
|
|
25
27
|
};
|
|
26
28
|
const groupFetchAllParticipating = async () => {
|
|
27
29
|
const result = await query({
|
|
@@ -52,6 +54,7 @@ const makeGroupsSocket = (config) => {
|
|
|
52
54
|
attrs: {},
|
|
53
55
|
content: [groupNode]
|
|
54
56
|
});
|
|
57
|
+
try { sock.signalRepository?.lidMapping?.learnParticipants(meta.participants); } catch (_e) { /* ignore */ }
|
|
55
58
|
data[meta.id] = meta;
|
|
56
59
|
}
|
|
57
60
|
}
|
|
@@ -210,8 +210,10 @@ const makeMessagesRecvSocket = (config) => {
|
|
|
210
210
|
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;
|
|
211
211
|
if (retryCount === 1) {
|
|
212
212
|
//request a resend via phone
|
|
213
|
-
|
|
214
|
-
|
|
213
|
+
// non bloccante: requestPlaceholderResend attende 5s e rallentava ogni retry
|
|
214
|
+
requestPlaceholderResend(msgKey)
|
|
215
|
+
.then(resendId => logger.debug(`sendRetryRequest: requested placeholder resend for message ${resendId}`))
|
|
216
|
+
.catch(err => logger.debug({ err }, 'sendRetryRequest: placeholder resend fallito'));
|
|
215
217
|
}
|
|
216
218
|
const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);
|
|
217
219
|
await authState.keys.transaction(async () => {
|
|
@@ -793,6 +795,13 @@ const makeMessagesRecvSocket = (config) => {
|
|
|
793
795
|
placeholderResendCache.del(node.attrs.id);
|
|
794
796
|
}
|
|
795
797
|
}
|
|
798
|
+
try {
|
|
799
|
+
const lm = signalRepository.lidMapping;
|
|
800
|
+
if (lm) {
|
|
801
|
+
lm.learn(node.attrs.from, node.attrs.sender_pn);
|
|
802
|
+
lm.learn(node.attrs.participant, node.attrs.participant_pn);
|
|
803
|
+
}
|
|
804
|
+
} catch (_e) { /* ignore */ }
|
|
796
805
|
const { fullMessage: msg, category, author, decrypt } = (0, Utils_1.decryptMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, logger);
|
|
797
806
|
if (response && ((_a = msg === null || msg === void 0 ? void 0 : msg.messageStubParameters) === null || _a === void 0 ? void 0 : _a[0]) === Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT) {
|
|
798
807
|
msg.messageStubParameters = [Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT, response];
|
package/lib/Socket/socket.js
CHANGED
|
@@ -45,6 +45,8 @@ const makeSocket = (config) => {
|
|
|
45
45
|
// add transaction capability
|
|
46
46
|
const keys = (0, Utils_1.addTransactionCapability)(authState.keys, logger, transactionOpts);
|
|
47
47
|
const signalRepository = makeSignalRepository({ creds, keys });
|
|
48
|
+
// mappa LID <-> numero (usata dal bot per risolvere i @lid)
|
|
49
|
+
signalRepository.lidMapping = (0, Utils_1.makeLidMapping)(ev, keys, logger);
|
|
48
50
|
let lastDateRecv;
|
|
49
51
|
let epoch = 1;
|
|
50
52
|
let keepAliveReq;
|
|
@@ -91,10 +93,6 @@ const makeSocket = (config) => {
|
|
|
91
93
|
// ignore
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
|
-
// rate limit (429): lo segnaliamo soltanto; rallentare l'invio spetta al codice del bot
|
|
95
|
-
if (message.includes('429') || message.includes('rate limit')) {
|
|
96
|
-
logger.warn({ msg }, 'rate limit rilevato: riduci la frequenza di invio per evitare ban');
|
|
97
|
-
}
|
|
98
96
|
};
|
|
99
97
|
/** await the next incoming message */
|
|
100
98
|
const awaitNextMessage = async (sendMsg) => {
|
package/lib/Utils/index.js
CHANGED
|
@@ -21,6 +21,7 @@ __exportStar(require("./messages-media"), exports);
|
|
|
21
21
|
__exportStar(require("./validate-connection"), exports);
|
|
22
22
|
__exportStar(require("./crypto"), exports);
|
|
23
23
|
__exportStar(require("./signal"), exports);
|
|
24
|
+
__exportStar(require("./lid-mapping"), exports);
|
|
24
25
|
__exportStar(require("./noise-handler"), exports);
|
|
25
26
|
__exportStar(require("./history"), exports);
|
|
26
27
|
__exportStar(require("./chat-utils"), exports);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeLidMapping = void 0;
|
|
4
|
+
// Mappa LID <-> numero di telefono (PN).
|
|
5
|
+
// Sta in memoria e viene salvata nelle chiavi di autenticazione come 'lid-mapping'
|
|
6
|
+
// (file lid-mapping-*.json con useMultiFileAuthState). Emette l'evento 'lid-mapping.update'.
|
|
7
|
+
const userOf = (jid) => String(jid || '').split('@')[0].split(':')[0];
|
|
8
|
+
const serverOf = (jid) => String(jid || '').split('@')[1] || '';
|
|
9
|
+
const isLid = (jid) => serverOf(jid) === 'lid';
|
|
10
|
+
const isPn = (jid) => serverOf(jid) === 's.whatsapp.net';
|
|
11
|
+
const makeLidMapping = (ev, keys, logger) => {
|
|
12
|
+
const lidToPn = new Map();
|
|
13
|
+
const pnToLid = new Map();
|
|
14
|
+
const persist = async (lidUser, pnUser) => {
|
|
15
|
+
if (!keys || typeof keys.set !== 'function') return;
|
|
16
|
+
try {
|
|
17
|
+
await keys.set({ 'lid-mapping': { [pnUser]: lidUser, [`${lidUser}_reverse`]: pnUser } });
|
|
18
|
+
} catch (err) {
|
|
19
|
+
logger?.debug?.({ err }, 'lid-mapping: salvataggio non riuscito');
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
// impara una coppia; ritorna true se è nuova
|
|
23
|
+
const learn = (a, b) => {
|
|
24
|
+
let lid, pn;
|
|
25
|
+
if (isLid(a) && isPn(b)) { lid = a; pn = b; }
|
|
26
|
+
else if (isLid(b) && isPn(a)) { lid = b; pn = a; }
|
|
27
|
+
else return false;
|
|
28
|
+
const lidUser = userOf(lid);
|
|
29
|
+
const pnUser = userOf(pn);
|
|
30
|
+
if (!lidUser || !pnUser || lidUser === pnUser) return false;
|
|
31
|
+
if (lidToPn.get(lidUser) === pnUser) return false;
|
|
32
|
+
lidToPn.set(lidUser, pnUser);
|
|
33
|
+
pnToLid.set(pnUser, lidUser);
|
|
34
|
+
persist(lidUser, pnUser);
|
|
35
|
+
try {
|
|
36
|
+
ev.emit('lid-mapping.update', { lid: `${lidUser}@lid`, pn: `${pnUser}@s.whatsapp.net` });
|
|
37
|
+
} catch (_e) { /* ignore */ }
|
|
38
|
+
return true;
|
|
39
|
+
};
|
|
40
|
+
const loadFromKeys = async (type, id) => {
|
|
41
|
+
if (!keys || typeof keys.get !== 'function') return undefined;
|
|
42
|
+
try {
|
|
43
|
+
const res = await keys.get('lid-mapping', [id]);
|
|
44
|
+
return res?.[id];
|
|
45
|
+
} catch (_e) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
learn,
|
|
51
|
+
async storeLIDPNMappings(pairs) {
|
|
52
|
+
for (const p of pairs || []) learn(p.lid, p.pn || p.jid || p.phoneNumber);
|
|
53
|
+
},
|
|
54
|
+
async getPNForLID(lid) {
|
|
55
|
+
if (!isLid(lid)) return null;
|
|
56
|
+
const lidUser = userOf(lid);
|
|
57
|
+
let pnUser = lidToPn.get(lidUser);
|
|
58
|
+
if (!pnUser) {
|
|
59
|
+
pnUser = await loadFromKeys('lid-mapping', `${lidUser}_reverse`);
|
|
60
|
+
if (pnUser) { lidToPn.set(lidUser, pnUser); pnToLid.set(pnUser, lidUser); }
|
|
61
|
+
}
|
|
62
|
+
return pnUser ? `${pnUser}@s.whatsapp.net` : null;
|
|
63
|
+
},
|
|
64
|
+
async getLIDForPN(pn) {
|
|
65
|
+
if (!isPn(pn)) return null;
|
|
66
|
+
const pnUser = userOf(pn);
|
|
67
|
+
let lidUser = pnToLid.get(pnUser);
|
|
68
|
+
if (!lidUser) {
|
|
69
|
+
lidUser = await loadFromKeys('lid-mapping', pnUser);
|
|
70
|
+
if (lidUser) { pnToLid.set(pnUser, lidUser); lidToPn.set(lidUser, pnUser); }
|
|
71
|
+
}
|
|
72
|
+
return lidUser ? `${lidUser}@lid` : null;
|
|
73
|
+
},
|
|
74
|
+
async getLIDsForPNs(pns) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const pn of pns || []) {
|
|
77
|
+
const lid = await this.getLIDForPN(pn);
|
|
78
|
+
if (lid) out.push({ pn, lid });
|
|
79
|
+
}
|
|
80
|
+
return out.length ? out : null;
|
|
81
|
+
},
|
|
82
|
+
// coppie da participant / messaggi: { id: lid, jid: pn }
|
|
83
|
+
learnParticipants(list) {
|
|
84
|
+
for (const p of list || []) learn(p?.id, p?.jid);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
exports.makeLidMapping = makeLidMapping;
|
|
@@ -38,6 +38,29 @@ const WABinary_1 = require("../WABinary");
|
|
|
38
38
|
const crypto_1 = require("./crypto");
|
|
39
39
|
const generics_1 = require("./generics");
|
|
40
40
|
const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
41
|
+
const JIMP_AUTO = -1;
|
|
42
|
+
/** rende l'API di jimp 1.x identica a quella di jimp 0.x usata dal resto del codice */
|
|
43
|
+
const wrapJimpV1 = (img) => {
|
|
44
|
+
let q = 50;
|
|
45
|
+
const api = {
|
|
46
|
+
getWidth: () => img.width,
|
|
47
|
+
getHeight: () => img.height,
|
|
48
|
+
quality(v) { q = v; return api; },
|
|
49
|
+
resize(w, h) {
|
|
50
|
+
img.resize((h === undefined || h === JIMP_AUTO) ? { w } : { w, h });
|
|
51
|
+
return api;
|
|
52
|
+
},
|
|
53
|
+
crop(x, y, w, h) { img.crop({ x, y, w, h }); return api; },
|
|
54
|
+
getBufferAsync(mime) { return img.getBuffer(mime, { quality: q }); }
|
|
55
|
+
};
|
|
56
|
+
return api;
|
|
57
|
+
};
|
|
58
|
+
const jimpV1Adapter = (Jimp) => ({
|
|
59
|
+
read: async (input) => wrapJimpV1(await Jimp.read(input)),
|
|
60
|
+
MIME_JPEG: 'image/jpeg',
|
|
61
|
+
RESIZE_BILINEAR: undefined,
|
|
62
|
+
AUTO: JIMP_AUTO
|
|
63
|
+
});
|
|
41
64
|
const getImageProcessingLibrary = async () => {
|
|
42
65
|
const [_jimp, sharp] = await Promise.all([
|
|
43
66
|
(async () => {
|
|
@@ -55,9 +78,15 @@ const getImageProcessingLibrary = async () => {
|
|
|
55
78
|
return { sharp };
|
|
56
79
|
}
|
|
57
80
|
const jimp = (_jimp === null || _jimp === void 0 ? void 0 : _jimp.default) || _jimp;
|
|
58
|
-
|
|
81
|
+
// jimp 0.x: l'oggetto ha direttamente il metodo read()
|
|
82
|
+
if (jimp && typeof jimp.read === 'function') {
|
|
59
83
|
return { jimp };
|
|
60
84
|
}
|
|
85
|
+
// jimp 1.x: la classe e' esportata come { Jimp } e ha un'API diversa -> adattatore
|
|
86
|
+
const jimpV1 = (_jimp && _jimp.Jimp) || (jimp && jimp.Jimp);
|
|
87
|
+
if (jimpV1 && typeof jimpV1.read === 'function') {
|
|
88
|
+
return { jimp: jimpV1Adapter(jimpV1) };
|
|
89
|
+
}
|
|
61
90
|
throw new boom_1.Boom('No image processing library available');
|
|
62
91
|
};
|
|
63
92
|
const hkdfInfoKey = (type) => {
|
package/lib/Utils/messages.js
CHANGED
|
@@ -250,6 +250,115 @@ const generateForwardMessageContent = (message, forceForward) => {
|
|
|
250
250
|
return content;
|
|
251
251
|
};
|
|
252
252
|
exports.generateForwardMessageContent = generateForwardMessageContent;
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
// Menzioni e pulsanti interattivi (chatbridge)
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
const WRAPPER_KEYS = ['viewOnceMessage', 'viewOnceMessageV2', 'viewOnceMessageV2Extension', 'ephemeralMessage', 'documentWithCaptionMessage'];
|
|
257
|
+
/** scende dentro gli involucri (viewOnce, effimero...) fino al contenuto reale del messaggio */
|
|
258
|
+
const getInnerContent = (m) => {
|
|
259
|
+
let cur = m;
|
|
260
|
+
for (let i = 0; i < 4; i++) {
|
|
261
|
+
const key = Object.keys(cur)[0];
|
|
262
|
+
const val = cur[key];
|
|
263
|
+
if (WRAPPER_KEYS.includes(key) && val && val.message) {
|
|
264
|
+
cur = val.message;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
return (val && typeof val === 'object') ? val : undefined;
|
|
268
|
+
}
|
|
269
|
+
return undefined;
|
|
270
|
+
};
|
|
271
|
+
const uniqueList = (arr) => [...new Set(arr)];
|
|
272
|
+
/** accetta "3939...@s.whatsapp.net", "@lid" oppure un semplice numero */
|
|
273
|
+
const normalizeMentionJid = (j) => {
|
|
274
|
+
if (typeof j !== 'string') {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
j = j.trim();
|
|
278
|
+
if (!j) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
if (!j.includes('@')) {
|
|
282
|
+
const digits = j.replace(/\D/g, '');
|
|
283
|
+
return digits ? `${digits}@s.whatsapp.net` : null;
|
|
284
|
+
}
|
|
285
|
+
return j;
|
|
286
|
+
};
|
|
287
|
+
const MENTION_REGEX = /(?<![\w.])@(\d{7,15})(?!\d)/g;
|
|
288
|
+
/**
|
|
289
|
+
* Elenco delle menzioni del messaggio.
|
|
290
|
+
* - se passi `mentions`, vengono usate esattamente quelle (ripulite e senza doppioni)
|
|
291
|
+
* - se non le passi, i numeri scritti come @39333... nel testo diventano menzioni automatiche
|
|
292
|
+
* (disattivabile con `autoMentions: false`)
|
|
293
|
+
*/
|
|
294
|
+
const resolveMentions = (message) => {
|
|
295
|
+
if (Array.isArray(message.mentions) && message.mentions.length) {
|
|
296
|
+
return uniqueList(message.mentions.map(normalizeMentionJid).filter(Boolean));
|
|
297
|
+
}
|
|
298
|
+
if (message.autoMentions === false) {
|
|
299
|
+
return [];
|
|
300
|
+
}
|
|
301
|
+
const found = [];
|
|
302
|
+
for (const text of [message.text, message.caption]) {
|
|
303
|
+
if (typeof text === 'string') {
|
|
304
|
+
for (const hit of text.matchAll(MENTION_REGEX)) {
|
|
305
|
+
found.push(`${hit[1]}@s.whatsapp.net`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return uniqueList(found);
|
|
310
|
+
};
|
|
311
|
+
/** unisce `extra` al contextInfo esistente, senza mai perdere le menzioni gia presenti */
|
|
312
|
+
const mergeContextInfo = (m, extra) => {
|
|
313
|
+
const content = getInnerContent(m);
|
|
314
|
+
if (!content) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const current = (content.contextInfo && typeof content.contextInfo === 'object') ? content.contextInfo : {};
|
|
318
|
+
const merged = { ...current, ...extra };
|
|
319
|
+
const a = current.mentionedJid;
|
|
320
|
+
const b = extra.mentionedJid;
|
|
321
|
+
if ((a && a.length) || (b && b.length)) {
|
|
322
|
+
merged.mentionedJid = uniqueList([...(a || []), ...(b || [])]);
|
|
323
|
+
}
|
|
324
|
+
content.contextInfo = merged;
|
|
325
|
+
};
|
|
326
|
+
/** pulsante interattivo: buttonParamsJson puo essere una stringa JSON oppure un oggetto */
|
|
327
|
+
const normalizeInteractiveButton = (b) => {
|
|
328
|
+
if (!b || typeof b !== 'object' || !b.name) {
|
|
329
|
+
throw new boom_1.Boom('Invalid interactive button: "name" is required', { statusCode: 400 });
|
|
330
|
+
}
|
|
331
|
+
let params = b.buttonParamsJson;
|
|
332
|
+
if (params !== undefined && typeof params !== 'string') {
|
|
333
|
+
params = JSON.stringify(params);
|
|
334
|
+
}
|
|
335
|
+
return { name: b.name, buttonParamsJson: params === undefined ? '{}' : params };
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* Righe e sezioni di una lista.
|
|
339
|
+
* Accetta sia `rowId` (formato classico) sia `id` (formato delle liste native):
|
|
340
|
+
* se manca l'id viene usato il titolo, cosi la riga e sempre selezionabile.
|
|
341
|
+
*/
|
|
342
|
+
const normalizeListSections = (sections) => {
|
|
343
|
+
if (!Array.isArray(sections) || !sections.length) {
|
|
344
|
+
throw new boom_1.Boom('Invalid list: "sections" must be a non-empty array', { statusCode: 400 });
|
|
345
|
+
}
|
|
346
|
+
return sections.map((section) => {
|
|
347
|
+
const rows = (section && Array.isArray(section.rows) ? section.rows : []).map((row) => {
|
|
348
|
+
const title = row && row.title !== undefined && row.title !== null ? String(row.title) : '';
|
|
349
|
+
const rawId = row && row.rowId !== undefined && row.rowId !== null ? row.rowId : (row ? row.id : undefined);
|
|
350
|
+
const out = { title, rowId: String(rawId !== undefined && rawId !== null && rawId !== '' ? rawId : title) };
|
|
351
|
+
if (row && row.description !== undefined && row.description !== null && row.description !== '') {
|
|
352
|
+
out.description = String(row.description);
|
|
353
|
+
}
|
|
354
|
+
return out;
|
|
355
|
+
});
|
|
356
|
+
if (!rows.length) {
|
|
357
|
+
throw new boom_1.Boom('Invalid list: every section needs at least one row', { statusCode: 400 });
|
|
358
|
+
}
|
|
359
|
+
return { title: section.title !== undefined && section.title !== null ? String(section.title) : '', rows };
|
|
360
|
+
});
|
|
361
|
+
};
|
|
253
362
|
const generateWAMessageContent = async (message, options) => {
|
|
254
363
|
var _a;
|
|
255
364
|
var _b;
|
|
@@ -313,6 +422,19 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
313
422
|
type: Types_1.WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
314
423
|
};
|
|
315
424
|
}
|
|
425
|
+
else if ('pin' in message && message.pin) {
|
|
426
|
+
const pinKey = message.pin.key || message.pin;
|
|
427
|
+
const pinType = message.pin.key ? message.pin.type : message.type;
|
|
428
|
+
const pinTime = message.pin.key ? message.pin.time : message.time;
|
|
429
|
+
m.pinInChatMessage = {
|
|
430
|
+
key: pinKey,
|
|
431
|
+
type: pinType,
|
|
432
|
+
senderTimestampMs: Date.now()
|
|
433
|
+
};
|
|
434
|
+
m.messageContextInfo = {
|
|
435
|
+
messageAddOnDurationInSecs: pinType === 1 ? (pinTime || 86400) : 0
|
|
436
|
+
};
|
|
437
|
+
}
|
|
316
438
|
else if ('forward' in message) {
|
|
317
439
|
m = (0, exports.generateForwardMessageContent)(message.forward, message.force);
|
|
318
440
|
}
|
|
@@ -427,10 +549,32 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
427
549
|
}
|
|
428
550
|
};
|
|
429
551
|
}
|
|
552
|
+
if ('interactiveButtons' in message && Array.isArray(message.interactiveButtons) && message.interactiveButtons.length) {
|
|
553
|
+
const interactiveMessage = {
|
|
554
|
+
nativeFlowMessage: {
|
|
555
|
+
buttons: message.interactiveButtons.map(normalizeInteractiveButton)
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
if (message.title || message.subtitle) {
|
|
559
|
+
interactiveMessage.header = {
|
|
560
|
+
title: message.title || '',
|
|
561
|
+
subtitle: message.subtitle || '',
|
|
562
|
+
hasMediaAttachment: false
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
const bodyText = message.text !== undefined ? message.text : message.caption;
|
|
566
|
+
if (bodyText) {
|
|
567
|
+
interactiveMessage.body = { text: bodyText };
|
|
568
|
+
}
|
|
569
|
+
if (message.footer) {
|
|
570
|
+
interactiveMessage.footer = { text: message.footer };
|
|
571
|
+
}
|
|
572
|
+
m = { interactiveMessage };
|
|
573
|
+
}
|
|
430
574
|
if ('sections' in message && !!message.sections) {
|
|
431
575
|
const listMessage = {
|
|
432
|
-
sections: message.sections,
|
|
433
|
-
buttonText: message.buttonText,
|
|
576
|
+
sections: normalizeListSections(message.sections),
|
|
577
|
+
buttonText: message.buttonText || 'Select',
|
|
434
578
|
title: message.title,
|
|
435
579
|
footerText: message.footer,
|
|
436
580
|
description: message.text,
|
|
@@ -441,10 +585,11 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
441
585
|
if ('viewOnce' in message && !!message.viewOnce) {
|
|
442
586
|
m = { viewOnceMessage: { message: m } };
|
|
443
587
|
}
|
|
444
|
-
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
588
|
+
{
|
|
589
|
+
const mentioned = resolveMentions(message);
|
|
590
|
+
if (mentioned.length) {
|
|
591
|
+
mergeContextInfo(m, { mentionedJid: mentioned });
|
|
592
|
+
}
|
|
448
593
|
}
|
|
449
594
|
if ('edit' in message) {
|
|
450
595
|
m = {
|
|
@@ -457,9 +602,7 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
457
602
|
};
|
|
458
603
|
}
|
|
459
604
|
if ('contextInfo' in message && !!message.contextInfo) {
|
|
460
|
-
|
|
461
|
-
m[messageType] = m[messageType] || {};
|
|
462
|
-
m[messageType].contextInfo = message.contextInfo;
|
|
605
|
+
mergeContextInfo(m, message.contextInfo);
|
|
463
606
|
}
|
|
464
607
|
return Types_1.WAProto.Message.fromObject(m);
|
|
465
608
|
};
|
|
@@ -147,6 +147,19 @@ const getAdditionalNode = (name) => {
|
|
|
147
147
|
},
|
|
148
148
|
content: []
|
|
149
149
|
}]
|
|
150
|
+
} else if (name === 'list') {
|
|
151
|
+
// lista classica (listMessage): serve il nodo <list type="product_list" v="2"/>
|
|
152
|
+
return [{
|
|
153
|
+
tag: 'biz',
|
|
154
|
+
attrs: {},
|
|
155
|
+
content: [{
|
|
156
|
+
tag: 'list',
|
|
157
|
+
attrs: {
|
|
158
|
+
v: '2',
|
|
159
|
+
type: 'product_list'
|
|
160
|
+
}
|
|
161
|
+
}]
|
|
162
|
+
}]
|
|
150
163
|
} else if (flow_name[name] || name === 'interactive' || name === 'buttons') {
|
|
151
164
|
return [{
|
|
152
165
|
tag: 'biz',
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatbridge-it/baileys",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "WhatsApp Baileys multifunction by ChatBridge",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chatbridge",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"audio-decode": "^2.1.3",
|
|
37
37
|
"axios": "^1.3.3",
|
|
38
38
|
"cache-manager": "4.0.1",
|
|
39
|
+
"fluent-ffmpeg": "^2.1.2",
|
|
39
40
|
"futoin-hkdf": "^1.5.1",
|
|
40
41
|
"libphonenumber-js": "^1.10.20",
|
|
41
42
|
"libsignal": "^6.0.0",
|
|
@@ -71,10 +72,10 @@
|
|
|
71
72
|
"typescript": "^4.6.4"
|
|
72
73
|
},
|
|
73
74
|
"peerDependencies": {
|
|
74
|
-
"jimp": "
|
|
75
|
-
"link-preview-js": "
|
|
76
|
-
"qrcode-terminal": "
|
|
77
|
-
"sharp": "
|
|
75
|
+
"jimp": ">=0.16.1",
|
|
76
|
+
"link-preview-js": ">=3.0.0",
|
|
77
|
+
"qrcode-terminal": ">=0.12.0",
|
|
78
|
+
"sharp": ">=0.32.2"
|
|
78
79
|
},
|
|
79
80
|
"peerDependenciesMeta": {
|
|
80
81
|
"jimp": {
|