@prototypevip/baileys 0.0.1
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/LICENSE +23 -0
- package/WAProto/index.js +131118 -0
- package/engine-requirements.js +1 -0
- package/lib/Defaults/index.js +187 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +108 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +14 -0
- package/lib/Signal/Group/sender-chain-key.js +31 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +79 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +46 -0
- package/lib/Signal/Group/sender-key-state.js +104 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +532 -0
- package/lib/Signal/lid-mapping.js +312 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +64 -0
- package/lib/Socket/business.js +293 -0
- package/lib/Socket/chats.js +1124 -0
- package/lib/Socket/communities.js +528 -0
- package/lib/Socket/groups.js +465 -0
- package/lib/Socket/index.js +14 -0
- package/lib/Socket/messages-recv.js +2325 -0
- package/lib/Socket/messages-send.js +1746 -0
- package/lib/Socket/mex.js +55 -0
- package/lib/Socket/newsletter.js +317 -0
- package/lib/Socket/socket.js +1185 -0
- package/lib/Store/cache-factory.js +20 -0
- package/lib/Store/index.js +5 -0
- package/lib/Store/make-in-memory-store.js +523 -0
- package/lib/Store/make-ordered-dictionary.js +203 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Store/prototype-store.js +958 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Newsletter.js +36 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.js +25 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +78 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +33 -0
- package/lib/Utils/auth-utils.js +303 -0
- package/lib/Utils/browser-utils.js +32 -0
- package/lib/Utils/business.js +245 -0
- package/lib/Utils/chat-utils.js +971 -0
- package/lib/Utils/companion-reg-client-utils.js +54 -0
- package/lib/Utils/constants.js +24 -0
- package/lib/Utils/crypto.js +133 -0
- package/lib/Utils/cstoken-utils.js +41 -0
- package/lib/Utils/decode-wa-message.js +406 -0
- package/lib/Utils/event-buffer.js +616 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history.js +147 -0
- package/lib/Utils/identity-change-handler.js +63 -0
- package/lib/Utils/index.js +29 -0
- package/lib/Utils/interactive-message-handler.js +478 -0
- package/lib/Utils/link-preview.js +91 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.js +31 -0
- package/lib/Utils/message-retry-manager.js +291 -0
- package/lib/Utils/messages-media.js +881 -0
- package/lib/Utils/messages.js +1434 -0
- package/lib/Utils/noise-handler.js +229 -0
- package/lib/Utils/normalize-message-identity.js +73 -0
- package/lib/Utils/offline-node-processor.js +50 -0
- package/lib/Utils/pre-key-manager.js +119 -0
- package/lib/Utils/process-message.js +648 -0
- package/lib/Utils/reporting-utils.js +355 -0
- package/lib/Utils/rich-message-utils.js +264 -0
- package/lib/Utils/signal.js +233 -0
- package/lib/Utils/stanza-ack.js +33 -0
- package/lib/Utils/sticker-utils.js +221 -0
- package/lib/Utils/sync-action-utils.js +53 -0
- package/lib/Utils/tc-token-utils.js +182 -0
- package/lib/Utils/use-multi-file-auth-state.js +118 -0
- package/lib/Utils/use-prototype-auth-state.js +217 -0
- package/lib/Utils/use-single-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +1468 -0
- package/lib/WABinary/decode.js +258 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +202 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +94 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +20669 -0
- package/lib/WAM/encode.js +151 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +133 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +12 -0
- package/package.json +78 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { createHmac } from "crypto";
|
|
2
|
+
import { proto } from "../../WAProto/index.js";
|
|
3
|
+
import { hkdf } from "./crypto.js";
|
|
4
|
+
const reportingFields = [
|
|
5
|
+
{ f: 1 },
|
|
6
|
+
{
|
|
7
|
+
f: 3,
|
|
8
|
+
s: [
|
|
9
|
+
{ f: 2 },
|
|
10
|
+
{ f: 3 },
|
|
11
|
+
{ f: 8 },
|
|
12
|
+
{ f: 11 },
|
|
13
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
14
|
+
{ f: 25 },
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
{ f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
18
|
+
{
|
|
19
|
+
f: 5,
|
|
20
|
+
s: [
|
|
21
|
+
{ f: 3 },
|
|
22
|
+
{ f: 4 },
|
|
23
|
+
{ f: 5 },
|
|
24
|
+
{ f: 16 },
|
|
25
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
{ f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
|
|
29
|
+
{
|
|
30
|
+
f: 7,
|
|
31
|
+
s: [
|
|
32
|
+
{ f: 2 },
|
|
33
|
+
{ f: 7 },
|
|
34
|
+
{ f: 10 },
|
|
35
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
36
|
+
{ f: 20 },
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
f: 8,
|
|
41
|
+
s: [
|
|
42
|
+
{ f: 2 },
|
|
43
|
+
{ f: 7 },
|
|
44
|
+
{ f: 9 },
|
|
45
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
46
|
+
{ f: 21 },
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
f: 9,
|
|
51
|
+
s: [
|
|
52
|
+
{ f: 2 },
|
|
53
|
+
{ f: 6 },
|
|
54
|
+
{ f: 7 },
|
|
55
|
+
{ f: 13 },
|
|
56
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
57
|
+
{ f: 20 },
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
{ f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
|
|
61
|
+
{ f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
62
|
+
{
|
|
63
|
+
f: 26,
|
|
64
|
+
s: [
|
|
65
|
+
{ f: 4 },
|
|
66
|
+
{ f: 5 },
|
|
67
|
+
{ f: 8 },
|
|
68
|
+
{ f: 13 },
|
|
69
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
f: 28,
|
|
74
|
+
s: [
|
|
75
|
+
{ f: 1 },
|
|
76
|
+
{ f: 2 },
|
|
77
|
+
{ f: 4 },
|
|
78
|
+
{ f: 5 },
|
|
79
|
+
{ f: 6 },
|
|
80
|
+
{ f: 7, s: [{ f: 21 }, { f: 22 }] },
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
{ f: 37, s: [{ f: 1, m: true }] },
|
|
84
|
+
{
|
|
85
|
+
f: 49,
|
|
86
|
+
s: [
|
|
87
|
+
{ f: 2 },
|
|
88
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
89
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
90
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] },
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
{ f: 53, s: [{ f: 1, m: true }] },
|
|
94
|
+
{ f: 55, s: [{ f: 1, m: true }] },
|
|
95
|
+
{ f: 58, s: [{ f: 1, m: true }] },
|
|
96
|
+
{ f: 59, s: [{ f: 1, m: true }] },
|
|
97
|
+
{
|
|
98
|
+
f: 60,
|
|
99
|
+
s: [
|
|
100
|
+
{ f: 2 },
|
|
101
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
102
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
103
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] },
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
f: 64,
|
|
108
|
+
s: [
|
|
109
|
+
{ f: 2 },
|
|
110
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
111
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
112
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] },
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
f: 66,
|
|
117
|
+
s: [
|
|
118
|
+
{ f: 2 },
|
|
119
|
+
{ f: 6 },
|
|
120
|
+
{ f: 7 },
|
|
121
|
+
{ f: 13 },
|
|
122
|
+
{ f: 17, s: [{ f: 21 }, { f: 22 }] },
|
|
123
|
+
{ f: 20 },
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
{ f: 74, s: [{ f: 1, m: true }] },
|
|
127
|
+
{ f: 87, s: [{ f: 1, m: true }] },
|
|
128
|
+
{
|
|
129
|
+
f: 88,
|
|
130
|
+
s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }],
|
|
131
|
+
},
|
|
132
|
+
{ f: 92, s: [{ f: 1, m: true }] },
|
|
133
|
+
{ f: 93, s: [{ f: 1, m: true }] },
|
|
134
|
+
{ f: 94, s: [{ f: 1, m: true }] },
|
|
135
|
+
];
|
|
136
|
+
const compileReportingFields = (fields) => {
|
|
137
|
+
const map = new Map();
|
|
138
|
+
for (const f of fields) {
|
|
139
|
+
map.set(f.f, {
|
|
140
|
+
m: f.m,
|
|
141
|
+
children: f.s ? compileReportingFields(f.s) : undefined,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return map;
|
|
145
|
+
};
|
|
146
|
+
const compiledReportingFields = compileReportingFields(reportingFields);
|
|
147
|
+
const EMPTY_MAP = new Map();
|
|
148
|
+
const ENC_SECRET_REPORT_TOKEN = "Report Token";
|
|
149
|
+
const WIRE = {
|
|
150
|
+
VARINT: 0,
|
|
151
|
+
FIXED64: 1,
|
|
152
|
+
BYTES: 2,
|
|
153
|
+
FIXED32: 5,
|
|
154
|
+
};
|
|
155
|
+
export const shouldIncludeReportingToken = (message) =>
|
|
156
|
+
!message.reactionMessage &&
|
|
157
|
+
!message.encReactionMessage &&
|
|
158
|
+
!message.encEventResponseMessage &&
|
|
159
|
+
!message.pollUpdateMessage;
|
|
160
|
+
const generateMsgSecretKey = (
|
|
161
|
+
modificationType,
|
|
162
|
+
origMsgId,
|
|
163
|
+
origMsgSender,
|
|
164
|
+
modificationSender,
|
|
165
|
+
origMsgSecret,
|
|
166
|
+
) => {
|
|
167
|
+
const useCaseSecret = Buffer.concat([
|
|
168
|
+
Buffer.from(origMsgId, "utf8"),
|
|
169
|
+
Buffer.from(origMsgSender, "utf8"),
|
|
170
|
+
Buffer.from(modificationSender, "utf8"),
|
|
171
|
+
Buffer.from(modificationType, "utf8"),
|
|
172
|
+
]);
|
|
173
|
+
return hkdf(origMsgSecret, 32, { info: useCaseSecret.toString("latin1") });
|
|
174
|
+
};
|
|
175
|
+
const extractReportingTokenContent = (data, cfg) => {
|
|
176
|
+
const out = [];
|
|
177
|
+
let i = 0;
|
|
178
|
+
while (i < data.length) {
|
|
179
|
+
const tag = decodeVarint(data, i);
|
|
180
|
+
if (!tag.ok) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const fieldNum = tag.value >> 3;
|
|
184
|
+
const wireType = tag.value & 0x7;
|
|
185
|
+
const fieldStart = i;
|
|
186
|
+
i += tag.bytes;
|
|
187
|
+
const fieldCfg = cfg.get(fieldNum);
|
|
188
|
+
const pushSlice = (end) => {
|
|
189
|
+
if (end > data.length) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
|
|
193
|
+
i = end;
|
|
194
|
+
return true;
|
|
195
|
+
};
|
|
196
|
+
const skip = (end) => {
|
|
197
|
+
if (end > data.length) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
i = end;
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
203
|
+
if (wireType === WIRE.VARINT) {
|
|
204
|
+
const v = decodeVarint(data, i);
|
|
205
|
+
if (!v.ok) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
const end = i + v.bytes;
|
|
209
|
+
if (!fieldCfg) {
|
|
210
|
+
if (!skip(end)) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!pushSlice(end)) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (wireType === WIRE.FIXED64) {
|
|
221
|
+
const end = i + 8;
|
|
222
|
+
if (!fieldCfg) {
|
|
223
|
+
if (!skip(end)) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (!pushSlice(end)) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (wireType === WIRE.FIXED32) {
|
|
234
|
+
const end = i + 4;
|
|
235
|
+
if (!fieldCfg) {
|
|
236
|
+
if (!skip(end)) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (!pushSlice(end)) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (wireType === WIRE.BYTES) {
|
|
247
|
+
const len = decodeVarint(data, i);
|
|
248
|
+
if (!len.ok) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
const valStart = i + len.bytes;
|
|
252
|
+
const valEnd = valStart + len.value;
|
|
253
|
+
if (valEnd > data.length) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
if (!fieldCfg) {
|
|
257
|
+
i = valEnd;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (fieldCfg.m || fieldCfg.children) {
|
|
261
|
+
const sub = extractReportingTokenContent(
|
|
262
|
+
data.subarray(valStart, valEnd),
|
|
263
|
+
fieldCfg.children ?? EMPTY_MAP,
|
|
264
|
+
);
|
|
265
|
+
if (sub === null) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
if (sub.length > 0) {
|
|
269
|
+
const newTag = encodeVarint(tag.value);
|
|
270
|
+
const newLen = encodeVarint(sub.length);
|
|
271
|
+
out.push({
|
|
272
|
+
num: fieldNum,
|
|
273
|
+
bytes: Buffer.concat([newTag, newLen, sub]),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
i = valEnd;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
|
|
280
|
+
i = valEnd;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
if (out.length === 0) {
|
|
286
|
+
return Buffer.alloc(0);
|
|
287
|
+
}
|
|
288
|
+
out.sort((a, b) => a.num - b.num);
|
|
289
|
+
return Buffer.concat(out.map((f) => f.bytes));
|
|
290
|
+
};
|
|
291
|
+
const decodeVarint = (buffer, offset) => {
|
|
292
|
+
let value = 0;
|
|
293
|
+
let bytes = 0;
|
|
294
|
+
let shift = 0;
|
|
295
|
+
while (offset + bytes < buffer.length) {
|
|
296
|
+
const current = buffer[offset + bytes];
|
|
297
|
+
value |= (current & 0x7f) << shift;
|
|
298
|
+
bytes++;
|
|
299
|
+
if ((current & 0x80) === 0) {
|
|
300
|
+
return { value, bytes, ok: true };
|
|
301
|
+
}
|
|
302
|
+
shift += 7;
|
|
303
|
+
if (shift > 35) {
|
|
304
|
+
return { value: 0, bytes: 0, ok: false };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return { value: 0, bytes: 0, ok: false };
|
|
308
|
+
};
|
|
309
|
+
const encodeVarint = (value) => {
|
|
310
|
+
const parts = [];
|
|
311
|
+
let remaining = value >>> 0;
|
|
312
|
+
while (remaining > 0x7f) {
|
|
313
|
+
parts.push((remaining & 0x7f) | 0x80);
|
|
314
|
+
remaining >>>= 7;
|
|
315
|
+
}
|
|
316
|
+
parts.push(remaining);
|
|
317
|
+
return Buffer.from(parts);
|
|
318
|
+
};
|
|
319
|
+
export const getMessageReportingToken = async (msgProtobuf, message, key) => {
|
|
320
|
+
const msgSecret = message.messageContextInfo?.messageSecret;
|
|
321
|
+
if (!msgSecret || !key.id) {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
|
|
325
|
+
const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
|
|
326
|
+
const reportingSecret = generateMsgSecretKey(
|
|
327
|
+
ENC_SECRET_REPORT_TOKEN,
|
|
328
|
+
key.id,
|
|
329
|
+
from,
|
|
330
|
+
to,
|
|
331
|
+
msgSecret,
|
|
332
|
+
);
|
|
333
|
+
const content = extractReportingTokenContent(
|
|
334
|
+
msgProtobuf,
|
|
335
|
+
compiledReportingFields,
|
|
336
|
+
);
|
|
337
|
+
if (!content || content.length === 0) {
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
const reportingToken = createHmac("sha256", reportingSecret)
|
|
341
|
+
.update(content)
|
|
342
|
+
.digest()
|
|
343
|
+
.subarray(0, 16);
|
|
344
|
+
return {
|
|
345
|
+
tag: "reporting",
|
|
346
|
+
attrs: {},
|
|
347
|
+
content: [
|
|
348
|
+
{
|
|
349
|
+
tag: "reporting_token",
|
|
350
|
+
attrs: { v: "2" },
|
|
351
|
+
content: reportingToken,
|
|
352
|
+
},
|
|
353
|
+
],
|
|
354
|
+
};
|
|
355
|
+
};
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* مضافة من مكتبة itsliaaa: دعم رسائل الـ Rich Response (بتاعة Meta AI) اللي
|
|
3
|
+
* بتدعم جداول وأكواد ملوّنة (code blocks) جوه رسالة تظهر كإنها متحوّلة/فورورد
|
|
4
|
+
* من بوت ميتا الرسمي.
|
|
5
|
+
*
|
|
6
|
+
* الكريدت الأصلي: itsliaaa/baileys — تم التعديل عشان يشتغل جوه @prototypevip/baileys
|
|
7
|
+
*/
|
|
8
|
+
import { getRandomValues, randomUUID } from "crypto";
|
|
9
|
+
import { DONATE_URL, LEXER_REGEX } from "../Defaults/index.js";
|
|
10
|
+
import { LANGUAGE_KEYWORDS } from "../WABinary/constants.js";
|
|
11
|
+
import { CodeHighlightType, RichSubMessageType } from "../Types/RichType.js";
|
|
12
|
+
import { proto } from "../../WAProto/index.js";
|
|
13
|
+
|
|
14
|
+
const NOOP = new Set([]);
|
|
15
|
+
|
|
16
|
+
export const tokenizeCode = (code, language = "javascript") => {
|
|
17
|
+
const keywords = LANGUAGE_KEYWORDS[language] || NOOP;
|
|
18
|
+
const blocks = [];
|
|
19
|
+
LEXER_REGEX.lastIndex = 0;
|
|
20
|
+
let match;
|
|
21
|
+
while ((match = LEXER_REGEX.exec(code)) !== null) {
|
|
22
|
+
if (match[1]) {
|
|
23
|
+
blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: match[1] });
|
|
24
|
+
} else if (match[2]) {
|
|
25
|
+
blocks.push({ highlightType: CodeHighlightType.STRING, codeContent: match[2] });
|
|
26
|
+
} else if (match[3]) {
|
|
27
|
+
blocks.push({
|
|
28
|
+
highlightType: keywords.has(match[3]) ? CodeHighlightType.KEYWORD : CodeHighlightType.METHOD,
|
|
29
|
+
codeContent: match[3],
|
|
30
|
+
});
|
|
31
|
+
} else if (match[4]) {
|
|
32
|
+
blocks.push({
|
|
33
|
+
highlightType: keywords.has(match[4]) ? CodeHighlightType.KEYWORD : CodeHighlightType.DEFAULT,
|
|
34
|
+
codeContent: match[4],
|
|
35
|
+
});
|
|
36
|
+
} else if (match[5]) {
|
|
37
|
+
blocks.push({ highlightType: CodeHighlightType.NUMBER, codeContent: match[5] });
|
|
38
|
+
} else {
|
|
39
|
+
blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: match[6] });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return blocks;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const toUnified = (submessages, uuid) => ({
|
|
46
|
+
response_id: uuid || randomUUID(),
|
|
47
|
+
sections: submessages.map((submessage) => {
|
|
48
|
+
switch (submessage.messageType) {
|
|
49
|
+
case RichSubMessageType.CODE: {
|
|
50
|
+
const codeMetadata = submessage.codeMetadata;
|
|
51
|
+
return {
|
|
52
|
+
view_model: {
|
|
53
|
+
primitive: {
|
|
54
|
+
language: codeMetadata.codeLanguage,
|
|
55
|
+
code_blocks: codeMetadata.codeBlocks.map((block) => ({
|
|
56
|
+
content: block.codeContent,
|
|
57
|
+
type: CodeHighlightType[block.highlightType],
|
|
58
|
+
})),
|
|
59
|
+
__typename: "GenAICodeUXPrimitive",
|
|
60
|
+
},
|
|
61
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
case RichSubMessageType.CONTENT_ITEMS:
|
|
66
|
+
return {};
|
|
67
|
+
case RichSubMessageType.INLINE_IMAGE:
|
|
68
|
+
return {};
|
|
69
|
+
case RichSubMessageType.LATEX:
|
|
70
|
+
return {};
|
|
71
|
+
case RichSubMessageType.TABLE: {
|
|
72
|
+
const tableMetadata = submessage.tableMetadata;
|
|
73
|
+
return {
|
|
74
|
+
view_model: {
|
|
75
|
+
primitive: {
|
|
76
|
+
title: tableMetadata.title,
|
|
77
|
+
rows: tableMetadata.rows.map((row) => ({
|
|
78
|
+
is_header: row.isHeading,
|
|
79
|
+
cells: row.items,
|
|
80
|
+
markdown_cells: row.items.map((item) => ({ text: item })),
|
|
81
|
+
})),
|
|
82
|
+
__typename: "GenATableUXPrimitive",
|
|
83
|
+
},
|
|
84
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
case RichSubMessageType.TEXT:
|
|
89
|
+
return {
|
|
90
|
+
view_model: {
|
|
91
|
+
primitive: {
|
|
92
|
+
text: submessage.messageText,
|
|
93
|
+
inline_entities: submessage.inlineEntities || [],
|
|
94
|
+
__typename: "GenAIMarkdownTextUXPrimitive",
|
|
95
|
+
},
|
|
96
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return submessage;
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* بناء رسالة Rich Response كاملة (جدول / كود ملوّن / نص) وتغليفها كإنها
|
|
106
|
+
* متحوّلة من بوت ميتا الرسمي.
|
|
107
|
+
* الحقول المدعومة: contentText, code + language, table + title, links, footerText, headerText, disclaimerText
|
|
108
|
+
* أو تمرير richResponse كمصفوفة sub-messages جاهزة مباشرة.
|
|
109
|
+
*/
|
|
110
|
+
export const prepareRichResponseMessage = (content) => {
|
|
111
|
+
const {
|
|
112
|
+
code,
|
|
113
|
+
contentText,
|
|
114
|
+
disclaimerText,
|
|
115
|
+
footerText,
|
|
116
|
+
headerText,
|
|
117
|
+
language,
|
|
118
|
+
links,
|
|
119
|
+
noHeading,
|
|
120
|
+
richResponse,
|
|
121
|
+
table,
|
|
122
|
+
title,
|
|
123
|
+
} = content;
|
|
124
|
+
let submessages = [];
|
|
125
|
+
if (Array.isArray(richResponse)) {
|
|
126
|
+
submessages = richResponse.map((submessage) => {
|
|
127
|
+
if (submessage.text) {
|
|
128
|
+
return {
|
|
129
|
+
messageType: RichSubMessageType.TEXT,
|
|
130
|
+
messageText: submessage.text,
|
|
131
|
+
inlineEntities: submessage.inlineEntities,
|
|
132
|
+
};
|
|
133
|
+
} else if (submessage.code) {
|
|
134
|
+
return {
|
|
135
|
+
messageType: RichSubMessageType.CODE,
|
|
136
|
+
codeMetadata: {
|
|
137
|
+
codeLanguage: submessage.language,
|
|
138
|
+
codeBlocks: submessage.code,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
} else if (submessage.table) {
|
|
142
|
+
return {
|
|
143
|
+
messageType: RichSubMessageType.TABLE,
|
|
144
|
+
tableMetadata: { title: submessage.title, rows: submessage.table },
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return submessage;
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
if (headerText) {
|
|
151
|
+
submessages.push({ messageType: RichSubMessageType.TEXT, messageText: headerText });
|
|
152
|
+
}
|
|
153
|
+
if (contentText) {
|
|
154
|
+
submessages.push({ messageType: RichSubMessageType.TEXT, messageText: contentText });
|
|
155
|
+
}
|
|
156
|
+
if (code) {
|
|
157
|
+
const lang = language || "javascript";
|
|
158
|
+
submessages.push({
|
|
159
|
+
messageType: RichSubMessageType.CODE,
|
|
160
|
+
codeMetadata: { codeLanguage: lang, codeBlocks: tokenizeCode(code, lang) },
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (links) {
|
|
164
|
+
links.forEach((linkField, index) => {
|
|
165
|
+
const prefix = "SS_" + index;
|
|
166
|
+
const url = linkField.url || DONATE_URL;
|
|
167
|
+
const sources = linkField.sources?.map((sourceField) => ({
|
|
168
|
+
source_type: "THIRD_PARTY",
|
|
169
|
+
source_display_name: sourceField.displayName || "",
|
|
170
|
+
source_subtitle: sourceField.subtitle || "",
|
|
171
|
+
source_url: sourceField.url || url,
|
|
172
|
+
}));
|
|
173
|
+
submessages.push({
|
|
174
|
+
messageType: RichSubMessageType.TEXT,
|
|
175
|
+
messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
|
|
176
|
+
inlineEntities: [
|
|
177
|
+
{
|
|
178
|
+
key: prefix,
|
|
179
|
+
metadata: {
|
|
180
|
+
reference_id: index + 1,
|
|
181
|
+
reference_url: url,
|
|
182
|
+
reference_title: linkField.title || "",
|
|
183
|
+
reference_display_name: linkField.displayName || "",
|
|
184
|
+
sources: sources || [],
|
|
185
|
+
__typename: "GenAISearchCitationItem",
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (table) {
|
|
193
|
+
submessages.push({
|
|
194
|
+
messageType: RichSubMessageType.TABLE,
|
|
195
|
+
tableMetadata: {
|
|
196
|
+
title,
|
|
197
|
+
rows: table.map((items, index) => ({
|
|
198
|
+
isHeading: !noHeading && index === 0,
|
|
199
|
+
items,
|
|
200
|
+
})),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
if (footerText) {
|
|
205
|
+
submessages.push({ messageType: RichSubMessageType.TEXT, messageText: footerText });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const uuid = randomUUID();
|
|
209
|
+
const unified = toUnified(submessages, uuid);
|
|
210
|
+
const richResponseMessage = proto.AIRichResponseMessage.create({
|
|
211
|
+
submessages,
|
|
212
|
+
messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
|
|
213
|
+
unifiedResponse: {
|
|
214
|
+
data: Buffer.from(JSON.stringify(unified)),
|
|
215
|
+
},
|
|
216
|
+
contextInfo: {
|
|
217
|
+
isForwarded: true,
|
|
218
|
+
forwardingScore: 1,
|
|
219
|
+
forwardedAiBotMessageInfo: { botJid: "867051314767696@bot" },
|
|
220
|
+
forwardOrigin: 4,
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
const message = wrapToBotForwardedMessage(richResponseMessage);
|
|
224
|
+
const botMetadata = message.messageContextInfo.botMetadata;
|
|
225
|
+
if (disclaimerText) {
|
|
226
|
+
botMetadata.messageDisclaimerText = disclaimerText;
|
|
227
|
+
}
|
|
228
|
+
botMetadata.botResponseId = uuid;
|
|
229
|
+
return message;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const botMetadataSignature = () => {
|
|
233
|
+
const signature = new Uint8Array(64);
|
|
234
|
+
getRandomValues(signature);
|
|
235
|
+
return signature;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export const botMetadataCertificate = (length = 685) => {
|
|
239
|
+
const certificate = new Uint8Array(length);
|
|
240
|
+
certificate[0] = 48;
|
|
241
|
+
certificate[1] = 130;
|
|
242
|
+
getRandomValues(certificate.subarray(2));
|
|
243
|
+
return certificate;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
export const wrapToBotForwardedMessage = (richResponseMessage) => ({
|
|
247
|
+
messageContextInfo: {
|
|
248
|
+
botMetadata: {
|
|
249
|
+
verificationMetadata: {
|
|
250
|
+
proofs: [
|
|
251
|
+
{
|
|
252
|
+
certificateChain: [botMetadataCertificate(), botMetadataCertificate(892)],
|
|
253
|
+
version: 1,
|
|
254
|
+
useCase: 1,
|
|
255
|
+
signature: botMetadataSignature(),
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
botForwardedMessage: {
|
|
262
|
+
message: { richResponseMessage },
|
|
263
|
+
},
|
|
264
|
+
});
|