@badzz88/baileys 8.5.5 → 8.5.6
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 +2 -2
- package/WAProto/WAProto.proto +1256 -75
- package/WAProto/fix-imports.js +69 -79
- package/WAProto/index.d.ts +15254 -71491
- package/WAProto/index.js +19816 -126438
- package/engine-requirements.js +3 -4
- package/package.json +4 -4
- package/src/Defaults/index.js +186 -0
- package/src/Signal/Group/ciphertext-message.js +15 -0
- package/src/Signal/Group/group-session-builder.js +86 -0
- package/src/Signal/Group/group_cipher.js +82 -0
- package/src/Signal/Group/index.js +140 -0
- package/src/Signal/Group/keyhelper.js +77 -0
- package/src/Signal/Group/sender-chain-key.js +29 -0
- package/src/Signal/Group/sender-key-distribution-message.js +66 -0
- package/src/Signal/Group/sender-key-message.js +69 -0
- package/src/Signal/Group/sender-key-name.js +51 -0
- package/src/Signal/Group/sender-key-record.js +44 -0
- package/src/Signal/Group/sender-key-state.js +87 -0
- package/src/Signal/Group/sender-message-key.js +29 -0
- package/src/Signal/libsignal.js +455 -0
- package/src/Signal/lid-mapping.js +273 -0
- package/src/Socket/Client/index.js +31 -0
- package/src/Socket/Client/types.js +13 -0
- package/src/Socket/Client/websocket.js +61 -0
- package/src/Socket/aigroups.js +221 -0
- package/src/Socket/business.js +411 -0
- package/src/Socket/chats.js +1449 -0
- package/src/Socket/communities.js +463 -0
- package/src/Socket/graphql.js +523 -0
- package/src/Socket/groups.js +516 -0
- package/src/Socket/index.js +31 -0
- package/src/Socket/interactive-handler.js +527 -0
- package/src/Socket/interop.js +339 -0
- package/{lib → src}/Socket/luxu.js +82 -120
- package/src/Socket/managed-account.js +98 -0
- package/src/Socket/messages-recv.js +2685 -0
- package/src/Socket/messages-send.js +2047 -0
- package/src/Socket/mex.js +57 -0
- package/src/Socket/newsletter.js +455 -0
- package/src/Socket/privacy.js +125 -0
- package/src/Socket/registration.js +236 -0
- package/src/Socket/socket.js +983 -0
- package/src/Socket/text-router.js +65 -0
- package/src/Socket/username.js +130 -0
- package/src/Store/index.js +33 -0
- package/{lib → src}/Store/keyed-db.js +21 -11
- package/src/Store/make-cache-manager-store.js +84 -0
- package/src/Store/make-in-memory-store.js +551 -0
- package/src/Store/make-ordered-dictionary.js +81 -0
- package/src/Store/object-repository.js +26 -0
- package/src/Types/Auth.js +31 -0
- package/src/Types/Bussines.js +2 -0
- package/src/Types/Call.js +2 -0
- package/src/Types/Chat.js +4 -0
- package/src/Types/Contact.js +2 -0
- package/src/Types/Events.js +2 -0
- package/src/Types/GroupMetadata.js +2 -0
- package/src/Types/Label.js +26 -0
- package/src/Types/LabelAssociation.js +8 -0
- package/src/Types/Message.js +93 -0
- package/src/Types/Mex.js +112 -0
- package/src/Types/Newsletter.js +109 -0
- package/src/Types/Product.js +2 -0
- package/src/Types/Signal.js +2 -0
- package/src/Types/Socket.js +2 -0
- package/src/Types/State.js +62 -0
- package/src/Types/USync.js +2 -0
- package/src/Types/index.js +56 -0
- package/src/Utils/auth-utils.js +254 -0
- package/src/Utils/browser-utils.js +112 -0
- package/src/Utils/business.js +240 -0
- package/src/Utils/chat-utils.js +1230 -0
- package/src/Utils/command-loader.d.ts +27 -0
- package/src/Utils/command-loader.js +89 -0
- package/src/Utils/companion-reg-client-utils.js +40 -0
- package/src/Utils/consumer-application.js +105 -0
- package/src/Utils/crypto.js +118 -0
- package/src/Utils/curve25519-js.js +242 -0
- package/src/Utils/decode-wa-message.js +529 -0
- package/src/Utils/event-buffer.js +580 -0
- package/src/Utils/generics.js +483 -0
- package/src/Utils/group-history.js +44 -0
- package/src/Utils/history.js +232 -0
- package/src/Utils/identity-change-handler.js +52 -0
- package/src/Utils/index.js +58 -0
- package/src/Utils/jid-display-normalization.js +195 -0
- package/src/Utils/link-preview.js +135 -0
- package/src/Utils/logger.js +8 -0
- package/src/Utils/lt-hash.js +25 -0
- package/src/Utils/make-mutex.js +36 -0
- package/src/Utils/message-composer.js +471 -0
- package/src/Utils/message-retry-manager.js +217 -0
- package/src/Utils/messages-media.js +843 -0
- package/src/Utils/messages.js +2817 -0
- package/src/Utils/meta-ai-msmsg.js +222 -0
- package/src/Utils/native-bridge.js +68 -0
- package/src/Utils/noise-handler.js +169 -0
- package/src/Utils/offline-node-processor.js +34 -0
- package/src/Utils/pre-key-manager.js +90 -0
- package/src/Utils/process-message.js +950 -0
- package/src/Utils/reporting-utils.js +261 -0
- package/src/Utils/session-pool.d.ts +14 -0
- package/src/Utils/session-pool.js +70 -0
- package/src/Utils/signal.js +217 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/sticker.d.ts +13 -0
- package/src/Utils/sticker.js +123 -0
- package/src/Utils/sync-action-utils.js +45 -0
- package/src/Utils/tc-token-utils.js +158 -0
- package/src/Utils/use-multi-file-auth-state.js +111 -0
- package/src/Utils/validate-connection.js +209 -0
- package/src/Utils/view-once-cache.d.ts +12 -0
- package/src/Utils/view-once-cache.js +63 -0
- package/src/Utils/voip-rekey.js +22 -0
- package/src/WABinary/constants.js +1304 -0
- package/src/WABinary/decode.js +342 -0
- package/{lib → src}/WABinary/encode.js +9 -12
- package/src/WABinary/generic-utils.js +238 -0
- package/src/WABinary/index.js +34 -0
- package/src/WABinary/jid-utils.js +351 -0
- package/src/WABinary/types.js +2 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/constants.js +39484 -0
- package/src/WAM/encode.js +149 -0
- package/src/WAM/index.js +32 -0
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.js +40 -0
- package/src/WAUSync/USyncQuery.js +126 -0
- package/src/WAUSync/USyncUser.js +50 -0
- package/src/WAUSync/index.js +32 -0
- package/src/antiban.js +4152 -0
- package/{lib → src}/index.js +51 -49
- package/lib/Defaults/index.js +0 -187
- package/lib/Signal/Group/ciphertext-message.js +0 -15
- package/lib/Signal/Group/group-session-builder.js +0 -92
- package/lib/Signal/Group/group_cipher.js +0 -89
- package/lib/Signal/Group/index.js +0 -136
- package/lib/Signal/Group/keyhelper.js +0 -73
- package/lib/Signal/Group/sender-chain-key.js +0 -32
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
- package/lib/Signal/Group/sender-key-message.js +0 -69
- package/lib/Signal/Group/sender-key-name.js +0 -50
- package/lib/Signal/Group/sender-key-record.js +0 -44
- package/lib/Signal/Group/sender-key-state.js +0 -97
- package/lib/Signal/Group/sender-message-key.js +0 -30
- package/lib/Signal/libsignal.js +0 -459
- package/lib/Signal/lid-mapping.js +0 -260
- package/lib/Socket/Client/index.js +0 -30
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/aigroups.js +0 -240
- package/lib/Socket/business.js +0 -422
- package/lib/Socket/chats.js +0 -1816
- package/lib/Socket/communities.js +0 -549
- package/lib/Socket/graphql.js +0 -656
- package/lib/Socket/groups.js +0 -764
- package/lib/Socket/index.js +0 -39
- package/lib/Socket/interactive-handler.js +0 -522
- package/lib/Socket/interop.js +0 -549
- package/lib/Socket/managed-account.js +0 -183
- package/lib/Socket/messages-recv.js +0 -2442
- package/lib/Socket/messages-send.js +0 -1853
- package/lib/Socket/mex.js +0 -60
- package/lib/Socket/newsletter.js +0 -818
- package/lib/Socket/privacy.js +0 -449
- package/lib/Socket/registration.js +0 -427
- package/lib/Socket/socket.js +0 -1092
- package/lib/Socket/text-router.js +0 -83
- package/lib/Socket/username.js +0 -243
- package/lib/Store/index.js +0 -36
- package/lib/Store/make-cache-manager-store.js +0 -90
- package/lib/Store/make-in-memory-store.js +0 -604
- package/lib/Store/make-ordered-dictionary.js +0 -81
- package/lib/Store/object-repository.js +0 -29
- package/lib/Types/Auth.js +0 -38
- package/lib/Types/Bussines.js +0 -2
- package/lib/Types/Call.js +0 -2
- package/lib/Types/Chat.js +0 -4
- package/lib/Types/Contact.js +0 -2
- package/lib/Types/Events.js +0 -2
- package/lib/Types/GroupMetadata.js +0 -2
- package/lib/Types/Label.js +0 -27
- package/lib/Types/LabelAssociation.js +0 -9
- package/lib/Types/Message.js +0 -95
- package/lib/Types/Mex.js +0 -112
- package/lib/Types/Newsletter.js +0 -121
- package/lib/Types/Product.js +0 -2
- package/lib/Types/Signal.js +0 -2
- package/lib/Types/Socket.js +0 -2
- package/lib/Types/State.js +0 -70
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.js +0 -55
- package/lib/Utils/auth-utils.js +0 -306
- package/lib/Utils/browser-utils.js +0 -114
- package/lib/Utils/business.js +0 -247
- package/lib/Utils/chat-utils.js +0 -1208
- package/lib/Utils/command-loader.d.ts +0 -31
- package/lib/Utils/command-loader.js +0 -100
- package/lib/Utils/companion-reg-client-utils.js +0 -42
- package/lib/Utils/consumer-application.js +0 -107
- package/lib/Utils/crypto.js +0 -155
- package/lib/Utils/curve25519-js.js +0 -275
- package/lib/Utils/decode-wa-message.js +0 -560
- package/lib/Utils/event-buffer.js +0 -607
- package/lib/Utils/generics.js +0 -563
- package/lib/Utils/group-history.js +0 -60
- package/lib/Utils/history.js +0 -244
- package/lib/Utils/identity-change-handler.js +0 -52
- package/lib/Utils/index.js +0 -57
- package/lib/Utils/jid-display-normalization.js +0 -218
- package/lib/Utils/link-preview.js +0 -143
- package/lib/Utils/logger.js +0 -9
- package/lib/Utils/lt-hash.js +0 -39
- package/lib/Utils/make-mutex.js +0 -36
- package/lib/Utils/message-composer.js +0 -479
- package/lib/Utils/message-inspect.js +0 -393
- package/lib/Utils/message-retry-manager.js +0 -270
- package/lib/Utils/messages-media.js +0 -896
- package/lib/Utils/messages.js +0 -2904
- package/lib/Utils/meta-ai-msmsg.js +0 -262
- package/lib/Utils/native-bridge.js +0 -82
- package/lib/Utils/noise-handler.js +0 -196
- package/lib/Utils/offline-node-processor.js +0 -42
- package/lib/Utils/pre-key-manager.js +0 -107
- package/lib/Utils/process-message.js +0 -1048
- package/lib/Utils/reporting-utils.js +0 -262
- package/lib/Utils/session-pool.d.ts +0 -16
- package/lib/Utils/session-pool.js +0 -94
- package/lib/Utils/signal.js +0 -224
- package/lib/Utils/stanza-ack.js +0 -40
- package/lib/Utils/sticker.d.ts +0 -17
- package/lib/Utils/sticker.js +0 -145
- package/lib/Utils/sync-action-utils.js +0 -54
- package/lib/Utils/tc-token-utils.js +0 -161
- package/lib/Utils/use-multi-file-auth-state.js +0 -121
- package/lib/Utils/validate-connection.js +0 -219
- package/lib/Utils/view-once-cache.d.ts +0 -9
- package/lib/Utils/view-once-cache.js +0 -79
- package/lib/Utils/voip-rekey.js +0 -22
- package/lib/WABinary/constants.js +0 -1304
- package/lib/WABinary/decode.js +0 -343
- package/lib/WABinary/generic-utils.js +0 -240
- package/lib/WABinary/index.js +0 -33
- package/lib/WABinary/jid-utils.js +0 -361
- package/lib/WABinary/types.js +0 -2
- package/lib/WAM/BinaryInfo.js +0 -13
- package/lib/WAM/constants.js +0 -39486
- package/lib/WAM/encode.js +0 -142
- package/lib/WAM/index.js +0 -31
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -43
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -54
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -57
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -30
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -59
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
- package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -39
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -27
- package/lib/WAUSync/Protocols/index.js +0 -39
- package/lib/WAUSync/USyncBackoff.js +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -133
- package/lib/WAUSync/USyncUser.js +0 -50
- package/lib/WAUSync/index.js +0 -31
- package/lib/antiban.js +0 -4637
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
|
@@ -0,0 +1,2047 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) ||
|
|
3
|
+
function (mod) {
|
|
4
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
7
|
+
exports.makeMessagesSocket = void 0;
|
|
8
|
+
const node_cache_1 = __importDefault(require('@cacheable/node-cache'));
|
|
9
|
+
const boom_1 = require('@hapi/boom');
|
|
10
|
+
const index_js_1 = require('../../WAProto/index.js');
|
|
11
|
+
const Defaults_1 = require('../Defaults');
|
|
12
|
+
const Utils_1 = require('../Utils');
|
|
13
|
+
const link_preview_1 = require('../Utils/link-preview');
|
|
14
|
+
const make_mutex_1 = require('../Utils/make-mutex');
|
|
15
|
+
const reporting_utils_1 = require('../Utils/reporting-utils');
|
|
16
|
+
const tc_token_utils_1 = require('../Utils/tc-token-utils');
|
|
17
|
+
const jid_display_normalization_1 = require('../Utils/jid-display-normalization');
|
|
18
|
+
const WABinary_1 = require('../WABinary');
|
|
19
|
+
const WAUSync_1 = require('../WAUSync');
|
|
20
|
+
const message_composer_1 = require('../Utils/message-composer.js');
|
|
21
|
+
const interactive_handler_1 = require('./interactive-handler.js');
|
|
22
|
+
const username_1 = require('./username');
|
|
23
|
+
const { setBotMessageSecret } = require('../Utils/decode-wa-message');
|
|
24
|
+
const makeMessagesSocket = config => {
|
|
25
|
+
const { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, options: httpRequestOptions, patchMessageBeforeSending, cachedGroupMetadata, enableRecentMessageCache, maxMsgRetryCount } = config;
|
|
26
|
+
const sock = (0, username_1.makeUsernameSocket)(config);
|
|
27
|
+
const { ev, authState, messageMutex, signalRepository, upsertMessage, query, fetchPrivacySettings, sendNode, groupMetadata, groupToggleEphemeral, trustInteropContact } = sock;
|
|
28
|
+
const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping);
|
|
29
|
+
const resolveStatusAudience = async (groupJids, includeMe = false) => {
|
|
30
|
+
const myJid = authState.creds.me?.id;
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
const allUsers = [];
|
|
33
|
+
for (const gjid of groupJids) {
|
|
34
|
+
let meta;
|
|
35
|
+
try {
|
|
36
|
+
meta = cachedGroupMetadata ? await cachedGroupMetadata(gjid) : null;
|
|
37
|
+
if (!meta)
|
|
38
|
+
meta = await groupMetadata(gjid);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
for (const p of meta?.participants || []) {
|
|
44
|
+
const id = p.id;
|
|
45
|
+
if (!id || seen.has(id))
|
|
46
|
+
continue;
|
|
47
|
+
if (!includeMe && id === myJid)
|
|
48
|
+
continue;
|
|
49
|
+
seen.add(id);
|
|
50
|
+
allUsers.push(id);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { allUsers };
|
|
54
|
+
};
|
|
55
|
+
const inFlightTcTokenIssuance = new Set();
|
|
56
|
+
const userDevicesCache = config.userDevicesCache ||
|
|
57
|
+
new node_cache_1.default({
|
|
58
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.USER_DEVICES,
|
|
59
|
+
useClones: false
|
|
60
|
+
});
|
|
61
|
+
const peerSessionsCache = new node_cache_1.default({
|
|
62
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.USER_DEVICES,
|
|
63
|
+
useClones: false
|
|
64
|
+
});
|
|
65
|
+
const messageRetryManager = enableRecentMessageCache
|
|
66
|
+
? new Utils_1.MessageRetryManager(logger, maxMsgRetryCount)
|
|
67
|
+
: null;
|
|
68
|
+
const encryptionMutex = (0, make_mutex_1.makeKeyedMutex)();
|
|
69
|
+
let mediaConn;
|
|
70
|
+
const refreshMediaConn = async (forceGet = false) => {
|
|
71
|
+
const media = await mediaConn;
|
|
72
|
+
if (!media || forceGet || new Date().getTime() - media.fetchDate.getTime() > media.ttl * 1000) {
|
|
73
|
+
mediaConn = (async () => {
|
|
74
|
+
const result = await query({
|
|
75
|
+
tag: 'iq',
|
|
76
|
+
attrs: {
|
|
77
|
+
type: 'set',
|
|
78
|
+
xmlns: 'w:m',
|
|
79
|
+
to: WABinary_1.S_WHATSAPP_NET
|
|
80
|
+
},
|
|
81
|
+
content: [{ tag: 'media_conn', attrs: {} }]
|
|
82
|
+
});
|
|
83
|
+
const mediaConnNode = (0, WABinary_1.getBinaryNodeChild)(result, 'media_conn');
|
|
84
|
+
const node = {
|
|
85
|
+
hosts: (0, WABinary_1.getBinaryNodeChildren)(mediaConnNode, 'host').map(({ attrs }) => ({
|
|
86
|
+
hostname: attrs.hostname,
|
|
87
|
+
maxContentLengthBytes: +attrs.maxContentLengthBytes
|
|
88
|
+
})),
|
|
89
|
+
auth: mediaConnNode.attrs.auth,
|
|
90
|
+
ttl: +mediaConnNode.attrs.ttl,
|
|
91
|
+
fetchDate: new Date()
|
|
92
|
+
};
|
|
93
|
+
logger.debug('fetched media conn');
|
|
94
|
+
return node;
|
|
95
|
+
})();
|
|
96
|
+
}
|
|
97
|
+
return mediaConn;
|
|
98
|
+
};
|
|
99
|
+
const sendReceipt = async (jid, participant, messageIds, type, sts) => {
|
|
100
|
+
if (!messageIds || messageIds.length === 0) {
|
|
101
|
+
throw new boom_1.Boom('missing ids in receipt');
|
|
102
|
+
}
|
|
103
|
+
const isInterop = (0, WABinary_1.isInteropUser)(jid);
|
|
104
|
+
const node = {
|
|
105
|
+
tag: 'receipt',
|
|
106
|
+
attrs: {
|
|
107
|
+
id: messageIds[0]
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const isReadReceipt = type === 'read' || type === 'read-self';
|
|
111
|
+
if (isReadReceipt) {
|
|
112
|
+
node.attrs.t = (0, Utils_1.unixTimestampSeconds)().toString();
|
|
113
|
+
}
|
|
114
|
+
if (type === 'sender' && ((0, WABinary_1.isPnUser)(jid) || (0, WABinary_1.isLidUser)(jid))) {
|
|
115
|
+
node.attrs.recipient = jid;
|
|
116
|
+
node.attrs.to = participant;
|
|
117
|
+
}
|
|
118
|
+
else if (isInterop && !type) {
|
|
119
|
+
const { user } = (0, WABinary_1.jidDecode)(jid);
|
|
120
|
+
node.attrs.to = `${user}:0@interop`;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
node.attrs.to = jid;
|
|
124
|
+
if (participant) {
|
|
125
|
+
node.attrs.participant = participant;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (type) {
|
|
129
|
+
node.attrs.type = type;
|
|
130
|
+
}
|
|
131
|
+
if (isInterop && sts) {
|
|
132
|
+
node.attrs.sts = sts;
|
|
133
|
+
}
|
|
134
|
+
const remainingMessageIds = messageIds.slice(1);
|
|
135
|
+
if (remainingMessageIds.length) {
|
|
136
|
+
node.content = [
|
|
137
|
+
{
|
|
138
|
+
tag: 'list',
|
|
139
|
+
attrs: {},
|
|
140
|
+
content: remainingMessageIds.map(id => ({
|
|
141
|
+
tag: 'item',
|
|
142
|
+
attrs: { id }
|
|
143
|
+
}))
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages');
|
|
148
|
+
await sendNode(node);
|
|
149
|
+
};
|
|
150
|
+
const sendReceipts = async (keys, type) => {
|
|
151
|
+
const recps = (0, Utils_1.aggregateMessageKeysNotFromMe)(keys);
|
|
152
|
+
for (const { jid, participant, messageIds, sts } of recps) {
|
|
153
|
+
await sendReceipt(jid, participant, messageIds, type, sts);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const readMessages = async (keys) => {
|
|
157
|
+
const privacySettings = await fetchPrivacySettings();
|
|
158
|
+
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self';
|
|
159
|
+
await sendReceipts(keys, readType);
|
|
160
|
+
};
|
|
161
|
+
const getUSyncDevices = async (jids, useCache, ignoreZeroDevices) => {
|
|
162
|
+
const deviceResults = [];
|
|
163
|
+
if (!useCache) {
|
|
164
|
+
logger.debug('not using cache for devices');
|
|
165
|
+
}
|
|
166
|
+
const toFetch = [];
|
|
167
|
+
const jidsWithUser = jids
|
|
168
|
+
.map(jid => {
|
|
169
|
+
const decoded = (0, WABinary_1.jidDecode)(jid);
|
|
170
|
+
const user = decoded?.user;
|
|
171
|
+
const device = decoded?.device;
|
|
172
|
+
const isExplicitDevice = typeof device === 'number' && device >= 0;
|
|
173
|
+
if (isExplicitDevice && user) {
|
|
174
|
+
deviceResults.push({
|
|
175
|
+
user,
|
|
176
|
+
device,
|
|
177
|
+
jid
|
|
178
|
+
});
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
jid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
182
|
+
return { jid, user };
|
|
183
|
+
})
|
|
184
|
+
.filter(jid => jid !== null);
|
|
185
|
+
let mgetDevices;
|
|
186
|
+
if (useCache && userDevicesCache.mget) {
|
|
187
|
+
const usersToFetch = jidsWithUser.map(j => j?.user).filter(Boolean);
|
|
188
|
+
mgetDevices = await userDevicesCache.mget(usersToFetch);
|
|
189
|
+
}
|
|
190
|
+
for (const { jid, user } of jidsWithUser) {
|
|
191
|
+
if (useCache) {
|
|
192
|
+
const devices = mgetDevices?.[user] || (userDevicesCache.mget ? undefined : await userDevicesCache.get(user));
|
|
193
|
+
if (devices) {
|
|
194
|
+
const devicesWithJid = devices.map(d => ({
|
|
195
|
+
...d,
|
|
196
|
+
jid: (0, WABinary_1.jidEncode)(d.user, d.server, d.device)
|
|
197
|
+
}));
|
|
198
|
+
deviceResults.push(...devicesWithJid);
|
|
199
|
+
logger.trace({ user }, 'using cache for devices');
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
toFetch.push(jid);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
toFetch.push(jid);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!toFetch.length) {
|
|
210
|
+
return deviceResults;
|
|
211
|
+
}
|
|
212
|
+
const requestedLidUsers = new Set();
|
|
213
|
+
for (const jid of toFetch) {
|
|
214
|
+
if ((0, WABinary_1.isLidUser)(jid) || (0, WABinary_1.isHostedLidUser)(jid)) {
|
|
215
|
+
const user = (0, WABinary_1.jidDecode)(jid)?.user;
|
|
216
|
+
if (user)
|
|
217
|
+
requestedLidUsers.add(user);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const query = new WAUSync_1.USyncQuery().withContext('message').withDeviceProtocol().withLIDProtocol();
|
|
221
|
+
const pnJidsToFetch = toFetch.filter(jid => (0, WABinary_1.isPnUser)(jid) || (0, WABinary_1.isHostedPnUser)(jid));
|
|
222
|
+
const cachedLidPairs = pnJidsToFetch.length
|
|
223
|
+
? (await signalRepository.lidMapping.getLIDsForPNs(pnJidsToFetch)) || []
|
|
224
|
+
: [];
|
|
225
|
+
const cachedLidByPn = new Map(cachedLidPairs.map(({ pn, lid }) => [pn, lid]));
|
|
226
|
+
for (const jid of toFetch) {
|
|
227
|
+
const syncUser = new WAUSync_1.USyncUser().withId(jid);
|
|
228
|
+
const cachedLid = cachedLidByPn.get(jid);
|
|
229
|
+
if (cachedLid)
|
|
230
|
+
syncUser.withLid(cachedLid);
|
|
231
|
+
query.withUser(syncUser);
|
|
232
|
+
}
|
|
233
|
+
const result = await sock.executeUSyncQuery(query);
|
|
234
|
+
if (result) {
|
|
235
|
+
const lidResults = result.list.filter(a => !!a.lid);
|
|
236
|
+
if (lidResults.length > 0) {
|
|
237
|
+
logger.trace('Storing LID maps from device call');
|
|
238
|
+
await signalRepository.lidMapping.storeLIDPNMappings(lidResults.map(a => ({ lid: a.lid, pn: a.id })));
|
|
239
|
+
try {
|
|
240
|
+
const lids = lidResults.map(a => a.lid);
|
|
241
|
+
if (lids.length) {
|
|
242
|
+
await assertSessions(lids, true);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
logger.warn({ e, count: lidResults.length }, 'failed to assert sessions for newly mapped LIDs');
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const extracted = (0, Utils_1.extractDeviceJids)(result?.list, authState.creds.me.id, authState.creds.me.lid, ignoreZeroDevices);
|
|
250
|
+
const deviceMap = {};
|
|
251
|
+
for (const item of extracted) {
|
|
252
|
+
deviceMap[item.user] = deviceMap[item.user] || [];
|
|
253
|
+
deviceMap[item.user]?.push(item);
|
|
254
|
+
}
|
|
255
|
+
for (const [user, userDevices] of Object.entries(deviceMap)) {
|
|
256
|
+
const isLidUser = requestedLidUsers.has(user);
|
|
257
|
+
for (const item of userDevices) {
|
|
258
|
+
const finalJid = isLidUser
|
|
259
|
+
? (0, WABinary_1.jidEncode)(user, item.server, item.device)
|
|
260
|
+
: (0, WABinary_1.jidEncode)(item.user, item.server, item.device);
|
|
261
|
+
deviceResults.push({
|
|
262
|
+
...item,
|
|
263
|
+
jid: finalJid
|
|
264
|
+
});
|
|
265
|
+
logger.debug({
|
|
266
|
+
user: item.user,
|
|
267
|
+
device: item.device,
|
|
268
|
+
finalJid,
|
|
269
|
+
usedLid: isLidUser
|
|
270
|
+
}, 'Processed device with LID priority');
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (userDevicesCache.mset) {
|
|
274
|
+
await userDevicesCache.mset(Object.entries(deviceMap).map(([key, value]) => ({ key, value })));
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
for (const key in deviceMap) {
|
|
278
|
+
if (deviceMap[key])
|
|
279
|
+
await userDevicesCache.set(key, deviceMap[key]);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const userDeviceUpdates = {};
|
|
283
|
+
for (const [userId, devices] of Object.entries(deviceMap)) {
|
|
284
|
+
if (devices && devices.length > 0) {
|
|
285
|
+
userDeviceUpdates[userId] = devices.map(d => d.device?.toString() || '0');
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (Object.keys(userDeviceUpdates).length > 0) {
|
|
289
|
+
try {
|
|
290
|
+
await authState.keys.set({ 'device-list': userDeviceUpdates });
|
|
291
|
+
logger.debug({ userCount: Object.keys(userDeviceUpdates).length }, 'stored user device lists for bulk migration');
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
logger.warn({ error }, 'failed to store user device lists');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return deviceResults;
|
|
299
|
+
};
|
|
300
|
+
const updateMemberLabel = (jid, memberLabel) => {
|
|
301
|
+
return relayMessage(jid, {
|
|
302
|
+
protocolMessage: {
|
|
303
|
+
type: index_js_1.proto.Message.ProtocolMessage.Type.GROUP_MEMBER_LABEL_CHANGE,
|
|
304
|
+
memberLabel: {
|
|
305
|
+
label: memberLabel?.slice(0, 30),
|
|
306
|
+
labelTimestamp: (0, Utils_1.unixTimestampSeconds)()
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}, {
|
|
310
|
+
additionalNodes: [
|
|
311
|
+
{
|
|
312
|
+
tag: 'meta',
|
|
313
|
+
attrs: {
|
|
314
|
+
tag_reason: 'user_update',
|
|
315
|
+
appdata: 'member_tag'
|
|
316
|
+
},
|
|
317
|
+
content: undefined
|
|
318
|
+
}
|
|
319
|
+
]
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
const assertSessions = async (jids, force) => {
|
|
323
|
+
let didFetchNewSession = false;
|
|
324
|
+
const uniqueJids = [...new Set(jids)];
|
|
325
|
+
const jidsRequiringFetch = [];
|
|
326
|
+
logger.debug({ jids }, 'assertSessions call with jids');
|
|
327
|
+
for (const jid of uniqueJids) {
|
|
328
|
+
const signalId = signalRepository.jidToSignalProtocolAddress(jid);
|
|
329
|
+
const cachedSession = peerSessionsCache.get(signalId);
|
|
330
|
+
if (cachedSession !== undefined) {
|
|
331
|
+
if (cachedSession && !force) {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
const sessionValidation = await signalRepository.validateSession(jid);
|
|
337
|
+
const hasSession = sessionValidation.exists;
|
|
338
|
+
peerSessionsCache.set(signalId, hasSession);
|
|
339
|
+
if (hasSession && !force) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
jidsRequiringFetch.push(jid);
|
|
344
|
+
}
|
|
345
|
+
if (jidsRequiringFetch.length) {
|
|
346
|
+
const wireJids = [
|
|
347
|
+
...jidsRequiringFetch.filter(jid => !!(0, WABinary_1.isLidUser)(jid) || !!(0, WABinary_1.isHostedLidUser)(jid)),
|
|
348
|
+
...((await signalRepository.lidMapping.getLIDsForPNs(jidsRequiringFetch.filter(jid => !!(0, WABinary_1.isPnUser)(jid) || !!(0, WABinary_1.isHostedPnUser)(jid)))) || []).map(a => a.lid),
|
|
349
|
+
...jidsRequiringFetch.filter(jid => (0, WABinary_1.isInteropUser)(jid))
|
|
350
|
+
];
|
|
351
|
+
const interopFetches = wireJids.filter(j => (0, WABinary_1.isInteropUser)(j));
|
|
352
|
+
if (interopFetches.length) {
|
|
353
|
+
logger.info({ interopFetches }, '[interop] fetching pre-key bundle for interop device(s)');
|
|
354
|
+
}
|
|
355
|
+
logger.debug({ jidsRequiringFetch, wireJids }, 'fetching sessions');
|
|
356
|
+
if (!wireJids.length) {
|
|
357
|
+
logger.debug({ jidsRequiringFetch }, 'assertSessions: no wire JIDs to fetch (all unsupported domain)');
|
|
358
|
+
return didFetchNewSession;
|
|
359
|
+
}
|
|
360
|
+
const result = await query({
|
|
361
|
+
tag: 'iq',
|
|
362
|
+
attrs: {
|
|
363
|
+
xmlns: 'encrypt',
|
|
364
|
+
type: 'get',
|
|
365
|
+
to: WABinary_1.S_WHATSAPP_NET
|
|
366
|
+
},
|
|
367
|
+
content: [
|
|
368
|
+
{
|
|
369
|
+
tag: 'key',
|
|
370
|
+
attrs: {},
|
|
371
|
+
content: wireJids.map(jid => {
|
|
372
|
+
const attrs = { jid };
|
|
373
|
+
if (force)
|
|
374
|
+
attrs.reason = 'identity';
|
|
375
|
+
return { tag: 'user', attrs };
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
]
|
|
379
|
+
});
|
|
380
|
+
if (interopFetches.length) {
|
|
381
|
+
logger.debug({ interopFetches }, '[interop] pre-key IQ response received');
|
|
382
|
+
}
|
|
383
|
+
await (0, Utils_1.parseAndInjectE2ESessions)(result, signalRepository);
|
|
384
|
+
didFetchNewSession = true;
|
|
385
|
+
for (const wireJid of wireJids) {
|
|
386
|
+
const signalId = signalRepository.jidToSignalProtocolAddress(wireJid);
|
|
387
|
+
peerSessionsCache.set(signalId, true);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return didFetchNewSession;
|
|
391
|
+
};
|
|
392
|
+
const sendPeerDataOperationMessage = async (pdoMessage) => {
|
|
393
|
+
if (!authState.creds.me?.id) {
|
|
394
|
+
throw new boom_1.Boom('Not authenticated');
|
|
395
|
+
}
|
|
396
|
+
const protocolMessage = {
|
|
397
|
+
protocolMessage: {
|
|
398
|
+
peerDataOperationRequestMessage: pdoMessage,
|
|
399
|
+
type: index_js_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
const meJid = (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id);
|
|
403
|
+
const msgId = await relayMessage(meJid, protocolMessage, {
|
|
404
|
+
additionalAttributes: {
|
|
405
|
+
category: 'peer',
|
|
406
|
+
push_priority: 'high_force'
|
|
407
|
+
},
|
|
408
|
+
additionalNodes: [
|
|
409
|
+
{
|
|
410
|
+
tag: 'meta',
|
|
411
|
+
attrs: { appdata: 'default' }
|
|
412
|
+
}
|
|
413
|
+
]
|
|
414
|
+
});
|
|
415
|
+
return msgId;
|
|
416
|
+
};
|
|
417
|
+
const createParticipantNodes = async (recipientJids, message, extraAttrs, dsmMessage) => {
|
|
418
|
+
if (!recipientJids.length) {
|
|
419
|
+
return { nodes: [], shouldIncludeDeviceIdentity: false };
|
|
420
|
+
}
|
|
421
|
+
const patched = await patchMessageBeforeSending(message, recipientJids);
|
|
422
|
+
const patchedMessages = Array.isArray(patched)
|
|
423
|
+
? patched
|
|
424
|
+
: recipientJids.map(jid => ({ recipientJid: jid, message: patched }));
|
|
425
|
+
let shouldIncludeDeviceIdentity = false;
|
|
426
|
+
const meId = authState.creds.me.id;
|
|
427
|
+
const meLid = authState.creds.me?.lid;
|
|
428
|
+
const meLidUser = meLid ? (0, WABinary_1.jidDecode)(meLid)?.user : null;
|
|
429
|
+
const encryptionPromises = patchedMessages.map(async ({ recipientJid: jid, message: patchedMessage }) => {
|
|
430
|
+
try {
|
|
431
|
+
if (!jid)
|
|
432
|
+
return null;
|
|
433
|
+
let msgToEncrypt = patchedMessage;
|
|
434
|
+
if (dsmMessage) {
|
|
435
|
+
const { user: targetUser } = (0, WABinary_1.jidDecode)(jid);
|
|
436
|
+
const { user: ownPnUser } = (0, WABinary_1.jidDecode)(meId);
|
|
437
|
+
const ownLidUser = meLidUser;
|
|
438
|
+
const isOwnUser = targetUser === ownPnUser || (ownLidUser && targetUser === ownLidUser);
|
|
439
|
+
const isExactSenderDevice = jid === meId || (meLid && jid === meLid);
|
|
440
|
+
if (isOwnUser && !isExactSenderDevice) {
|
|
441
|
+
msgToEncrypt = dsmMessage;
|
|
442
|
+
logger.debug({ jid, targetUser }, 'Using DSM for own device');
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const bytes = (0, Utils_1.encodeWAMessage)(msgToEncrypt);
|
|
446
|
+
const mutexKey = jid;
|
|
447
|
+
const node = await encryptionMutex.mutex(mutexKey, async () => {
|
|
448
|
+
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes });
|
|
449
|
+
if (type === 'pkmsg') {
|
|
450
|
+
shouldIncludeDeviceIdentity = true;
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
tag: 'to',
|
|
454
|
+
attrs: { jid },
|
|
455
|
+
content: [
|
|
456
|
+
{
|
|
457
|
+
tag: 'enc',
|
|
458
|
+
attrs: { v: '2', type, ...(extraAttrs || {}) },
|
|
459
|
+
content: ciphertext
|
|
460
|
+
}
|
|
461
|
+
]
|
|
462
|
+
};
|
|
463
|
+
});
|
|
464
|
+
return node;
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
logger.error({ jid, err }, 'Failed to encrypt for recipient');
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null);
|
|
472
|
+
if (recipientJids.length > 0 && nodes.length === 0) {
|
|
473
|
+
throw new boom_1.Boom('All encryptions failed', { statusCode: 500 });
|
|
474
|
+
}
|
|
475
|
+
return { nodes, shouldIncludeDeviceIdentity };
|
|
476
|
+
};
|
|
477
|
+
const relayMessage = async (jid, message, { messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, useCachedGroupMetadata, statusJidList, statusPrivacy, AI = false }) => {
|
|
478
|
+
const meId = authState.creds.me.id;
|
|
479
|
+
const meLid = authState.creds.me?.lid;
|
|
480
|
+
const isRetryResend = Boolean(participant?.jid);
|
|
481
|
+
let shouldIncludeDeviceIdentity = isRetryResend;
|
|
482
|
+
const statusJid = 'status@broadcast';
|
|
483
|
+
const { user, server } = (0, WABinary_1.jidDecode)(jid);
|
|
484
|
+
const isGroup = server === 'g.us';
|
|
485
|
+
const isStatus = jid === statusJid;
|
|
486
|
+
const isLid = server === 'lid';
|
|
487
|
+
const isNewsletter = server === 'newsletter';
|
|
488
|
+
const isInterop = (0, WABinary_1.isInteropUser)(jid);
|
|
489
|
+
const isGroupOrStatus = isGroup || isStatus;
|
|
490
|
+
const finalJid = jid;
|
|
491
|
+
msgId = msgId || (0, Utils_1.generateMessageIDV2)(meId);
|
|
492
|
+
useUserDevicesCache = useUserDevicesCache !== false;
|
|
493
|
+
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus;
|
|
494
|
+
const participants = [];
|
|
495
|
+
const destinationJid = !isStatus ? finalJid : statusJid;
|
|
496
|
+
const binaryNodeContent = [];
|
|
497
|
+
const devices = [];
|
|
498
|
+
let reportingMessage;
|
|
499
|
+
const meMsg = {
|
|
500
|
+
deviceSentMessage: {
|
|
501
|
+
destinationJid,
|
|
502
|
+
message
|
|
503
|
+
},
|
|
504
|
+
messageContextInfo: message.messageContextInfo
|
|
505
|
+
};
|
|
506
|
+
const extraAttrs = {};
|
|
507
|
+
const regexGroupOld = /^(\d{1,15})-(\d+)@g\.us$/;
|
|
508
|
+
const messages = Utils_1.normalizeMessageContent(message);
|
|
509
|
+
const buttonType = getButtonType(messages);
|
|
510
|
+
const pollMessage = messages.pollCreationMessage || messages.pollCreationMessageV2 || messages.pollCreationMessageV3;
|
|
511
|
+
if (participant) {
|
|
512
|
+
if (!isGroup && !isStatus) {
|
|
513
|
+
additionalAttributes = { ...additionalAttributes, device_fanout: 'false' };
|
|
514
|
+
}
|
|
515
|
+
const { user, device } = (0, WABinary_1.jidDecode)(participant.jid);
|
|
516
|
+
devices.push({
|
|
517
|
+
user,
|
|
518
|
+
device,
|
|
519
|
+
jid: participant.jid
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
await authState.keys.transaction(async () => {
|
|
523
|
+
const mediaType = getMediaType(message);
|
|
524
|
+
if (mediaType) {
|
|
525
|
+
extraAttrs['mediatype'] = mediaType;
|
|
526
|
+
}
|
|
527
|
+
if (isNewsletter) {
|
|
528
|
+
const patched = patchMessageBeforeSending ? await patchMessageBeforeSending(message, []) : message;
|
|
529
|
+
const bytes = (0, Utils_1.encodeNewsletterMessage)(patched);
|
|
530
|
+
binaryNodeContent.push({
|
|
531
|
+
tag: 'plaintext',
|
|
532
|
+
attrs: {},
|
|
533
|
+
content: bytes
|
|
534
|
+
});
|
|
535
|
+
const stanza = {
|
|
536
|
+
tag: 'message',
|
|
537
|
+
attrs: {
|
|
538
|
+
to: jid,
|
|
539
|
+
id: msgId,
|
|
540
|
+
type: getTypeMessage(message),
|
|
541
|
+
...(additionalAttributes || {})
|
|
542
|
+
},
|
|
543
|
+
content: binaryNodeContent
|
|
544
|
+
};
|
|
545
|
+
logger.debug({ msgId }, `sending newsletter message to ${jid}`);
|
|
546
|
+
await sendNode(stanza);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if ((0, Utils_1.normalizeMessageContent)(message)?.pinInChatMessage ||
|
|
550
|
+
(0, Utils_1.normalizeMessageContent)(message)?.reactionMessage) {
|
|
551
|
+
extraAttrs['decrypt-fail'] = 'hide';
|
|
552
|
+
}
|
|
553
|
+
if (isGroupOrStatus && !isRetryResend) {
|
|
554
|
+
const [groupData, senderKeyMap] = await Promise.all([
|
|
555
|
+
(async () => {
|
|
556
|
+
let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined;
|
|
557
|
+
if (groupData && Array.isArray(groupData?.participants)) {
|
|
558
|
+
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata');
|
|
559
|
+
}
|
|
560
|
+
else if (!isStatus) {
|
|
561
|
+
groupData = await groupMetadata(jid);
|
|
562
|
+
}
|
|
563
|
+
return groupData;
|
|
564
|
+
})(),
|
|
565
|
+
(async () => {
|
|
566
|
+
if (!participant && !isStatus) {
|
|
567
|
+
const result = await authState.keys.get('sender-key-memory', [jid]);
|
|
568
|
+
return result[jid] || {};
|
|
569
|
+
}
|
|
570
|
+
return {};
|
|
571
|
+
})()
|
|
572
|
+
]);
|
|
573
|
+
const participantsList = groupData ? groupData.participants.map(p => p.id) : [];
|
|
574
|
+
if (groupData?.ephemeralDuration && groupData.ephemeralDuration > 0) {
|
|
575
|
+
additionalAttributes = {
|
|
576
|
+
...additionalAttributes,
|
|
577
|
+
expiration: groupData.ephemeralDuration.toString()
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
if (isStatus && statusJidList) {
|
|
581
|
+
participantsList.push(...statusJidList);
|
|
582
|
+
}
|
|
583
|
+
const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false);
|
|
584
|
+
devices.push(...additionalDevices);
|
|
585
|
+
if (isGroup) {
|
|
586
|
+
additionalAttributes = {
|
|
587
|
+
...additionalAttributes,
|
|
588
|
+
addressing_mode: groupData?.addressingMode || 'lid'
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
if (message?.groupStatusMessageV2 && !message?.messageContextInfo?.messageSecret) {
|
|
592
|
+
const { randomBytes } = require('crypto');
|
|
593
|
+
message = {
|
|
594
|
+
...message,
|
|
595
|
+
messageContextInfo: {
|
|
596
|
+
...(message.messageContextInfo || {}),
|
|
597
|
+
messageSecret: randomBytes(32)
|
|
598
|
+
},
|
|
599
|
+
groupStatusMessageV2: {
|
|
600
|
+
...message.groupStatusMessageV2,
|
|
601
|
+
message: {
|
|
602
|
+
...(message.groupStatusMessageV2.message || {}),
|
|
603
|
+
messageContextInfo: {
|
|
604
|
+
...(message.groupStatusMessageV2.message?.messageContextInfo || {}),
|
|
605
|
+
messageSecret: message.messageContextInfo?.messageSecret || randomBytes(32)
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
if (message.listMessage) {
|
|
612
|
+
const list = message.listMessage;
|
|
613
|
+
const interactiveMessage = {
|
|
614
|
+
nativeFlowMessage: {
|
|
615
|
+
buttons: [
|
|
616
|
+
{
|
|
617
|
+
name: 'single_select',
|
|
618
|
+
buttonParamsJson: JSON.stringify({
|
|
619
|
+
title: list.buttonText || 'Select',
|
|
620
|
+
sections: (list.sections || []).map(section => ({
|
|
621
|
+
title: section.title || '',
|
|
622
|
+
highlight_label: '',
|
|
623
|
+
rows: (section.rows || []).map(row => ({
|
|
624
|
+
header: '',
|
|
625
|
+
title: row.title || '',
|
|
626
|
+
description: row.description || '',
|
|
627
|
+
id: row.rowId || row.id || ''
|
|
628
|
+
}))
|
|
629
|
+
}))
|
|
630
|
+
})
|
|
631
|
+
}
|
|
632
|
+
],
|
|
633
|
+
messageParamsJson: '',
|
|
634
|
+
messageVersion: 1
|
|
635
|
+
},
|
|
636
|
+
body: { text: list.description || '' },
|
|
637
|
+
footer: list.footerText ? { text: list.footerText } : undefined,
|
|
638
|
+
header: list.title ? { title: list.title, hasMediaAttachment: false, subtitle: '' } : undefined,
|
|
639
|
+
contextInfo: list.contextInfo
|
|
640
|
+
};
|
|
641
|
+
message = { interactiveMessage };
|
|
642
|
+
}
|
|
643
|
+
else if (message.buttonsMessage) {
|
|
644
|
+
const bMsg = message.buttonsMessage;
|
|
645
|
+
const buttons = (bMsg.buttons || []).map(btn => ({
|
|
646
|
+
name: 'quick_reply',
|
|
647
|
+
buttonParamsJson: JSON.stringify({
|
|
648
|
+
display_text: btn.buttonText?.displayText || btn.buttonText || '',
|
|
649
|
+
id: btn.buttonId || btn.buttonText?.displayText || ''
|
|
650
|
+
})
|
|
651
|
+
}));
|
|
652
|
+
const interactiveMessage = {
|
|
653
|
+
nativeFlowMessage: {
|
|
654
|
+
buttons,
|
|
655
|
+
messageParamsJson: '',
|
|
656
|
+
messageVersion: 1
|
|
657
|
+
},
|
|
658
|
+
body: { text: bMsg.contentText || bMsg.text || '' },
|
|
659
|
+
footer: bMsg.footerText ? { text: bMsg.footerText } : undefined,
|
|
660
|
+
header: bMsg.text
|
|
661
|
+
? { title: bMsg.text, hasMediaAttachment: false, subtitle: '' }
|
|
662
|
+
: bMsg.imageMessage || bMsg.videoMessage || bMsg.documentMessage
|
|
663
|
+
? {
|
|
664
|
+
hasMediaAttachment: true,
|
|
665
|
+
...(bMsg.imageMessage ? { imageMessage: bMsg.imageMessage } : {}),
|
|
666
|
+
...(bMsg.videoMessage ? { videoMessage: bMsg.videoMessage } : {})
|
|
667
|
+
}
|
|
668
|
+
: undefined,
|
|
669
|
+
contextInfo: bMsg.contextInfo
|
|
670
|
+
};
|
|
671
|
+
message = { interactiveMessage };
|
|
672
|
+
}
|
|
673
|
+
else if (message.templateMessage) {
|
|
674
|
+
const tmpl = message.templateMessage.hydratedTemplate || message.templateMessage.fourRowTemplate;
|
|
675
|
+
if (tmpl) {
|
|
676
|
+
const hydratedButtons = tmpl.hydratedButtons || [];
|
|
677
|
+
const buttons = hydratedButtons
|
|
678
|
+
.map(hBtn => {
|
|
679
|
+
if (hBtn.quickReplyButton) {
|
|
680
|
+
return {
|
|
681
|
+
name: 'quick_reply',
|
|
682
|
+
buttonParamsJson: JSON.stringify({
|
|
683
|
+
display_text: hBtn.quickReplyButton.displayText || '',
|
|
684
|
+
id: hBtn.quickReplyButton.id || hBtn.quickReplyButton.displayText || ''
|
|
685
|
+
})
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
else if (hBtn.urlButton) {
|
|
689
|
+
return {
|
|
690
|
+
name: 'cta_url',
|
|
691
|
+
buttonParamsJson: JSON.stringify({
|
|
692
|
+
display_text: hBtn.urlButton.displayText || '',
|
|
693
|
+
url: hBtn.urlButton.url || '',
|
|
694
|
+
merchant_url: hBtn.urlButton.url || ''
|
|
695
|
+
})
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
else if (hBtn.callButton) {
|
|
699
|
+
return {
|
|
700
|
+
name: 'cta_call',
|
|
701
|
+
buttonParamsJson: JSON.stringify({
|
|
702
|
+
display_text: hBtn.callButton.displayText || '',
|
|
703
|
+
phone_number: hBtn.callButton.phoneNumber || ''
|
|
704
|
+
})
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
return null;
|
|
708
|
+
})
|
|
709
|
+
.filter(Boolean);
|
|
710
|
+
const interactiveMessage = {
|
|
711
|
+
nativeFlowMessage: {
|
|
712
|
+
buttons,
|
|
713
|
+
messageParamsJson: '',
|
|
714
|
+
messageVersion: 1
|
|
715
|
+
},
|
|
716
|
+
body: { text: tmpl.hydratedContentText || tmpl.contentText || '' },
|
|
717
|
+
footer: tmpl.hydratedFooterText ? { text: tmpl.hydratedFooterText } : undefined,
|
|
718
|
+
header: tmpl.hydratedTitleText
|
|
719
|
+
? { title: tmpl.hydratedTitleText, hasMediaAttachment: false, subtitle: '' }
|
|
720
|
+
: tmpl.imageMessage || tmpl.videoMessage || tmpl.documentMessage
|
|
721
|
+
? {
|
|
722
|
+
hasMediaAttachment: true,
|
|
723
|
+
...(tmpl.imageMessage ? { imageMessage: tmpl.imageMessage } : {}),
|
|
724
|
+
...(tmpl.videoMessage ? { videoMessage: tmpl.videoMessage } : {})
|
|
725
|
+
}
|
|
726
|
+
: undefined,
|
|
727
|
+
contextInfo: tmpl.contextInfo
|
|
728
|
+
};
|
|
729
|
+
message = { interactiveMessage };
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const patched = await patchMessageBeforeSending(message);
|
|
733
|
+
if (Array.isArray(patched)) {
|
|
734
|
+
throw new boom_1.Boom('Per-jid patching is not supported in groups');
|
|
735
|
+
}
|
|
736
|
+
const bytes = (0, Utils_1.encodeWAMessage)(patched);
|
|
737
|
+
reportingMessage = patched;
|
|
738
|
+
const groupAddressingMode = additionalAttributes?.['addressing_mode'] || groupData?.addressingMode || 'lid';
|
|
739
|
+
const groupSenderIdentity = groupAddressingMode === 'lid' && meLid ? meLid : meId;
|
|
740
|
+
const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({
|
|
741
|
+
group: destinationJid,
|
|
742
|
+
data: bytes,
|
|
743
|
+
meId: groupSenderIdentity
|
|
744
|
+
});
|
|
745
|
+
const senderKeyRecipients = [];
|
|
746
|
+
for (const device of devices) {
|
|
747
|
+
const deviceJid = device.jid;
|
|
748
|
+
const hasKey = !!senderKeyMap[deviceJid];
|
|
749
|
+
if ((!hasKey || !!participant) &&
|
|
750
|
+
!(0, WABinary_1.isHostedLidUser)(deviceJid) &&
|
|
751
|
+
!(0, WABinary_1.isHostedPnUser)(deviceJid) &&
|
|
752
|
+
device.device !== 99) {
|
|
753
|
+
senderKeyRecipients.push(deviceJid);
|
|
754
|
+
senderKeyMap[deviceJid] = true;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (senderKeyRecipients.length) {
|
|
758
|
+
logger.debug({ senderKeyJids: senderKeyRecipients }, 'sending new sender key');
|
|
759
|
+
const senderKeyMsg = {
|
|
760
|
+
senderKeyDistributionMessage: {
|
|
761
|
+
axolotlSenderKeyDistributionMessage: senderKeyDistributionMessage,
|
|
762
|
+
groupId: destinationJid
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
const senderKeySessionTargets = senderKeyRecipients;
|
|
766
|
+
await assertSessions(senderKeySessionTargets);
|
|
767
|
+
const result = await createParticipantNodes(senderKeyRecipients, senderKeyMsg, extraAttrs);
|
|
768
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity;
|
|
769
|
+
participants.push(...result.nodes);
|
|
770
|
+
}
|
|
771
|
+
binaryNodeContent.push({
|
|
772
|
+
tag: 'enc',
|
|
773
|
+
attrs: { v: '2', type: 'skmsg', ...extraAttrs },
|
|
774
|
+
content: ciphertext
|
|
775
|
+
});
|
|
776
|
+
await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } });
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
let ownId = meId;
|
|
780
|
+
if (isLid && meLid) {
|
|
781
|
+
ownId = meLid;
|
|
782
|
+
logger.debug({ to: jid, ownId }, 'Using LID identity for @lid conversation');
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
logger.debug({ to: jid, ownId }, 'Using PN identity for @s.whatsapp.net conversation');
|
|
786
|
+
}
|
|
787
|
+
const { user: ownUser } = (0, WABinary_1.jidDecode)(ownId);
|
|
788
|
+
if (!participant) {
|
|
789
|
+
const patchedForReporting = await patchMessageBeforeSending(message, [jid]);
|
|
790
|
+
reportingMessage = Array.isArray(patchedForReporting)
|
|
791
|
+
? patchedForReporting.find(item => item.recipientJid === jid) || patchedForReporting[0]
|
|
792
|
+
: patchedForReporting;
|
|
793
|
+
}
|
|
794
|
+
if (!isRetryResend) {
|
|
795
|
+
const targetUserServer = isLid ? 'lid' : isInterop ? 'interop' : 's.whatsapp.net';
|
|
796
|
+
devices.push({
|
|
797
|
+
user,
|
|
798
|
+
device: 0,
|
|
799
|
+
jid: (0, WABinary_1.jidEncode)(user, targetUserServer, 0)
|
|
800
|
+
});
|
|
801
|
+
if (user !== ownUser && !isInterop) {
|
|
802
|
+
const ownUserServer = isLid ? 'lid' : 's.whatsapp.net';
|
|
803
|
+
const ownUserForAddressing = isLid && meLid ? (0, WABinary_1.jidDecode)(meLid).user : (0, WABinary_1.jidDecode)(meId).user;
|
|
804
|
+
devices.push({
|
|
805
|
+
user: ownUserForAddressing,
|
|
806
|
+
device: 0,
|
|
807
|
+
jid: (0, WABinary_1.jidEncode)(ownUserForAddressing, ownUserServer, 0)
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
if (additionalAttributes?.['category'] !== 'peer' && !isInterop) {
|
|
811
|
+
devices.length = 0;
|
|
812
|
+
const senderIdentity = isLid && meLid
|
|
813
|
+
? (0, WABinary_1.jidEncode)((0, WABinary_1.jidDecode)(meLid)?.user, 'lid', undefined)
|
|
814
|
+
: (0, WABinary_1.jidEncode)((0, WABinary_1.jidDecode)(meId)?.user, 's.whatsapp.net', undefined);
|
|
815
|
+
const sessionDevices = await getUSyncDevices([senderIdentity, jid], true, false);
|
|
816
|
+
devices.push(...sessionDevices);
|
|
817
|
+
logger.debug({
|
|
818
|
+
deviceCount: devices.length,
|
|
819
|
+
devices: devices.map(d => `${d.user}:${d.device}@${(0, WABinary_1.jidDecode)(d.jid)?.server}`)
|
|
820
|
+
}, 'Device enumeration complete with unified addressing');
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const allRecipients = [];
|
|
824
|
+
const meRecipients = [];
|
|
825
|
+
const otherRecipients = [];
|
|
826
|
+
const { user: mePnUser } = (0, WABinary_1.jidDecode)(meId);
|
|
827
|
+
const { user: meLidUser } = meLid ? (0, WABinary_1.jidDecode)(meLid) : { user: null };
|
|
828
|
+
for (const { user, jid } of devices) {
|
|
829
|
+
const isExactSenderDevice = jid === meId || (meLid && jid === meLid);
|
|
830
|
+
if (isExactSenderDevice) {
|
|
831
|
+
logger.debug({ jid, meId, meLid }, 'Skipping exact sender device (whatsmeow pattern)');
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
const isMe = user === mePnUser || user === meLidUser;
|
|
835
|
+
if (isMe) {
|
|
836
|
+
meRecipients.push(jid);
|
|
837
|
+
}
|
|
838
|
+
else {
|
|
839
|
+
otherRecipients.push(jid);
|
|
840
|
+
}
|
|
841
|
+
allRecipients.push(jid);
|
|
842
|
+
}
|
|
843
|
+
await assertSessions(allRecipients);
|
|
844
|
+
const [{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }] = await Promise.all([
|
|
845
|
+
createParticipantNodes(meRecipients, meMsg || message, extraAttrs),
|
|
846
|
+
createParticipantNodes(otherRecipients, message, extraAttrs, meMsg)
|
|
847
|
+
]);
|
|
848
|
+
participants.push(...meNodes);
|
|
849
|
+
participants.push(...otherNodes);
|
|
850
|
+
if (meRecipients.length > 0 || otherRecipients.length > 0) {
|
|
851
|
+
extraAttrs['phash'] = (0, Utils_1.generateParticipantHashV2)([...meRecipients, ...otherRecipients]);
|
|
852
|
+
}
|
|
853
|
+
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2;
|
|
854
|
+
}
|
|
855
|
+
if (isRetryResend) {
|
|
856
|
+
const isParticipantLid = (0, WABinary_1.isLidUser)(participant.jid);
|
|
857
|
+
const isMe = (0, WABinary_1.areJidsSameUser)(participant.jid, isParticipantLid ? meLid : meId);
|
|
858
|
+
const encodedMessageToSend = isMe
|
|
859
|
+
? (0, Utils_1.encodeWAMessage)({
|
|
860
|
+
deviceSentMessage: {
|
|
861
|
+
destinationJid,
|
|
862
|
+
message
|
|
863
|
+
}
|
|
864
|
+
})
|
|
865
|
+
: (0, Utils_1.encodeWAMessage)(message);
|
|
866
|
+
const { type, ciphertext: encryptedContent } = await signalRepository.encryptMessage({
|
|
867
|
+
data: encodedMessageToSend,
|
|
868
|
+
jid: participant.jid
|
|
869
|
+
});
|
|
870
|
+
binaryNodeContent.push({
|
|
871
|
+
tag: 'enc',
|
|
872
|
+
attrs: {
|
|
873
|
+
v: '2',
|
|
874
|
+
type,
|
|
875
|
+
count: (participant.count ?? 0).toString()
|
|
876
|
+
},
|
|
877
|
+
content: encryptedContent
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
if (participants.length) {
|
|
881
|
+
if (additionalAttributes?.['category'] === 'peer') {
|
|
882
|
+
const peerNode = participants[0]?.content?.[0];
|
|
883
|
+
if (peerNode) {
|
|
884
|
+
binaryNodeContent.push(peerNode);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
else if (isInterop) {
|
|
888
|
+
const recipientNode = participants.find(p => (0, WABinary_1.isInteropUser)(p?.attrs?.jid));
|
|
889
|
+
const encNode = (recipientNode ?? participants[0])?.content?.[0];
|
|
890
|
+
if (encNode) {
|
|
891
|
+
binaryNodeContent.push(encNode);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
else {
|
|
895
|
+
binaryNodeContent.push({
|
|
896
|
+
tag: 'participants',
|
|
897
|
+
attrs: {},
|
|
898
|
+
content: participants
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
const stanza = {
|
|
903
|
+
tag: 'message',
|
|
904
|
+
attrs: {
|
|
905
|
+
id: msgId,
|
|
906
|
+
to: destinationJid,
|
|
907
|
+
type: getTypeMessage(message),
|
|
908
|
+
...(additionalAttributes || {})
|
|
909
|
+
},
|
|
910
|
+
content: binaryNodeContent
|
|
911
|
+
};
|
|
912
|
+
if (participant) {
|
|
913
|
+
if ((0, WABinary_1.isJidGroup)(destinationJid)) {
|
|
914
|
+
stanza.attrs.to = destinationJid;
|
|
915
|
+
stanza.attrs.participant = participant.jid;
|
|
916
|
+
}
|
|
917
|
+
else if ((0, WABinary_1.areJidsSameUser)(participant.jid, meId)) {
|
|
918
|
+
stanza.attrs.to = participant.jid;
|
|
919
|
+
stanza.attrs.recipient = destinationJid;
|
|
920
|
+
}
|
|
921
|
+
else {
|
|
922
|
+
stanza.attrs.to = participant.jid;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
else {
|
|
926
|
+
stanza.attrs.to = destinationJid;
|
|
927
|
+
}
|
|
928
|
+
if (shouldIncludeDeviceIdentity) {
|
|
929
|
+
stanza.content.push({
|
|
930
|
+
tag: 'device-identity',
|
|
931
|
+
attrs: {},
|
|
932
|
+
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
|
|
933
|
+
});
|
|
934
|
+
logger.debug({ jid }, 'adding device identity');
|
|
935
|
+
}
|
|
936
|
+
if (isGroup && regexGroupOld.test(jid) && !message.reactionMessage) {
|
|
937
|
+
stanza.content.push({
|
|
938
|
+
tag: 'multicast',
|
|
939
|
+
attrs: {}
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
if (isStatus && statusPrivacy && !additionalAttributes?.edit) {
|
|
943
|
+
stanza.content.push({
|
|
944
|
+
tag: 'meta',
|
|
945
|
+
attrs: { status_setting: statusPrivacy, session_scope: 'status' }
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
if (pollMessage || messages.eventMessage) {
|
|
949
|
+
stanza.content.push({
|
|
950
|
+
tag: 'meta',
|
|
951
|
+
attrs: messages.eventMessage
|
|
952
|
+
? {
|
|
953
|
+
event_type: 'creation'
|
|
954
|
+
}
|
|
955
|
+
: isNewsletter
|
|
956
|
+
? {
|
|
957
|
+
polltype: 'creation',
|
|
958
|
+
contenttype: pollMessage?.pollContentType === 2 ? 'image' : 'text'
|
|
959
|
+
}
|
|
960
|
+
: {
|
|
961
|
+
polltype: 'creation'
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
if (!isNewsletter &&
|
|
966
|
+
!isRetryResend &&
|
|
967
|
+
reportingMessage?.messageContextInfo?.messageSecret &&
|
|
968
|
+
(0, reporting_utils_1.shouldIncludeReportingToken)(reportingMessage)) {
|
|
969
|
+
try {
|
|
970
|
+
const encoded = (0, Utils_1.encodeWAMessage)(reportingMessage);
|
|
971
|
+
const reportingKey = {
|
|
972
|
+
id: msgId,
|
|
973
|
+
fromMe: true,
|
|
974
|
+
remoteJid: destinationJid,
|
|
975
|
+
participant: participant?.jid
|
|
976
|
+
};
|
|
977
|
+
const reportingNode = await (0, reporting_utils_1.getMessageReportingToken)(encoded, reportingMessage, reportingKey);
|
|
978
|
+
if (reportingNode) {
|
|
979
|
+
stanza.content.push(reportingNode);
|
|
980
|
+
logger.trace({ jid }, 'added reporting token to message');
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
catch (error) {
|
|
984
|
+
logger.warn({ jid, trace: error?.stack }, 'failed to attach reporting token');
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
let didPushAdditional = false;
|
|
988
|
+
if (!isNewsletter && buttonType) {
|
|
989
|
+
const buttonsNode = getButtonArgs(messages);
|
|
990
|
+
const filteredButtons = WABinary_1.getBinaryFilteredButtons(additionalNodes ? additionalNodes : []);
|
|
991
|
+
if (filteredButtons) {
|
|
992
|
+
stanza.content.push(...additionalNodes);
|
|
993
|
+
didPushAdditional = true;
|
|
994
|
+
}
|
|
995
|
+
else {
|
|
996
|
+
stanza.content.push(buttonsNode);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
if (!AI && (0, WABinary_1.isPnUser)(destinationJid)) {
|
|
1000
|
+
const alreadyHasBizBot = WABinary_1.getBinaryFilteredBizBot(additionalNodes || []) ||
|
|
1001
|
+
WABinary_1.getBinaryFilteredBizBot(stanza.content);
|
|
1002
|
+
if (!alreadyHasBizBot) {
|
|
1003
|
+
stanza.content.push({ tag: 'bot', attrs: { biz_bot: '1' } });
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
else if (AI && !isGroup && !isStatus && !isNewsletter) {
|
|
1007
|
+
const existingBizBot = WABinary_1.getBinaryFilteredBizBot(additionalNodes || []);
|
|
1008
|
+
if (!existingBizBot) {
|
|
1009
|
+
stanza.content.push({ tag: 'bot', attrs: { biz_bot: '1' } });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
const isPeerMessage = additionalAttributes?.['category'] === 'peer';
|
|
1013
|
+
const is1on1Send = !isGroup && !isRetryResend && !isStatus && !isNewsletter && !isPeerMessage;
|
|
1014
|
+
const tcTokenJid = is1on1Send
|
|
1015
|
+
? await (0, tc_token_utils_1.resolveTcTokenJid)(destinationJid, getLIDForPN)
|
|
1016
|
+
: destinationJid;
|
|
1017
|
+
const contactTcTokenData = is1on1Send ? await authState.keys.get('tctoken', [tcTokenJid]) : {};
|
|
1018
|
+
const existingTokenEntry = contactTcTokenData[tcTokenJid];
|
|
1019
|
+
let tcTokenBuffer = existingTokenEntry?.token;
|
|
1020
|
+
if (tcTokenBuffer?.length && (0, tc_token_utils_1.isTcTokenExpired)(existingTokenEntry?.timestamp)) {
|
|
1021
|
+
logger.debug({ jid: destinationJid, timestamp: existingTokenEntry?.timestamp }, 'tctoken expired, clearing');
|
|
1022
|
+
tcTokenBuffer = undefined;
|
|
1023
|
+
const cleared = existingTokenEntry?.senderTimestamp !== undefined
|
|
1024
|
+
? { token: Buffer.alloc(0), senderTimestamp: existingTokenEntry.senderTimestamp }
|
|
1025
|
+
: null;
|
|
1026
|
+
try {
|
|
1027
|
+
await authState.keys.set({ tctoken: { [tcTokenJid]: cleared } });
|
|
1028
|
+
}
|
|
1029
|
+
catch (err) {
|
|
1030
|
+
logger.debug({ jid: destinationJid, err: err?.message }, 'failed to persist tctoken expiry cleanup');
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
if (tcTokenBuffer?.length && sock.serverProps.privacyTokenOn1to1) {
|
|
1034
|
+
stanza.content.push({
|
|
1035
|
+
tag: 'tctoken',
|
|
1036
|
+
attrs: {},
|
|
1037
|
+
content: tcTokenBuffer
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
if (is1on1Send && !tcTokenBuffer?.length) {
|
|
1041
|
+
try {
|
|
1042
|
+
const saltStore = await authState.keys.get('nct-salt', ['default']);
|
|
1043
|
+
const nctSalt = saltStore?.['default'];
|
|
1044
|
+
if (nctSalt?.length) {
|
|
1045
|
+
const recipientLid = await getLIDForPN(destinationJid);
|
|
1046
|
+
if (recipientLid) {
|
|
1047
|
+
const { createHmac } = require('crypto');
|
|
1048
|
+
const lidStr = (0, WABinary_1.isLidUser)(recipientLid)
|
|
1049
|
+
? recipientLid
|
|
1050
|
+
: `${recipientLid.split('@')[0]}@lid`;
|
|
1051
|
+
const cs = createHmac('sha256', nctSalt).update(lidStr, 'utf8').digest();
|
|
1052
|
+
stanza.content.push({ tag: 'cstoken', attrs: {}, content: cs });
|
|
1053
|
+
logger.debug({ jid: destinationJid, lid: lidStr }, 'nct: attached cstoken');
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
catch (err) {
|
|
1058
|
+
logger.debug({ jid: destinationJid, err: err?.message }, 'nct: cstoken attach failed');
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
if (additionalNodes && additionalNodes.length > 0 && !didPushAdditional) {
|
|
1062
|
+
stanza.content.push(...additionalNodes);
|
|
1063
|
+
}
|
|
1064
|
+
logger.debug({ msgId }, `sending message to ${participants.length} devices`);
|
|
1065
|
+
await sendNode(stanza);
|
|
1066
|
+
if (message.messageContextInfo?.messageSecret) {
|
|
1067
|
+
setBotMessageSecret(msgId, message.messageContextInfo.messageSecret, destinationJid);
|
|
1068
|
+
}
|
|
1069
|
+
const isProtocolMsg = !!(0, Utils_1.normalizeMessageContent)(message)?.protocolMessage;
|
|
1070
|
+
const isBotOrPSA = destinationJid === WABinary_1.PSA_WID ||
|
|
1071
|
+
(0, WABinary_1.isJidBot)(destinationJid) ||
|
|
1072
|
+
(0, WABinary_1.isJidMetaAI)(destinationJid);
|
|
1073
|
+
if (is1on1Send &&
|
|
1074
|
+
!isProtocolMsg &&
|
|
1075
|
+
!isBotOrPSA &&
|
|
1076
|
+
(0, tc_token_utils_1.shouldSendNewTcToken)(existingTokenEntry?.senderTimestamp) &&
|
|
1077
|
+
!inFlightTcTokenIssuance.has(tcTokenJid)) {
|
|
1078
|
+
inFlightTcTokenIssuance.add(tcTokenJid);
|
|
1079
|
+
const issueTimestamp = (0, Utils_1.unixTimestampSeconds)();
|
|
1080
|
+
const getPNForLID = signalRepository.lidMapping.getPNForLID.bind(signalRepository.lidMapping);
|
|
1081
|
+
(0, tc_token_utils_1.resolveIssuanceJid)(destinationJid, sock.serverProps.lidTrustedTokenIssueToLid, getLIDForPN, getPNForLID)
|
|
1082
|
+
.then(issueJid => issuePrivacyTokens([issueJid], issueTimestamp))
|
|
1083
|
+
.then(async (result) => {
|
|
1084
|
+
await (0, tc_token_utils_1.storeTcTokensFromIqResult)({
|
|
1085
|
+
result,
|
|
1086
|
+
fallbackJid: tcTokenJid,
|
|
1087
|
+
keys: authState.keys,
|
|
1088
|
+
getLIDForPN
|
|
1089
|
+
});
|
|
1090
|
+
const currentData = await authState.keys.get('tctoken', [tcTokenJid]);
|
|
1091
|
+
const currentEntry = currentData[tcTokenJid];
|
|
1092
|
+
const indexWrite = await (0, tc_token_utils_1.buildMergedTcTokenIndexWrite)(authState.keys, [tcTokenJid]);
|
|
1093
|
+
await authState.keys.set({
|
|
1094
|
+
tctoken: {
|
|
1095
|
+
[tcTokenJid]: {
|
|
1096
|
+
token: Buffer.alloc(0),
|
|
1097
|
+
...currentEntry,
|
|
1098
|
+
senderTimestamp: issueTimestamp
|
|
1099
|
+
},
|
|
1100
|
+
...indexWrite
|
|
1101
|
+
}
|
|
1102
|
+
});
|
|
1103
|
+
})
|
|
1104
|
+
.catch(err => {
|
|
1105
|
+
logger.debug({ jid: destinationJid, err: err?.message }, 'fire-and-forget tctoken issuance failed');
|
|
1106
|
+
})
|
|
1107
|
+
.finally(() => {
|
|
1108
|
+
inFlightTcTokenIssuance.delete(tcTokenJid);
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
if (messageRetryManager && !participant) {
|
|
1112
|
+
messageRetryManager.addRecentMessage(destinationJid, msgId, message);
|
|
1113
|
+
}
|
|
1114
|
+
if (isInterop && !isRetryResend) {
|
|
1115
|
+
await trustInteropContact(destinationJid).catch(err => {
|
|
1116
|
+
logger.debug({ err, jid: destinationJid }, 'failed to trust interop contact');
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
}, meId);
|
|
1120
|
+
return msgId;
|
|
1121
|
+
};
|
|
1122
|
+
const getTypeMessage = msg => {
|
|
1123
|
+
const message = Utils_1.normalizeMessageContent(msg);
|
|
1124
|
+
if (message.pollCreationMessage || message.pollCreationMessageV2 || message.pollCreationMessageV3) {
|
|
1125
|
+
return 'poll';
|
|
1126
|
+
}
|
|
1127
|
+
else if (message.reactionMessage) {
|
|
1128
|
+
return 'reaction';
|
|
1129
|
+
}
|
|
1130
|
+
else if (message.eventMessage) {
|
|
1131
|
+
return 'event';
|
|
1132
|
+
}
|
|
1133
|
+
else if (getMediaType(message)) {
|
|
1134
|
+
return 'media';
|
|
1135
|
+
}
|
|
1136
|
+
else {
|
|
1137
|
+
return 'text';
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
1140
|
+
const getMediaType = message => {
|
|
1141
|
+
if (message.imageMessage) {
|
|
1142
|
+
return 'image';
|
|
1143
|
+
}
|
|
1144
|
+
else if (message.stickerMessage) {
|
|
1145
|
+
return message.stickerMessage.isLottie
|
|
1146
|
+
? '1p_sticker'
|
|
1147
|
+
: message.stickerMessage.isAvatar
|
|
1148
|
+
? 'avatar_sticker'
|
|
1149
|
+
: 'sticker';
|
|
1150
|
+
}
|
|
1151
|
+
else if (message.videoMessage) {
|
|
1152
|
+
return message.videoMessage.gifPlayback ? 'gif' : 'video';
|
|
1153
|
+
}
|
|
1154
|
+
else if (message.audioMessage) {
|
|
1155
|
+
return message.audioMessage.ptt ? 'ptt' : 'audio';
|
|
1156
|
+
}
|
|
1157
|
+
else if (message.ptvMessage) {
|
|
1158
|
+
return 'ptv';
|
|
1159
|
+
}
|
|
1160
|
+
else if (message.albumMessage) {
|
|
1161
|
+
return 'collection';
|
|
1162
|
+
}
|
|
1163
|
+
else if (message.contactMessage) {
|
|
1164
|
+
return 'vcard';
|
|
1165
|
+
}
|
|
1166
|
+
else if (message.documentMessage) {
|
|
1167
|
+
return 'document';
|
|
1168
|
+
}
|
|
1169
|
+
else if (message.stickerPackMessage) {
|
|
1170
|
+
return 'sticker_pack';
|
|
1171
|
+
}
|
|
1172
|
+
else if (message.contactsArrayMessage) {
|
|
1173
|
+
return 'contact_array';
|
|
1174
|
+
}
|
|
1175
|
+
else if (message.locationMessage) {
|
|
1176
|
+
return 'location';
|
|
1177
|
+
}
|
|
1178
|
+
else if (message.liveLocationMessage) {
|
|
1179
|
+
return 'livelocation';
|
|
1180
|
+
}
|
|
1181
|
+
else if (message.listMessage) {
|
|
1182
|
+
return 'list';
|
|
1183
|
+
}
|
|
1184
|
+
else if (message.listResponseMessage) {
|
|
1185
|
+
return 'list_response';
|
|
1186
|
+
}
|
|
1187
|
+
else if (message.buttonsResponseMessage) {
|
|
1188
|
+
return 'buttons_response';
|
|
1189
|
+
}
|
|
1190
|
+
else if (message.orderMessage) {
|
|
1191
|
+
return 'order';
|
|
1192
|
+
}
|
|
1193
|
+
else if (message.productMessage) {
|
|
1194
|
+
return 'product';
|
|
1195
|
+
}
|
|
1196
|
+
else if (message.interactiveResponseMessage) {
|
|
1197
|
+
return 'native_flow_response';
|
|
1198
|
+
}
|
|
1199
|
+
else if (/https:\/\/wa\.me\/c\/\d+/.test(message.extendedTextMessage?.text)) {
|
|
1200
|
+
return 'cataloglink';
|
|
1201
|
+
}
|
|
1202
|
+
else if (/https:\/\/wa\.me\/p\/\d+\/\d+/.test(message.extendedTextMessage?.text)) {
|
|
1203
|
+
return 'productlink';
|
|
1204
|
+
}
|
|
1205
|
+
else if (message.extendedTextMessage?.matchedText || message.groupInviteMessage) {
|
|
1206
|
+
return 'url';
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
const getButtonType = message => {
|
|
1210
|
+
if (message.listMessage) {
|
|
1211
|
+
return 'list';
|
|
1212
|
+
}
|
|
1213
|
+
else if (message.buttonsMessage) {
|
|
1214
|
+
return 'buttons';
|
|
1215
|
+
}
|
|
1216
|
+
else if (message.interactiveMessage?.nativeFlowMessage) {
|
|
1217
|
+
const firstButtonName = message.interactiveMessage.nativeFlowMessage.buttons?.[0]?.name;
|
|
1218
|
+
if (firstButtonName === 'review_and_pay') {
|
|
1219
|
+
return 'review_and_pay';
|
|
1220
|
+
}
|
|
1221
|
+
else if (firstButtonName === 'review_order') {
|
|
1222
|
+
return 'review_order';
|
|
1223
|
+
}
|
|
1224
|
+
else if (firstButtonName === 'payment_info') {
|
|
1225
|
+
return 'payment_info';
|
|
1226
|
+
}
|
|
1227
|
+
else if (firstButtonName === 'payment_status') {
|
|
1228
|
+
return 'payment_status';
|
|
1229
|
+
}
|
|
1230
|
+
else if (firstButtonName === 'payment_method') {
|
|
1231
|
+
return 'payment_method';
|
|
1232
|
+
}
|
|
1233
|
+
else if (firstButtonName === 'pix') {
|
|
1234
|
+
return 'pix';
|
|
1235
|
+
}
|
|
1236
|
+
else if (firstButtonName === 'pay') {
|
|
1237
|
+
return 'pay';
|
|
1238
|
+
}
|
|
1239
|
+
return 'native_flow';
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
const getButtonArgs = message => {
|
|
1243
|
+
const nativeFlow = message.interactiveMessage?.nativeFlowMessage;
|
|
1244
|
+
const firstButtonName = nativeFlow?.buttons?.[0]?.name;
|
|
1245
|
+
const nativeFlowSpecials = [
|
|
1246
|
+
'mpm',
|
|
1247
|
+
'cta_catalog',
|
|
1248
|
+
'send_location',
|
|
1249
|
+
'call_permission_request',
|
|
1250
|
+
'wa_payment_transaction_details',
|
|
1251
|
+
'automated_greeting_message_view_catalog'
|
|
1252
|
+
];
|
|
1253
|
+
if (nativeFlow && (firstButtonName === 'review_and_pay' || firstButtonName === 'payment_info')) {
|
|
1254
|
+
return {
|
|
1255
|
+
tag: 'biz',
|
|
1256
|
+
attrs: {
|
|
1257
|
+
native_flow_name: firstButtonName === 'review_and_pay' ? 'order_details' : firstButtonName
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
else if (nativeFlow && nativeFlowSpecials.includes(firstButtonName)) {
|
|
1262
|
+
return {
|
|
1263
|
+
tag: 'biz',
|
|
1264
|
+
attrs: {
|
|
1265
|
+
actual_actors: '2',
|
|
1266
|
+
host_storage: '2',
|
|
1267
|
+
privacy_mode_ts: Utils_1.unixTimestampSeconds().toString()
|
|
1268
|
+
},
|
|
1269
|
+
content: [
|
|
1270
|
+
{
|
|
1271
|
+
tag: 'interactive',
|
|
1272
|
+
attrs: {
|
|
1273
|
+
type: 'native_flow',
|
|
1274
|
+
v: '1'
|
|
1275
|
+
},
|
|
1276
|
+
content: [
|
|
1277
|
+
{
|
|
1278
|
+
tag: 'native_flow',
|
|
1279
|
+
attrs: {
|
|
1280
|
+
v: '2',
|
|
1281
|
+
name: firstButtonName
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
]
|
|
1285
|
+
},
|
|
1286
|
+
{
|
|
1287
|
+
tag: 'quality_control',
|
|
1288
|
+
attrs: {
|
|
1289
|
+
source_type: 'third_party'
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
]
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
else if (nativeFlow || message.buttonsMessage) {
|
|
1296
|
+
return {
|
|
1297
|
+
tag: 'biz',
|
|
1298
|
+
attrs: {
|
|
1299
|
+
actual_actors: '2',
|
|
1300
|
+
host_storage: '2',
|
|
1301
|
+
privacy_mode_ts: Utils_1.unixTimestampSeconds().toString()
|
|
1302
|
+
},
|
|
1303
|
+
content: [
|
|
1304
|
+
{
|
|
1305
|
+
tag: 'interactive',
|
|
1306
|
+
attrs: {
|
|
1307
|
+
type: 'native_flow',
|
|
1308
|
+
v: '1'
|
|
1309
|
+
},
|
|
1310
|
+
content: [
|
|
1311
|
+
{
|
|
1312
|
+
tag: 'native_flow',
|
|
1313
|
+
attrs: {
|
|
1314
|
+
v: '9',
|
|
1315
|
+
name: 'mixed'
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
]
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
tag: 'quality_control',
|
|
1322
|
+
attrs: {
|
|
1323
|
+
source_type: 'third_party'
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
]
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
else if (message.listMessage) {
|
|
1330
|
+
return {
|
|
1331
|
+
tag: 'biz',
|
|
1332
|
+
attrs: {
|
|
1333
|
+
actual_actors: '2',
|
|
1334
|
+
host_storage: '2',
|
|
1335
|
+
privacy_mode_ts: Utils_1.unixTimestampSeconds().toString()
|
|
1336
|
+
},
|
|
1337
|
+
content: [
|
|
1338
|
+
{
|
|
1339
|
+
tag: 'list',
|
|
1340
|
+
attrs: {
|
|
1341
|
+
v: '2',
|
|
1342
|
+
type: 'product_list'
|
|
1343
|
+
}
|
|
1344
|
+
},
|
|
1345
|
+
{
|
|
1346
|
+
tag: 'quality_control',
|
|
1347
|
+
attrs: {
|
|
1348
|
+
source_type: 'third_party'
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
]
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
else {
|
|
1355
|
+
return {
|
|
1356
|
+
tag: 'biz',
|
|
1357
|
+
attrs: {
|
|
1358
|
+
actual_actors: '2',
|
|
1359
|
+
host_storage: '2',
|
|
1360
|
+
privacy_mode_ts: Utils_1.unixTimestampSeconds().toString()
|
|
1361
|
+
}
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
const issuePrivacyTokens = async (jids, timestamp) => {
|
|
1366
|
+
const t = (timestamp ?? (0, Utils_1.unixTimestampSeconds)()).toString();
|
|
1367
|
+
const result = await query({
|
|
1368
|
+
tag: 'iq',
|
|
1369
|
+
attrs: {
|
|
1370
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
1371
|
+
type: 'set',
|
|
1372
|
+
xmlns: 'privacy'
|
|
1373
|
+
},
|
|
1374
|
+
content: [
|
|
1375
|
+
{
|
|
1376
|
+
tag: 'tokens',
|
|
1377
|
+
attrs: {},
|
|
1378
|
+
content: jids.map(jid => ({
|
|
1379
|
+
tag: 'token',
|
|
1380
|
+
attrs: {
|
|
1381
|
+
jid: (0, WABinary_1.jidNormalizedUser)(jid),
|
|
1382
|
+
t,
|
|
1383
|
+
type: 'trusted_contact'
|
|
1384
|
+
}
|
|
1385
|
+
}))
|
|
1386
|
+
}
|
|
1387
|
+
]
|
|
1388
|
+
});
|
|
1389
|
+
return result;
|
|
1390
|
+
};
|
|
1391
|
+
const getPrivacyTokens = issuePrivacyTokens;
|
|
1392
|
+
const waUploadToServer = (0, Utils_1.getWAUploadToServer)(config, refreshMediaConn);
|
|
1393
|
+
const badzzne2 = new interactive_handler_1.badzz88(waUploadToServer, relayMessage, config, sock);
|
|
1394
|
+
const waitForMsgMediaUpdate = (0, Utils_1.bindWaitForEvent)(ev, 'messages.media-update');
|
|
1395
|
+
return {
|
|
1396
|
+
...sock,
|
|
1397
|
+
issuePrivacyTokens,
|
|
1398
|
+
getPrivacyTokens,
|
|
1399
|
+
assertSessions,
|
|
1400
|
+
relayMessage,
|
|
1401
|
+
sendReceipt,
|
|
1402
|
+
sendReceipts,
|
|
1403
|
+
readMessages,
|
|
1404
|
+
refreshMediaConn,
|
|
1405
|
+
waUploadToServer,
|
|
1406
|
+
fetchPrivacySettings,
|
|
1407
|
+
sendPeerDataOperationMessage,
|
|
1408
|
+
createParticipantNodes,
|
|
1409
|
+
getUSyncDevices,
|
|
1410
|
+
messageRetryManager,
|
|
1411
|
+
updateMemberLabel,
|
|
1412
|
+
updateMediaMessage: async (message) => {
|
|
1413
|
+
const content = (0, Utils_1.assertMediaContent)(message.message);
|
|
1414
|
+
const mediaKey = content.mediaKey;
|
|
1415
|
+
const meId = authState.creds.me.id;
|
|
1416
|
+
const node = (0, Utils_1.encryptMediaRetryRequest)(message.key, mediaKey, meId);
|
|
1417
|
+
let error = undefined;
|
|
1418
|
+
await Promise.all([
|
|
1419
|
+
sendNode(node),
|
|
1420
|
+
waitForMsgMediaUpdate(async (update) => {
|
|
1421
|
+
const result = update.find(c => c.key.id === message.key.id);
|
|
1422
|
+
if (result) {
|
|
1423
|
+
if (result.error) {
|
|
1424
|
+
error = result.error;
|
|
1425
|
+
}
|
|
1426
|
+
else {
|
|
1427
|
+
try {
|
|
1428
|
+
const media = (0, Utils_1.decryptMediaRetryData)(result.media, mediaKey, result.key.id);
|
|
1429
|
+
if (media.result !== index_js_1.proto.MediaRetryNotification.ResultType.SUCCESS) {
|
|
1430
|
+
const resultStr = index_js_1.proto.MediaRetryNotification.ResultType[media.result];
|
|
1431
|
+
throw new boom_1.Boom(`Media re-upload failed by device (${resultStr})`, {
|
|
1432
|
+
data: media,
|
|
1433
|
+
statusCode: (0, Utils_1.getStatusCodeForMediaRetry)(media.result) || 404
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
content.directPath = media.directPath;
|
|
1437
|
+
content.url = (0, Utils_1.getUrlFromDirectPath)(content.directPath);
|
|
1438
|
+
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful');
|
|
1439
|
+
}
|
|
1440
|
+
catch (err) {
|
|
1441
|
+
error = err;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
})
|
|
1447
|
+
]);
|
|
1448
|
+
if (error) {
|
|
1449
|
+
throw error;
|
|
1450
|
+
}
|
|
1451
|
+
ev.emit('messages.update', [{ key: message.key, update: { message: message.message } }]);
|
|
1452
|
+
return message;
|
|
1453
|
+
},
|
|
1454
|
+
sendGroupStatus: async (groupIdsOrContent = [], contentOrGroups = {}, opts = {}) => {
|
|
1455
|
+
const toGroupIdArray = input => {
|
|
1456
|
+
if (Array.isArray(input)) {
|
|
1457
|
+
return input;
|
|
1458
|
+
}
|
|
1459
|
+
if (typeof input === 'string') {
|
|
1460
|
+
return [input];
|
|
1461
|
+
}
|
|
1462
|
+
return [];
|
|
1463
|
+
};
|
|
1464
|
+
const normalizeGroupJid = value => {
|
|
1465
|
+
if (typeof value !== 'string') {
|
|
1466
|
+
return '';
|
|
1467
|
+
}
|
|
1468
|
+
const trimmed = value.trim();
|
|
1469
|
+
if (!trimmed) {
|
|
1470
|
+
return '';
|
|
1471
|
+
}
|
|
1472
|
+
if (/^\d{8,}$/.test(trimmed)) {
|
|
1473
|
+
return `${trimmed}@g.us`;
|
|
1474
|
+
}
|
|
1475
|
+
return trimmed;
|
|
1476
|
+
};
|
|
1477
|
+
const useNewSignature = Array.isArray(groupIdsOrContent) || typeof groupIdsOrContent === 'string';
|
|
1478
|
+
const groupIds = useNewSignature ? toGroupIdArray(groupIdsOrContent) : toGroupIdArray(contentOrGroups);
|
|
1479
|
+
const content = useNewSignature ? contentOrGroups || {} : groupIdsOrContent || {};
|
|
1480
|
+
const sendOpts = opts || {};
|
|
1481
|
+
const groupJids = groupIds.map(normalizeGroupJid).filter(jid => (0, WABinary_1.isJidGroup)(jid));
|
|
1482
|
+
const nonGroupJids = groupIds.filter(jid => !(0, WABinary_1.isJidGroup)(normalizeGroupJid(jid)));
|
|
1483
|
+
if (nonGroupJids.length) {
|
|
1484
|
+
logger.warn({ nonGroupJids }, 'ignoring non-group JIDs in sendGroupStatus');
|
|
1485
|
+
}
|
|
1486
|
+
if (!groupJids.length) {
|
|
1487
|
+
return [];
|
|
1488
|
+
}
|
|
1489
|
+
const { allUsers } = await resolveStatusAudience(groupJids, false);
|
|
1490
|
+
if (!allUsers.length) {
|
|
1491
|
+
return [];
|
|
1492
|
+
}
|
|
1493
|
+
const msg = await generateStatusMessage({
|
|
1494
|
+
...content,
|
|
1495
|
+
backgroundColor: content.backgroundColor || getRandomHexColor(),
|
|
1496
|
+
font: normalizeStatusFont(content.font, logger)
|
|
1497
|
+
});
|
|
1498
|
+
await relayMessage(WABinary_1.STORIES_JID, msg.message, {
|
|
1499
|
+
messageId: msg.key.id,
|
|
1500
|
+
statusJidList: allUsers,
|
|
1501
|
+
...(sendOpts.relay || {})
|
|
1502
|
+
});
|
|
1503
|
+
return groupJids;
|
|
1504
|
+
},
|
|
1505
|
+
sendAlbumMessage: async (jid, medias, options = {}) => {
|
|
1506
|
+
const userJid = authState.creds.me.id;
|
|
1507
|
+
for (const media of medias) {
|
|
1508
|
+
if (!media.image && !media.video)
|
|
1509
|
+
throw new TypeError(`medias[i] must have image or video property`);
|
|
1510
|
+
}
|
|
1511
|
+
if (medias.length < 2)
|
|
1512
|
+
throw new RangeError('Minimum 2 media');
|
|
1513
|
+
const time = options.delay || 500;
|
|
1514
|
+
delete options.delay;
|
|
1515
|
+
const album = await (0, Utils_1.generateWAMessageFromContent)(jid, {
|
|
1516
|
+
albumMessage: {
|
|
1517
|
+
expectedImageCount: medias.filter(media => media.image).length,
|
|
1518
|
+
expectedVideoCount: medias.filter(media => media.video).length,
|
|
1519
|
+
...options
|
|
1520
|
+
}
|
|
1521
|
+
}, { userJid, ...options });
|
|
1522
|
+
await relayMessage(jid, album.message, { messageId: album.key.id });
|
|
1523
|
+
let mediaHandle;
|
|
1524
|
+
let msg;
|
|
1525
|
+
for (const i in medias) {
|
|
1526
|
+
const media = medias[i];
|
|
1527
|
+
if (media.image) {
|
|
1528
|
+
msg = await (0, Utils_1.generateWAMessage)(jid, {
|
|
1529
|
+
image: media.image,
|
|
1530
|
+
...media,
|
|
1531
|
+
...options
|
|
1532
|
+
}, {
|
|
1533
|
+
userJid,
|
|
1534
|
+
upload: async (readStream, opts) => {
|
|
1535
|
+
const up = await waUploadToServer(readStream, {
|
|
1536
|
+
...opts,
|
|
1537
|
+
newsletter: (0, WABinary_1.isJidNewsletter)(jid)
|
|
1538
|
+
});
|
|
1539
|
+
mediaHandle = up.handle;
|
|
1540
|
+
return up;
|
|
1541
|
+
},
|
|
1542
|
+
mediaAbProps: authState.creds.mediaAbProps,
|
|
1543
|
+
...options
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
else if (media.video) {
|
|
1547
|
+
msg = await (0, Utils_1.generateWAMessage)(jid, {
|
|
1548
|
+
video: media.video,
|
|
1549
|
+
...media,
|
|
1550
|
+
...options
|
|
1551
|
+
}, {
|
|
1552
|
+
userJid,
|
|
1553
|
+
upload: async (readStream, opts) => {
|
|
1554
|
+
const up = await waUploadToServer(readStream, {
|
|
1555
|
+
...opts,
|
|
1556
|
+
newsletter: (0, WABinary_1.isJidNewsletter)(jid)
|
|
1557
|
+
});
|
|
1558
|
+
mediaHandle = up.handle;
|
|
1559
|
+
return up;
|
|
1560
|
+
},
|
|
1561
|
+
mediaAbProps: authState.creds.mediaAbProps,
|
|
1562
|
+
...options
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
if (msg) {
|
|
1566
|
+
msg.message.messageContextInfo = {
|
|
1567
|
+
messageAssociation: {
|
|
1568
|
+
associationType: 1,
|
|
1569
|
+
parentMessageKey: album.key
|
|
1570
|
+
}
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
await relayMessage(jid, msg.message, { messageId: msg.key.id });
|
|
1574
|
+
await (0, Utils_1.delay)(time);
|
|
1575
|
+
}
|
|
1576
|
+
return album;
|
|
1577
|
+
},
|
|
1578
|
+
sendStatusMention: async (content, jids = []) => {
|
|
1579
|
+
return await badzzne2.sendStatusWhatsApp(content, jids);
|
|
1580
|
+
},
|
|
1581
|
+
sendTable: async (jid, title, headers, rows, quoted, options = {}) => {
|
|
1582
|
+
const { message, messageId } = message_composer_1.generateTableContent(title, headers, rows, quoted, options);
|
|
1583
|
+
await relayMessage(jid, message, { messageId });
|
|
1584
|
+
return { message, messageId };
|
|
1585
|
+
},
|
|
1586
|
+
sendList: async (jid, title, items, quoted, options = {}) => {
|
|
1587
|
+
const { message, messageId } = message_composer_1.generateListContent(title, items, quoted, options);
|
|
1588
|
+
await relayMessage(jid, message, { messageId });
|
|
1589
|
+
return { message, messageId };
|
|
1590
|
+
},
|
|
1591
|
+
sendCodeBlock: async (jid, code, quoted, options = {}) => {
|
|
1592
|
+
const { message, messageId } = message_composer_1.generateCodeBlockContent(code, quoted, options);
|
|
1593
|
+
await relayMessage(jid, message, { messageId });
|
|
1594
|
+
return { message, messageId };
|
|
1595
|
+
},
|
|
1596
|
+
sendLatex: async (jid, quoted, options) => {
|
|
1597
|
+
const { message, messageId } = message_composer_1.generateLatexContent(quoted, options);
|
|
1598
|
+
await relayMessage(jid, message, { messageId });
|
|
1599
|
+
return { message, messageId };
|
|
1600
|
+
},
|
|
1601
|
+
sendLatexImage: async (jid, quoted, options, renderLatexToPng, uploadFn) => {
|
|
1602
|
+
const { message, messageId } = await message_composer_1.generateLatexImageContent(quoted, options, uploadFn, renderLatexToPng);
|
|
1603
|
+
await relayMessage(jid, message, { messageId });
|
|
1604
|
+
return { message, messageId };
|
|
1605
|
+
},
|
|
1606
|
+
sendLatexInlineImage: async (jid, quoted, options, renderLatexToPng, uploadFn) => {
|
|
1607
|
+
const { message, messageId } = await message_composer_1.generateLatexInlineImageContent(quoted, options, uploadFn, renderLatexToPng);
|
|
1608
|
+
await relayMessage(jid, message, { messageId });
|
|
1609
|
+
return { message, messageId };
|
|
1610
|
+
},
|
|
1611
|
+
captureUnifiedResponse: message_composer_1.captureUnifiedResponse,
|
|
1612
|
+
sendUnifiedResponse: async (jid, quoted, captured) => {
|
|
1613
|
+
const { message, messageId } = message_composer_1.generateUnifiedResponseContent(quoted, captured);
|
|
1614
|
+
await relayMessage(jid, message, { messageId });
|
|
1615
|
+
return { message, messageId };
|
|
1616
|
+
},
|
|
1617
|
+
sendRichMessage: async (jid, submessages, quoted, options = {}) => {
|
|
1618
|
+
const { message, messageId } = message_composer_1.generateRichMessageContent(submessages, quoted, options);
|
|
1619
|
+
await relayMessage(jid, message, { messageId });
|
|
1620
|
+
return { message, messageId };
|
|
1621
|
+
},
|
|
1622
|
+
sendMessage: async (jid, content, options = {}) => {
|
|
1623
|
+
const userJid = authState.creds.me.id;
|
|
1624
|
+
if (typeof content === 'object' &&
|
|
1625
|
+
Array.isArray(content.buttons) &&
|
|
1626
|
+
content.buttons.length > 0 &&
|
|
1627
|
+
content.buttons.some(b => b.nativeFlowInfo)) {
|
|
1628
|
+
const interactiveButtons = content.buttons.map(b => {
|
|
1629
|
+
if (b.nativeFlowInfo) {
|
|
1630
|
+
return {
|
|
1631
|
+
name: b.nativeFlowInfo.name,
|
|
1632
|
+
buttonParamsJson: b.nativeFlowInfo.paramsJson || '{}'
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
return {
|
|
1636
|
+
name: 'quick_reply',
|
|
1637
|
+
buttonParamsJson: JSON.stringify({
|
|
1638
|
+
display_text: b.buttonText?.displayText || b.buttonId || 'Button',
|
|
1639
|
+
id: b.buttonId || b.buttonText?.displayText || 'btn'
|
|
1640
|
+
})
|
|
1641
|
+
};
|
|
1642
|
+
});
|
|
1643
|
+
const { buttons, headerType, viewOnce, ...rest } = content;
|
|
1644
|
+
content = { ...rest, interactiveButtons };
|
|
1645
|
+
}
|
|
1646
|
+
if (typeof content === 'object' &&
|
|
1647
|
+
Array.isArray(content.interactiveButtons) &&
|
|
1648
|
+
content.interactiveButtons.length > 0) {
|
|
1649
|
+
const { text = '', caption = '', title = '', footer = '', interactiveButtons, hasMediaAttachment = false, image = null, video = null, document = null, mimetype = null, jpegThumbnail = null, location = null, product = null, businessOwnerJid = null, externalAdReply = null } = content;
|
|
1650
|
+
const processedButtons = [];
|
|
1651
|
+
for (let i = 0; i < interactiveButtons.length; i++) {
|
|
1652
|
+
const btn = interactiveButtons[i];
|
|
1653
|
+
if (!btn || typeof btn !== 'object')
|
|
1654
|
+
throw new Error(`interactiveButtons[${i}] must be an object`);
|
|
1655
|
+
if (btn.name && btn.buttonParamsJson) {
|
|
1656
|
+
processedButtons.push(btn);
|
|
1657
|
+
continue;
|
|
1658
|
+
}
|
|
1659
|
+
if (btn.id || btn.text || btn.displayText) {
|
|
1660
|
+
processedButtons.push({
|
|
1661
|
+
name: 'quick_reply',
|
|
1662
|
+
buttonParamsJson: JSON.stringify({
|
|
1663
|
+
display_text: btn.text || btn.displayText || `Button ${i + 1}`,
|
|
1664
|
+
id: btn.id || `quick_${i + 1}`
|
|
1665
|
+
})
|
|
1666
|
+
});
|
|
1667
|
+
continue;
|
|
1668
|
+
}
|
|
1669
|
+
if (btn.buttonId && btn.buttonText?.displayText) {
|
|
1670
|
+
processedButtons.push({
|
|
1671
|
+
name: 'quick_reply',
|
|
1672
|
+
buttonParamsJson: JSON.stringify({ display_text: btn.buttonText.displayText, id: btn.buttonId })
|
|
1673
|
+
});
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
throw new Error(`interactiveButtons[${i}] has invalid shape`);
|
|
1677
|
+
}
|
|
1678
|
+
let messageContent = {};
|
|
1679
|
+
if (image) {
|
|
1680
|
+
const mi = Buffer.isBuffer(image)
|
|
1681
|
+
? { image }
|
|
1682
|
+
: { image: { url: typeof image === 'object' ? image.url : image } };
|
|
1683
|
+
const pm = await (0, Utils_1.prepareWAMessageMedia)(mi, { upload: waUploadToServer });
|
|
1684
|
+
messageContent.header = { title: title || '', hasMediaAttachment: true, imageMessage: pm.imageMessage };
|
|
1685
|
+
}
|
|
1686
|
+
else if (video) {
|
|
1687
|
+
const mi = Buffer.isBuffer(video)
|
|
1688
|
+
? { video }
|
|
1689
|
+
: { video: { url: typeof video === 'object' ? video.url : video } };
|
|
1690
|
+
const pm = await (0, Utils_1.prepareWAMessageMedia)(mi, { upload: waUploadToServer });
|
|
1691
|
+
messageContent.header = { title: title || '', hasMediaAttachment: true, videoMessage: pm.videoMessage };
|
|
1692
|
+
}
|
|
1693
|
+
else if (document) {
|
|
1694
|
+
const mi = Buffer.isBuffer(document)
|
|
1695
|
+
? { document }
|
|
1696
|
+
: { document: { url: typeof document === 'object' ? document.url : document } };
|
|
1697
|
+
if (mimetype && typeof mi.document === 'object')
|
|
1698
|
+
mi.document.mimetype = mimetype;
|
|
1699
|
+
if (jpegThumbnail) {
|
|
1700
|
+
const thumb = Buffer.isBuffer(jpegThumbnail)
|
|
1701
|
+
? jpegThumbnail
|
|
1702
|
+
: await (async () => {
|
|
1703
|
+
try {
|
|
1704
|
+
const r = await fetch(jpegThumbnail);
|
|
1705
|
+
return Buffer.from(await r.arrayBuffer());
|
|
1706
|
+
}
|
|
1707
|
+
catch {
|
|
1708
|
+
return undefined;
|
|
1709
|
+
}
|
|
1710
|
+
})();
|
|
1711
|
+
if (thumb)
|
|
1712
|
+
mi.document.jpegThumbnail = thumb;
|
|
1713
|
+
}
|
|
1714
|
+
const pm = await (0, Utils_1.prepareWAMessageMedia)(mi, { upload: waUploadToServer });
|
|
1715
|
+
messageContent.header = { title: title || '', hasMediaAttachment: true, documentMessage: pm.documentMessage };
|
|
1716
|
+
}
|
|
1717
|
+
else if (location && typeof location === 'object') {
|
|
1718
|
+
messageContent.header = {
|
|
1719
|
+
title: title || location.name || 'Location',
|
|
1720
|
+
hasMediaAttachment: false,
|
|
1721
|
+
locationMessage: {
|
|
1722
|
+
degreesLatitude: location.degreesLatitude || location.degressLatitude || 0,
|
|
1723
|
+
degreesLongitude: location.degreesLongitude || location.degressLongitude || 0,
|
|
1724
|
+
name: location.name || '',
|
|
1725
|
+
address: location.address || ''
|
|
1726
|
+
}
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
else if (product && typeof product === 'object') {
|
|
1730
|
+
let productImageMessage = null;
|
|
1731
|
+
if (product.productImage) {
|
|
1732
|
+
const mi = Buffer.isBuffer(product.productImage)
|
|
1733
|
+
? { image: product.productImage }
|
|
1734
|
+
: {
|
|
1735
|
+
image: {
|
|
1736
|
+
url: typeof product.productImage === 'object' ? product.productImage.url : product.productImage
|
|
1737
|
+
}
|
|
1738
|
+
};
|
|
1739
|
+
const pm = await (0, Utils_1.prepareWAMessageMedia)(mi, { upload: waUploadToServer });
|
|
1740
|
+
productImageMessage = pm.imageMessage;
|
|
1741
|
+
}
|
|
1742
|
+
messageContent.header = {
|
|
1743
|
+
title: title || product.title || 'Product',
|
|
1744
|
+
hasMediaAttachment: false,
|
|
1745
|
+
productMessage: {
|
|
1746
|
+
product: {
|
|
1747
|
+
productImage: productImageMessage,
|
|
1748
|
+
productId: product.productId || '',
|
|
1749
|
+
title: product.title || '',
|
|
1750
|
+
description: product.description || '',
|
|
1751
|
+
currencyCode: product.currencyCode || 'USD',
|
|
1752
|
+
priceAmount1000: parseInt(product.priceAmount1000, 10) || 0,
|
|
1753
|
+
retailerId: product.retailerId || '',
|
|
1754
|
+
url: product.url || '',
|
|
1755
|
+
productImageCount: product.productImageCount || 1
|
|
1756
|
+
},
|
|
1757
|
+
businessOwnerJid: businessOwnerJid || product.businessOwnerJid || userJid
|
|
1758
|
+
}
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
else if (title) {
|
|
1762
|
+
messageContent.header = { title, hasMediaAttachment: false };
|
|
1763
|
+
}
|
|
1764
|
+
const hasMedia = !!(image || video || document || location || product);
|
|
1765
|
+
const bodyText = hasMedia ? caption : text || caption;
|
|
1766
|
+
if (bodyText)
|
|
1767
|
+
messageContent.body = { text: bodyText };
|
|
1768
|
+
if (footer)
|
|
1769
|
+
messageContent.footer = { text: footer };
|
|
1770
|
+
messageContent.nativeFlowMessage = { buttons: processedButtons };
|
|
1771
|
+
if (externalAdReply && typeof externalAdReply === 'object') {
|
|
1772
|
+
messageContent.contextInfo = {
|
|
1773
|
+
externalAdReply: {
|
|
1774
|
+
title: externalAdReply.title || '',
|
|
1775
|
+
body: externalAdReply.body || '',
|
|
1776
|
+
mediaType: externalAdReply.mediaType || 1,
|
|
1777
|
+
sourceUrl: externalAdReply.sourceUrl || externalAdReply.url || '',
|
|
1778
|
+
thumbnailUrl: externalAdReply.thumbnailUrl || externalAdReply.thumbnail || '',
|
|
1779
|
+
renderLargerThumbnail: externalAdReply.renderLargerThumbnail || false,
|
|
1780
|
+
showAdAttribution: externalAdReply.showAdAttribution !== false,
|
|
1781
|
+
containsAutoReply: externalAdReply.containsAutoReply || false,
|
|
1782
|
+
...(externalAdReply.mediaUrl && { mediaUrl: externalAdReply.mediaUrl }),
|
|
1783
|
+
...(Buffer.isBuffer(externalAdReply.thumbnail) && { thumbnail: externalAdReply.thumbnail }),
|
|
1784
|
+
...(externalAdReply.jpegThumbnail && { jpegThumbnail: externalAdReply.jpegThumbnail })
|
|
1785
|
+
},
|
|
1786
|
+
...(options.mentionedJid && { mentionedJid: options.mentionedJid })
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
else if (options.mentionedJid) {
|
|
1790
|
+
messageContent.contextInfo = { mentionedJid: options.mentionedJid };
|
|
1791
|
+
}
|
|
1792
|
+
const payload = index_js_1.proto.Message.InteractiveMessage.create(messageContent);
|
|
1793
|
+
const msg = (0, Utils_1.generateWAMessageFromContent)(jid, { viewOnceMessage: { message: { interactiveMessage: payload } } }, { userJid, quoted: options?.quoted || null });
|
|
1794
|
+
const additionalNodes = [
|
|
1795
|
+
{
|
|
1796
|
+
tag: 'biz',
|
|
1797
|
+
attrs: {},
|
|
1798
|
+
content: [
|
|
1799
|
+
{
|
|
1800
|
+
tag: 'interactive',
|
|
1801
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
1802
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }]
|
|
1803
|
+
}
|
|
1804
|
+
]
|
|
1805
|
+
}
|
|
1806
|
+
];
|
|
1807
|
+
await relayMessage(jid, msg.message, { messageId: msg.key.id, additionalNodes });
|
|
1808
|
+
return msg;
|
|
1809
|
+
}
|
|
1810
|
+
if (typeof content === 'object' &&
|
|
1811
|
+
'disappearingMessagesInChat' in content &&
|
|
1812
|
+
typeof content['disappearingMessagesInChat'] !== 'undefined' &&
|
|
1813
|
+
(0, WABinary_1.isJidGroup)(jid)) {
|
|
1814
|
+
const { disappearingMessagesInChat } = content;
|
|
1815
|
+
const value = typeof disappearingMessagesInChat === 'boolean'
|
|
1816
|
+
? disappearingMessagesInChat
|
|
1817
|
+
? WA_DEFAULT_EPHEMERAL
|
|
1818
|
+
: 0
|
|
1819
|
+
: disappearingMessagesInChat;
|
|
1820
|
+
await groupToggleEphemeral(jid, value);
|
|
1821
|
+
}
|
|
1822
|
+
else if (badzzne2.detectType(content)) {
|
|
1823
|
+
const { quoted } = options;
|
|
1824
|
+
const messageType = badzzne2.detectType(content);
|
|
1825
|
+
switch (messageType) {
|
|
1826
|
+
case 'PAYMENT': {
|
|
1827
|
+
const paymentContent = await badzzne2.handlePayment(content, quoted);
|
|
1828
|
+
return await relayMessage(jid, paymentContent, {
|
|
1829
|
+
messageId: (0, Utils_1.generateMessageIDV2)(userJid)
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
case 'PRODUCT': {
|
|
1833
|
+
const productContent = await badzzne2.handleProduct(content, jid, quoted);
|
|
1834
|
+
const productMsg = await (0, Utils_1.generateWAMessageFromContent)(jid, productContent, { quoted, userJid });
|
|
1835
|
+
return await relayMessage(jid, productMsg.message, {
|
|
1836
|
+
messageId: productMsg.key.id
|
|
1837
|
+
});
|
|
1838
|
+
}
|
|
1839
|
+
case 'INTERACTIVE': {
|
|
1840
|
+
const interactiveContent = await badzzne2.handleInteractive(content, jid, quoted);
|
|
1841
|
+
const interactiveMsg = await (0, Utils_1.generateWAMessageFromContent)(jid, interactiveContent, {
|
|
1842
|
+
quoted,
|
|
1843
|
+
userJid
|
|
1844
|
+
});
|
|
1845
|
+
return await relayMessage(jid, interactiveMsg.message, {
|
|
1846
|
+
messageId: interactiveMsg.key.id
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
case 'INTERACTIVE_BUTTONS': {
|
|
1850
|
+
const ibContent = await badzzne2.handleInteractiveButtons(content, jid, quoted);
|
|
1851
|
+
const ibMsg = await (0, Utils_1.generateWAMessageFromContent)(jid, ibContent, { quoted, userJid });
|
|
1852
|
+
return await relayMessage(jid, ibMsg.message, {
|
|
1853
|
+
messageId: ibMsg.key.id
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
case 'ALBUM':
|
|
1857
|
+
return await badzzne2.handleAlbum(content, jid, quoted);
|
|
1858
|
+
case 'EVENT':
|
|
1859
|
+
return await badzzne2.handleEvent(content, jid, quoted);
|
|
1860
|
+
case 'POLL_RESULT':
|
|
1861
|
+
return await badzzne2.handlePollResult(content, jid, quoted);
|
|
1862
|
+
case 'GROUP_STORY':
|
|
1863
|
+
return await badzzne2.handleGroupStory(content, jid, quoted);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
else {
|
|
1867
|
+
let mediaHandle;
|
|
1868
|
+
const { ai, ...optionsWithoutAI } = options;
|
|
1869
|
+
const fullMsg = await (0, Utils_1.generateWAMessage)(jid, content, {
|
|
1870
|
+
logger,
|
|
1871
|
+
userJid,
|
|
1872
|
+
getUrlInfo: text => (0, link_preview_1.getUrlInfo)(text, {
|
|
1873
|
+
thumbnailWidth: linkPreviewImageThumbnailWidth,
|
|
1874
|
+
fetchOpts: {
|
|
1875
|
+
timeout: 3000,
|
|
1876
|
+
...(httpRequestOptions || {})
|
|
1877
|
+
},
|
|
1878
|
+
logger,
|
|
1879
|
+
uploadImage: generateHighQualityLinkPreview ? waUploadToServer : undefined
|
|
1880
|
+
}),
|
|
1881
|
+
getProfilePicUrl: sock.profilePictureUrl,
|
|
1882
|
+
getCallLink: sock.createCallLink,
|
|
1883
|
+
newsletter: (0, WABinary_1.isJidNewsletter)(jid),
|
|
1884
|
+
upload: async (encFilePath, opts) => {
|
|
1885
|
+
const up = await waUploadToServer(encFilePath, {
|
|
1886
|
+
...opts,
|
|
1887
|
+
newsletter: (0, WABinary_1.isJidNewsletter)(jid)
|
|
1888
|
+
});
|
|
1889
|
+
mediaHandle = up.handle;
|
|
1890
|
+
return up;
|
|
1891
|
+
},
|
|
1892
|
+
mediaCache: config.mediaCache,
|
|
1893
|
+
options: config.options,
|
|
1894
|
+
messageId: (0, Utils_1.generateMessageIDV2)(sock.user?.id),
|
|
1895
|
+
mediaAbProps: authState.creds.mediaAbProps,
|
|
1896
|
+
...optionsWithoutAI
|
|
1897
|
+
});
|
|
1898
|
+
if (!mediaHandle) {
|
|
1899
|
+
const msgContent = fullMsg.message;
|
|
1900
|
+
const msgTypes = ['audioMessage', 'imageMessage', 'videoMessage', 'documentMessage', 'stickerMessage'];
|
|
1901
|
+
for (const t of msgTypes) {
|
|
1902
|
+
if (msgContent?.[t]?._uploadHandle) {
|
|
1903
|
+
mediaHandle = msgContent[t]._uploadHandle;
|
|
1904
|
+
delete msgContent[t]._uploadHandle;
|
|
1905
|
+
break;
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
const isEventMsg = 'event' in content && !!content.event;
|
|
1910
|
+
const isDeleteMsg = 'delete' in content && !!content.delete;
|
|
1911
|
+
const isEditMsg = 'edit' in content && !!content.edit;
|
|
1912
|
+
const isPinMsg = 'pin' in content && !!content.pin;
|
|
1913
|
+
const isPollMessage = 'poll' in content && !!content.poll;
|
|
1914
|
+
const additionalAttributes = {};
|
|
1915
|
+
const additionalNodes = [];
|
|
1916
|
+
if (isDeleteMsg) {
|
|
1917
|
+
if ((0, WABinary_1.isJidGroup)(content.delete?.remoteJid) && !content.delete?.fromMe) {
|
|
1918
|
+
additionalAttributes.edit = '8';
|
|
1919
|
+
}
|
|
1920
|
+
else {
|
|
1921
|
+
additionalAttributes.edit = '7';
|
|
1922
|
+
}
|
|
1923
|
+
if (content.delete?.server_id) {
|
|
1924
|
+
additionalAttributes.server_id = String(content.delete.server_id);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
else if (isEditMsg) {
|
|
1928
|
+
additionalAttributes.edit = '1';
|
|
1929
|
+
}
|
|
1930
|
+
else if (isPinMsg) {
|
|
1931
|
+
additionalAttributes.edit = '2';
|
|
1932
|
+
}
|
|
1933
|
+
else if (isPollMessage) {
|
|
1934
|
+
additionalNodes.push({
|
|
1935
|
+
tag: 'meta',
|
|
1936
|
+
attrs: {
|
|
1937
|
+
polltype: 'creation'
|
|
1938
|
+
}
|
|
1939
|
+
});
|
|
1940
|
+
}
|
|
1941
|
+
else if (isEventMsg) {
|
|
1942
|
+
additionalNodes.push({
|
|
1943
|
+
tag: 'meta',
|
|
1944
|
+
attrs: {
|
|
1945
|
+
event_type: 'creation'
|
|
1946
|
+
}
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
const buttonType = getButtonType(fullMsg.message);
|
|
1950
|
+
if (content?.audio && options?.contextInfo) {
|
|
1951
|
+
const msgContent = fullMsg.message;
|
|
1952
|
+
if (msgContent?.audioMessage) {
|
|
1953
|
+
msgContent.audioMessage.contextInfo = options.contextInfo;
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
if (buttonType) {
|
|
1957
|
+
const btnNode = getButtonArgs(fullMsg.message);
|
|
1958
|
+
if (btnNode)
|
|
1959
|
+
additionalNodes.push(btnNode);
|
|
1960
|
+
}
|
|
1961
|
+
if (mediaHandle) {
|
|
1962
|
+
additionalAttributes['media_id'] = mediaHandle;
|
|
1963
|
+
}
|
|
1964
|
+
await relayMessage(jid, fullMsg.message, {
|
|
1965
|
+
messageId: fullMsg.key.id,
|
|
1966
|
+
useCachedGroupMetadata: options.useCachedGroupMetadata,
|
|
1967
|
+
additionalAttributes,
|
|
1968
|
+
statusJidList: options.statusJidList,
|
|
1969
|
+
statusPrivacy: options.statusPrivacy,
|
|
1970
|
+
additionalNodes,
|
|
1971
|
+
AI: ai || false
|
|
1972
|
+
});
|
|
1973
|
+
if (config.emitOwnEvents) {
|
|
1974
|
+
process.nextTick(async () => {
|
|
1975
|
+
await messageMutex.mutex(() => upsertMessage(fullMsg, 'append'));
|
|
1976
|
+
});
|
|
1977
|
+
}
|
|
1978
|
+
return fullMsg;
|
|
1979
|
+
}
|
|
1980
|
+
},
|
|
1981
|
+
sendMetaAI: async (jid1, text, opts = {}) => {
|
|
1982
|
+
const crypto = require('crypto');
|
|
1983
|
+
const { proto } = require('../../WAProto/index.js');
|
|
1984
|
+
const META_AI_BOT_JID = '867051314767696@bot';
|
|
1985
|
+
const yourJid = jid1 || '';
|
|
1986
|
+
const jid = opts.jid || META_AI_BOT_JID;
|
|
1987
|
+
const threadId = opts.threadId || Utils_1.generateMessageIDV2(yourJid);
|
|
1988
|
+
const now = Date.now();
|
|
1989
|
+
const senderTimestamp = opts.senderTimestamp || String(Math.floor(now / 1000));
|
|
1990
|
+
const messageSecret = opts.messageSecret || crypto.randomBytes(32);
|
|
1991
|
+
const senderKeyHash = opts.senderKeyHash || crypto.randomBytes(8).toString('base64');
|
|
1992
|
+
const message = {
|
|
1993
|
+
extendedTextMessage: proto.Message.ExtendedTextMessage.fromObject({
|
|
1994
|
+
text,
|
|
1995
|
+
previewType: 'NONE',
|
|
1996
|
+
contextInfo: proto.ContextInfo.fromObject({
|
|
1997
|
+
botMessageSharingInfo: {
|
|
1998
|
+
botEntryPointOrigin: 'FAVICON',
|
|
1999
|
+
forwardScore: 0
|
|
2000
|
+
}
|
|
2001
|
+
}),
|
|
2002
|
+
inviteLinkGroupTypeV2: 'DEFAULT'
|
|
2003
|
+
}),
|
|
2004
|
+
messageContextInfo: proto.MessageContextInfo.fromObject({
|
|
2005
|
+
deviceListMetadata: {
|
|
2006
|
+
senderKeyHash,
|
|
2007
|
+
senderTimestamp
|
|
2008
|
+
},
|
|
2009
|
+
deviceListMetadataVersion: 2,
|
|
2010
|
+
messageSecret,
|
|
2011
|
+
botMetadata: {
|
|
2012
|
+
botModeSelectionMetadata: {
|
|
2013
|
+
overrideMode: [0]
|
|
2014
|
+
},
|
|
2015
|
+
botThreadInfo: {
|
|
2016
|
+
serverInfo: { title: text.substring(0, 50) },
|
|
2017
|
+
clientInfo: { type: 'DEFAULT' }
|
|
2018
|
+
},
|
|
2019
|
+
botRenderingConfigMetadata: {
|
|
2020
|
+
bloksVersioningId: '1eb86e6f4117d052e6bab62fe758a2e2af43747b85c5c1a886c8262bac462ea4',
|
|
2021
|
+
pixelDensity: 2.625
|
|
2022
|
+
}
|
|
2023
|
+
},
|
|
2024
|
+
threadId: [
|
|
2025
|
+
{
|
|
2026
|
+
threadType: 'AI_THREAD',
|
|
2027
|
+
threadKey: {
|
|
2028
|
+
remoteJid: '0002@s.whatsapp.net',
|
|
2029
|
+
fromMe: true,
|
|
2030
|
+
id: threadId
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
]
|
|
2034
|
+
})
|
|
2035
|
+
};
|
|
2036
|
+
const msgId = (0, Utils_1.generateMessageIDV2)(yourJid);
|
|
2037
|
+
const messageOptions = {
|
|
2038
|
+
messageId: msgId,
|
|
2039
|
+
quoted: opts.quoted,
|
|
2040
|
+
links: opts.links
|
|
2041
|
+
};
|
|
2042
|
+
await relayMessage(jid, message, messageOptions);
|
|
2043
|
+
return msgId;
|
|
2044
|
+
}
|
|
2045
|
+
};
|
|
2046
|
+
};
|
|
2047
|
+
exports.makeMessagesSocket = makeMessagesSocket;
|