@vinzsocket/baileys 2.1.0 → 2.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Signal/Group/group_cipher.d.ts +1 -0
- package/lib/Signal/Group/group_cipher.js +38 -28
- package/lib/Signal/Group/queue-job.d.ts +2 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Socket/groups.js +2 -0
- package/lib/Socket/messages-send.js +450 -496
- package/lib/Store/index.d.ts +4 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-cache-manager-store.d.ts +17 -0
- package/lib/Store/make-cache-manager-store.js +102 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +427 -0
- package/lib/Store/make-ordered-dictionary.d.ts +14 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Message.d.ts +12 -42
- package/lib/Utils/extended-messages.d.ts +73 -0
- package/lib/Utils/extended-messages.js +231 -0
- package/lib/Utils/index.d.ts +3 -0
- package/lib/Utils/index.js +3 -0
- package/lib/Utils/lid-resolver.d.ts +17 -0
- package/lib/Utils/lid-resolver.js +67 -0
- package/lib/Utils/mention.d.ts +27 -0
- package/lib/Utils/mention.js +134 -0
- package/lib/Utils/messages.js +44 -143
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.d.ts +1 -0
- package/lib/WAUSync/Protocols/index.js +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +1 -0
- package/lib/WAUSync/USyncQuery.js +5 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +9 -8
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type OrderedDictionary<T> = {
|
|
2
|
+
array: T[];
|
|
3
|
+
get: (id: string) => T | undefined;
|
|
4
|
+
upsert: (item: T, mode: 'append' | 'prepend') => void;
|
|
5
|
+
update: (item: T) => boolean;
|
|
6
|
+
remove: (item: T) => boolean;
|
|
7
|
+
updateAssign: (id: string, update: Partial<T>) => boolean;
|
|
8
|
+
clear: () => void;
|
|
9
|
+
filter: (contain: (item: T) => boolean) => void;
|
|
10
|
+
toJSON: () => T[];
|
|
11
|
+
fromJSON: (newItems: T[]) => void;
|
|
12
|
+
};
|
|
13
|
+
export declare function makeOrderedDictionary<T>(idGetter: (item: T) => string): OrderedDictionary<T>;
|
|
14
|
+
export default makeOrderedDictionary;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export function makeOrderedDictionary(idGetter) {
|
|
2
|
+
const array = [];
|
|
3
|
+
const dict = {};
|
|
4
|
+
const get = (id) => dict[id];
|
|
5
|
+
const update = (item) => {
|
|
6
|
+
const id = idGetter(item);
|
|
7
|
+
const idx = array.findIndex(i => idGetter(i) === id);
|
|
8
|
+
if (idx >= 0) {
|
|
9
|
+
array[idx] = item;
|
|
10
|
+
dict[id] = item;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
};
|
|
14
|
+
const upsert = (item, mode) => {
|
|
15
|
+
const id = idGetter(item);
|
|
16
|
+
if (get(id)) {
|
|
17
|
+
update(item);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
if (mode === 'append') {
|
|
21
|
+
array.push(item);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
array.splice(0, 0, item);
|
|
25
|
+
}
|
|
26
|
+
dict[id] = item;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const remove = (item) => {
|
|
30
|
+
const id = idGetter(item);
|
|
31
|
+
const idx = array.findIndex(i => idGetter(i) === id);
|
|
32
|
+
if (idx >= 0) {
|
|
33
|
+
array.splice(idx, 1);
|
|
34
|
+
delete dict[id];
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
array,
|
|
41
|
+
get,
|
|
42
|
+
upsert,
|
|
43
|
+
update,
|
|
44
|
+
remove,
|
|
45
|
+
updateAssign: (id, update) => {
|
|
46
|
+
const item = get(id);
|
|
47
|
+
if (item) {
|
|
48
|
+
Object.assign(item, update);
|
|
49
|
+
delete dict[id];
|
|
50
|
+
dict[idGetter(item)] = item;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
},
|
|
55
|
+
clear: () => {
|
|
56
|
+
array.splice(0, array.length);
|
|
57
|
+
Object.keys(dict).forEach(key => {
|
|
58
|
+
delete dict[key];
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
filter: (contain) => {
|
|
62
|
+
let i = 0;
|
|
63
|
+
while (i < array.length) {
|
|
64
|
+
if (!contain(array[i])) {
|
|
65
|
+
delete dict[idGetter(array[i])];
|
|
66
|
+
array.splice(i, 1);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
i += 1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
toJSON: () => array,
|
|
74
|
+
fromJSON: (newItems) => {
|
|
75
|
+
array.splice(0, array.length, ...newItems);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export default makeOrderedDictionary;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class ObjectRepository<T extends object> {
|
|
2
|
+
readonly entityMap: Map<string, T>;
|
|
3
|
+
constructor(entities?: Record<string, T>);
|
|
4
|
+
findById(id: string): T | undefined;
|
|
5
|
+
findAll(): T[];
|
|
6
|
+
upsertById(id: string, entity: T): Map<string, T>;
|
|
7
|
+
deleteById(id: string): boolean;
|
|
8
|
+
count(): number;
|
|
9
|
+
toJSON(): T[];
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class ObjectRepository {
|
|
2
|
+
constructor(entities = {}) {
|
|
3
|
+
this.entityMap = new Map(Object.entries(entities));
|
|
4
|
+
}
|
|
5
|
+
findById(id) {
|
|
6
|
+
return this.entityMap.get(id);
|
|
7
|
+
}
|
|
8
|
+
findAll() {
|
|
9
|
+
return Array.from(this.entityMap.values());
|
|
10
|
+
}
|
|
11
|
+
upsertById(id, entity) {
|
|
12
|
+
return this.entityMap.set(id, { ...entity });
|
|
13
|
+
}
|
|
14
|
+
deleteById(id) {
|
|
15
|
+
return this.entityMap.delete(id);
|
|
16
|
+
}
|
|
17
|
+
count() {
|
|
18
|
+
return this.entityMap.size;
|
|
19
|
+
}
|
|
20
|
+
toJSON() {
|
|
21
|
+
return this.findAll();
|
|
22
|
+
}
|
|
23
|
+
}
|
package/lib/Types/Message.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AxiosRequestConfig } from 'axios';
|
|
1
2
|
import type { Readable } from 'stream';
|
|
2
3
|
import type { URL } from 'url';
|
|
3
4
|
import { proto } from '../../WAProto/index.js';
|
|
@@ -8,20 +9,14 @@ import type { CacheStore } from './Socket.js';
|
|
|
8
9
|
export { proto as WAProto };
|
|
9
10
|
export type WAMessage = proto.IWebMessageInfo & {
|
|
10
11
|
key: WAMessageKey;
|
|
11
|
-
messageStubParameters?: any;
|
|
12
|
-
category?: string;
|
|
13
|
-
retryCount?: number;
|
|
14
12
|
};
|
|
15
13
|
export type WAMessageContent = proto.IMessage;
|
|
16
14
|
export type WAContactMessage = proto.Message.IContactMessage;
|
|
17
15
|
export type WAContactsArrayMessage = proto.Message.IContactsArrayMessage;
|
|
18
16
|
export type WAMessageKey = proto.IMessageKey & {
|
|
19
17
|
remoteJidAlt?: string;
|
|
20
|
-
remoteJidUsername?: string;
|
|
21
18
|
participantAlt?: string;
|
|
22
|
-
participantUsername?: string;
|
|
23
19
|
server_id?: string;
|
|
24
|
-
addressingMode?: string;
|
|
25
20
|
isViewOnce?: boolean;
|
|
26
21
|
};
|
|
27
22
|
export type WATextMessage = proto.Message.IExtendedTextMessage;
|
|
@@ -72,8 +67,6 @@ export interface WAUrlInfo {
|
|
|
72
67
|
type Mentionable = {
|
|
73
68
|
/** list of jids that are mentioned in the accompanying text */
|
|
74
69
|
mentions?: string[];
|
|
75
|
-
/** mention all */
|
|
76
|
-
mentionAll?: boolean;
|
|
77
70
|
};
|
|
78
71
|
type Contextable = {
|
|
79
72
|
/** add contextInfo to the message */
|
|
@@ -85,16 +78,6 @@ type ViewOnce = {
|
|
|
85
78
|
type Editable = {
|
|
86
79
|
edit?: WAMessageKey;
|
|
87
80
|
};
|
|
88
|
-
type Listable = {
|
|
89
|
-
/** Sections of the native list message */
|
|
90
|
-
sections?: proto.Message.ListMessage.ISection[];
|
|
91
|
-
/** Title of a list message */
|
|
92
|
-
title?: string;
|
|
93
|
-
/** Text of the button that opens the list (required) */
|
|
94
|
-
buttonText?: string;
|
|
95
|
-
/** Footer text shown under the list message */
|
|
96
|
-
footer?: string;
|
|
97
|
-
};
|
|
98
81
|
type WithDimensions = {
|
|
99
82
|
width?: number;
|
|
100
83
|
height?: number;
|
|
@@ -119,12 +102,6 @@ export type EventMessageOptions = {
|
|
|
119
102
|
extraGuestsAllowed?: boolean;
|
|
120
103
|
messageSecret?: Uint8Array<ArrayBufferLike>;
|
|
121
104
|
};
|
|
122
|
-
export type AlbumMessageOptions = {
|
|
123
|
-
/** Number of images expected in the album */
|
|
124
|
-
expectedImageCount?: number;
|
|
125
|
-
/** Number of videos expected in the album */
|
|
126
|
-
expectedVideoCount?: number;
|
|
127
|
-
};
|
|
128
105
|
type SharePhoneNumber = {
|
|
129
106
|
sharePhoneNumber: boolean;
|
|
130
107
|
};
|
|
@@ -158,10 +135,7 @@ export type AnyMediaMessageContent = (({
|
|
|
158
135
|
caption?: string;
|
|
159
136
|
} & Contextable)) & {
|
|
160
137
|
mimetype?: string;
|
|
161
|
-
} & Editable
|
|
162
|
-
/** key of the parent albumMessage to associate this media with */
|
|
163
|
-
albumParentKey?: WAMessageKey;
|
|
164
|
-
};
|
|
138
|
+
} & Editable;
|
|
165
139
|
export type ButtonReplyInfo = {
|
|
166
140
|
displayText: string;
|
|
167
141
|
id: string;
|
|
@@ -180,13 +154,11 @@ export type WASendableProduct = Omit<proto.Message.ProductMessage.IProductSnapsh
|
|
|
180
154
|
export type AnyRegularMessageContent = (({
|
|
181
155
|
text: string;
|
|
182
156
|
linkPreview?: WAUrlInfo | null;
|
|
183
|
-
} & Mentionable & Contextable & Editable
|
|
157
|
+
} & Mentionable & Contextable & Editable) | AnyMediaMessageContent | {
|
|
184
158
|
event: EventMessageOptions;
|
|
185
159
|
} | ({
|
|
186
160
|
poll: PollMessageOptions;
|
|
187
|
-
} & Mentionable & Contextable & Editable) |
|
|
188
|
-
album: AlbumMessageOptions;
|
|
189
|
-
} & Contextable & Mentionable) | {
|
|
161
|
+
} & Mentionable & Contextable & Editable) | {
|
|
190
162
|
contacts: {
|
|
191
163
|
displayName?: string;
|
|
192
164
|
contacts: proto.Message.IContactMessage[];
|
|
@@ -223,9 +195,7 @@ export type AnyMessageContent = AnyRegularMessageContent | {
|
|
|
223
195
|
delete: WAMessageKey;
|
|
224
196
|
} | {
|
|
225
197
|
disappearingMessagesInChat: boolean | number;
|
|
226
|
-
} |
|
|
227
|
-
limitSharing: boolean;
|
|
228
|
-
};
|
|
198
|
+
} | import('../Utils/extended-messages.js').RequestPaymentMessageContent | import('../Utils/extended-messages.js').InteractiveMessageContent | import('../Utils/extended-messages.js').AlbumMessageContent | import('../Utils/extended-messages.js').PollResultMessageContent | import('../Utils/extended-messages.js').GroupStoryMessageContent;
|
|
229
199
|
export type GroupMetadataParticipants = Pick<GroupMetadata, 'participants'>;
|
|
230
200
|
type MinimalRelayOptions = {
|
|
231
201
|
/** override the message ID with a custom provided string */
|
|
@@ -288,7 +258,7 @@ export type MediaGenerationOptions = {
|
|
|
288
258
|
/** cache media so it does not have to be uploaded again */
|
|
289
259
|
mediaCache?: CacheStore;
|
|
290
260
|
mediaUploadTimeoutMs?: number;
|
|
291
|
-
options?:
|
|
261
|
+
options?: AxiosRequestConfig;
|
|
292
262
|
backgroundColor?: string;
|
|
293
263
|
font?: number;
|
|
294
264
|
};
|
|
@@ -310,7 +280,7 @@ export type MessageUpsertType = 'append' | 'notify';
|
|
|
310
280
|
export type MessageUserReceipt = proto.IUserReceipt;
|
|
311
281
|
export type WAMessageUpdate = {
|
|
312
282
|
update: Partial<WAMessage>;
|
|
313
|
-
key:
|
|
283
|
+
key: proto.IMessageKey;
|
|
314
284
|
};
|
|
315
285
|
export type WAMessageCursor = {
|
|
316
286
|
before: WAMessageKey | undefined;
|
|
@@ -318,13 +288,13 @@ export type WAMessageCursor = {
|
|
|
318
288
|
after: WAMessageKey | undefined;
|
|
319
289
|
};
|
|
320
290
|
export type MessageUserReceiptUpdate = {
|
|
321
|
-
key:
|
|
291
|
+
key: proto.IMessageKey;
|
|
322
292
|
receipt: MessageUserReceipt;
|
|
323
293
|
};
|
|
324
294
|
export type MediaDecryptionKeyInfo = {
|
|
325
|
-
iv:
|
|
326
|
-
cipherKey:
|
|
327
|
-
macKey?:
|
|
295
|
+
iv: Buffer;
|
|
296
|
+
cipherKey: Buffer;
|
|
297
|
+
macKey?: Buffer;
|
|
328
298
|
};
|
|
329
|
-
export type MinimalMessage = Pick<
|
|
299
|
+
export type MinimalMessage = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>;
|
|
330
300
|
//# sourceMappingURL=Message.d.ts.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import type { WAMediaUpload, WAMessage, WAMessageKey } from '../Types/Message.js';
|
|
3
|
+
export type ExtendedMessageType = 'PAYMENT' | 'INTERACTIVE' | 'ALBUM' | 'POLL_RESULT' | 'GROUP_STORY';
|
|
4
|
+
export declare function detectExtendedMessageType(content: unknown): ExtendedMessageType | null;
|
|
5
|
+
export type RequestPaymentMessageContent = {
|
|
6
|
+
requestPaymentMessage: {
|
|
7
|
+
amount?: number;
|
|
8
|
+
currency?: string;
|
|
9
|
+
expiry?: number;
|
|
10
|
+
from?: string;
|
|
11
|
+
note?: string;
|
|
12
|
+
sticker?: {
|
|
13
|
+
stickerMessage: proto.Message.IStickerMessage;
|
|
14
|
+
};
|
|
15
|
+
background?: proto.Message.RequestPaymentMessage.IPaymentBackground;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export type InteractiveMessageContent = {
|
|
19
|
+
interactiveMessage: {
|
|
20
|
+
title?: string;
|
|
21
|
+
footer?: string;
|
|
22
|
+
thumbnail?: WAMediaUpload;
|
|
23
|
+
image?: WAMediaUpload;
|
|
24
|
+
video?: WAMediaUpload;
|
|
25
|
+
document?: WAMediaUpload;
|
|
26
|
+
mimetype?: string;
|
|
27
|
+
fileName?: string;
|
|
28
|
+
header?: string;
|
|
29
|
+
contextInfo?: proto.IContextInfo;
|
|
30
|
+
externalAdReply?: proto.IContextInfo['externalAdReply'];
|
|
31
|
+
buttons?: proto.Message.InteractiveMessage.NativeFlowMessage.INativeFlowButton[];
|
|
32
|
+
nativeFlowMessage?: proto.Message.InteractiveMessage.INativeFlowMessage;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export type AlbumMessageContent = {
|
|
36
|
+
albumMessage: AnyRegularAlbumItem[];
|
|
37
|
+
};
|
|
38
|
+
type AnyRegularAlbumItem = Record<string, unknown>;
|
|
39
|
+
export type PollResultMessageContent = {
|
|
40
|
+
pollResultMessage: {
|
|
41
|
+
name: string;
|
|
42
|
+
pollVotes: Array<{
|
|
43
|
+
optionName: string;
|
|
44
|
+
optionVoteCount: number | string;
|
|
45
|
+
}>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export type GroupStoryMessageContent = {
|
|
49
|
+
groupStatusMessage: {
|
|
50
|
+
message?: proto.IMessage;
|
|
51
|
+
} | Record<string, unknown>;
|
|
52
|
+
};
|
|
53
|
+
export type ExtendedMessageContent = RequestPaymentMessageContent | InteractiveMessageContent | AlbumMessageContent | PollResultMessageContent | GroupStoryMessageContent;
|
|
54
|
+
export type ExtendedMessageHandlerDeps = {
|
|
55
|
+
generateWAMessage: (jid: string, content: unknown, options: Record<string, unknown>) => Promise<WAMessage>;
|
|
56
|
+
generateWAMessageContent: (content: unknown, options: Record<string, unknown>) => Promise<proto.IMessage>;
|
|
57
|
+
generateWAMessageFromContent: (jid: string, message: proto.IMessage, options: Record<string, unknown>) => WAMessage;
|
|
58
|
+
generateMessageID: () => string;
|
|
59
|
+
prepareWAMessageMedia: (message: unknown, options: Record<string, unknown>) => Promise<proto.IMessage>;
|
|
60
|
+
waUploadToServer: (...args: unknown[]) => unknown;
|
|
61
|
+
relayMessage: (jid: string, message: proto.IMessage, options: Record<string, unknown>) => Promise<string | void>;
|
|
62
|
+
};
|
|
63
|
+
export declare function makeExtendedMessageHandlers(deps: ExtendedMessageHandlerDeps): {
|
|
64
|
+
handlePayment: (content: RequestPaymentMessageContent, quoted?: WAMessage) => Promise<proto.IMessage>;
|
|
65
|
+
buildInteractiveContent: (content: InteractiveMessageContent, options?: {
|
|
66
|
+
mentions?: string[];
|
|
67
|
+
}) => Promise<proto.IMessage>;
|
|
68
|
+
sendAlbumMessage: (jid: string, content: AlbumMessageContent, quoted?: WAMessage) => Promise<WAMessage>;
|
|
69
|
+
sendPollResultMessage: (jid: string, content: PollResultMessageContent, quoted?: WAMessage) => Promise<WAMessage>;
|
|
70
|
+
sendGroupStoryMessage: (jid: string, content: GroupStoryMessageContent, quoted?: WAMessage) => Promise<{
|
|
71
|
+
key: WAMessageKey;
|
|
72
|
+
} | void>;
|
|
73
|
+
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { randomBytes } from 'crypto';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { normalizeMentions } from './mention.js';
|
|
4
|
+
/**
|
|
5
|
+
* Detects which "extended" message type a sendMessage() content object is
|
|
6
|
+
* asking for, if any. These are content shapes that need custom assembly
|
|
7
|
+
* instead of the regular generateWAMessage() path (raw payment requests,
|
|
8
|
+
* interactive messages with a rich media header, multi-image/video albums,
|
|
9
|
+
* poll result snapshots, and group status/story posts).
|
|
10
|
+
*/
|
|
11
|
+
export function detectExtendedMessageType(content) {
|
|
12
|
+
if (!content || typeof content !== 'object')
|
|
13
|
+
return null;
|
|
14
|
+
if (content.requestPaymentMessage)
|
|
15
|
+
return 'PAYMENT';
|
|
16
|
+
if (content.interactiveMessage)
|
|
17
|
+
return 'INTERACTIVE';
|
|
18
|
+
if (content.albumMessage)
|
|
19
|
+
return 'ALBUM';
|
|
20
|
+
if (content.pollResultMessage)
|
|
21
|
+
return 'POLL_RESULT';
|
|
22
|
+
if (content.groupStatusMessage)
|
|
23
|
+
return 'GROUP_STORY';
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates the handler set for the extended message types. All the heavy
|
|
28
|
+
* lifting (media upload, relaying, WAMessage construction) is delegated to
|
|
29
|
+
* the bound functions passed in, which is why this factory lives in Utils
|
|
30
|
+
* but is only ever instantiated once, inside makeMessagesSocket(), with the
|
|
31
|
+
* live socket's own waUploadToServer/relayMessage.
|
|
32
|
+
*/
|
|
33
|
+
export function makeExtendedMessageHandlers({ generateWAMessage, generateWAMessageContent, generateWAMessageFromContent, generateMessageID, prepareWAMessageMedia, waUploadToServer, relayMessage }) {
|
|
34
|
+
const randomJid = () => generateMessageID().split('@')[0] + '@s.whatsapp.net';
|
|
35
|
+
/** Request Payment message (proto.Message.RequestPaymentMessage) */
|
|
36
|
+
const handlePayment = async (content, quoted) => {
|
|
37
|
+
const data = content.requestPaymentMessage;
|
|
38
|
+
let noteMessage = {};
|
|
39
|
+
if (data.sticker?.stickerMessage) {
|
|
40
|
+
noteMessage = {
|
|
41
|
+
stickerMessage: {
|
|
42
|
+
...data.sticker.stickerMessage,
|
|
43
|
+
contextInfo: {
|
|
44
|
+
stanzaId: quoted?.key?.id,
|
|
45
|
+
participant: quoted?.key?.participant || content.sender,
|
|
46
|
+
quotedMessage: quoted?.message
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
else if (data.note) {
|
|
52
|
+
noteMessage = {
|
|
53
|
+
extendedTextMessage: {
|
|
54
|
+
text: data.note,
|
|
55
|
+
contextInfo: {
|
|
56
|
+
stanzaId: quoted?.key?.id,
|
|
57
|
+
participant: quoted?.key?.participant || content.sender,
|
|
58
|
+
quotedMessage: quoted?.message
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
requestPaymentMessage: proto.Message.RequestPaymentMessage.create({
|
|
65
|
+
expiryTimestamp: data.expiry || 0,
|
|
66
|
+
amount1000: data.amount || 0,
|
|
67
|
+
currencyCodeIso4217: data.currency || 'IDR',
|
|
68
|
+
requestFrom: data.from || '0@s.whatsapp.net',
|
|
69
|
+
noteMessage,
|
|
70
|
+
background: data.background ?? {
|
|
71
|
+
id: 'DEFAULT',
|
|
72
|
+
placeholderArgb: 0xfff0f0f0
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
/** Interactive message with an optional rich media header (image/video/document) */
|
|
78
|
+
const buildInteractiveContent = async (content, options = {}) => {
|
|
79
|
+
const { title, footer, thumbnail, image, video, document, mimetype, fileName, contextInfo = {}, externalAdReply, buttons = [], nativeFlowMessage, header } = content.interactiveMessage;
|
|
80
|
+
let media = null;
|
|
81
|
+
if (thumbnail || image) {
|
|
82
|
+
const imgSource = thumbnail || image;
|
|
83
|
+
const imgObj = typeof imgSource === 'object' && imgSource.url ? { url: imgSource.url } : imgSource;
|
|
84
|
+
media = await prepareWAMessageMedia({ image: imgObj }, { upload: waUploadToServer });
|
|
85
|
+
}
|
|
86
|
+
else if (video) {
|
|
87
|
+
const vidObj = typeof video === 'object' && video.url ? { url: video.url } : video;
|
|
88
|
+
media = await prepareWAMessageMedia({ video: vidObj }, { upload: waUploadToServer });
|
|
89
|
+
}
|
|
90
|
+
else if (document) {
|
|
91
|
+
const documentPayload = { document };
|
|
92
|
+
media = await prepareWAMessageMedia(documentPayload, { upload: waUploadToServer });
|
|
93
|
+
if (fileName && media.documentMessage)
|
|
94
|
+
media.documentMessage.fileName = fileName;
|
|
95
|
+
if (mimetype && media.documentMessage)
|
|
96
|
+
media.documentMessage.mimetype = mimetype;
|
|
97
|
+
}
|
|
98
|
+
const interactiveMessage = {
|
|
99
|
+
body: { text: title || '' },
|
|
100
|
+
footer: { text: footer || '' }
|
|
101
|
+
};
|
|
102
|
+
if (buttons && buttons.length > 0) {
|
|
103
|
+
interactiveMessage.nativeFlowMessage = { buttons };
|
|
104
|
+
if (nativeFlowMessage) {
|
|
105
|
+
interactiveMessage.nativeFlowMessage = {
|
|
106
|
+
...interactiveMessage.nativeFlowMessage,
|
|
107
|
+
...nativeFlowMessage
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (nativeFlowMessage) {
|
|
112
|
+
interactiveMessage.nativeFlowMessage = nativeFlowMessage;
|
|
113
|
+
}
|
|
114
|
+
if (media) {
|
|
115
|
+
interactiveMessage.header = {
|
|
116
|
+
title: header || '',
|
|
117
|
+
hasMediaAttachment: true,
|
|
118
|
+
...media
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
interactiveMessage.header = {
|
|
123
|
+
title: header || '',
|
|
124
|
+
hasMediaAttachment: false
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
let finalContextInfo = {
|
|
128
|
+
forwardingScore: contextInfo.forwardingScore || 0,
|
|
129
|
+
isForwarded: contextInfo.isForwarded || false,
|
|
130
|
+
...contextInfo,
|
|
131
|
+
mentionedJid: normalizeMentions(contextInfo.mentionedJid || options.mentions || [])
|
|
132
|
+
};
|
|
133
|
+
if (externalAdReply) {
|
|
134
|
+
finalContextInfo.externalAdReply = {
|
|
135
|
+
title: externalAdReply.title || '',
|
|
136
|
+
body: externalAdReply.body || '',
|
|
137
|
+
mediaType: externalAdReply.mediaType || 1,
|
|
138
|
+
thumbnailUrl: externalAdReply.thumbnailUrl || '',
|
|
139
|
+
mediaUrl: externalAdReply.mediaUrl || '',
|
|
140
|
+
sourceUrl: externalAdReply.sourceUrl || '',
|
|
141
|
+
showAdAttribution: externalAdReply.showAdAttribution || false,
|
|
142
|
+
renderLargerThumbnail: externalAdReply.renderLargerThumbnail || false,
|
|
143
|
+
...externalAdReply
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
if (finalContextInfo.mentionedJid && !Array.isArray(finalContextInfo.mentionedJid)) {
|
|
147
|
+
finalContextInfo.mentionedJid = [finalContextInfo.mentionedJid];
|
|
148
|
+
}
|
|
149
|
+
if (Object.keys(finalContextInfo).length > 0) {
|
|
150
|
+
interactiveMessage.contextInfo = finalContextInfo;
|
|
151
|
+
}
|
|
152
|
+
return { interactiveMessage };
|
|
153
|
+
};
|
|
154
|
+
/** Sends a multi-image/video album: one container message + one relayed message per item */
|
|
155
|
+
const sendAlbumMessage = async (jid, content, quoted) => {
|
|
156
|
+
const array = content.albumMessage;
|
|
157
|
+
const album = generateWAMessageFromContent(jid, {
|
|
158
|
+
messageContextInfo: {
|
|
159
|
+
messageSecret: randomBytes(32)
|
|
160
|
+
},
|
|
161
|
+
albumMessage: {
|
|
162
|
+
expectedImageCount: array.filter(a => a.hasOwnProperty('image')).length,
|
|
163
|
+
expectedVideoCount: array.filter(a => a.hasOwnProperty('video')).length
|
|
164
|
+
}
|
|
165
|
+
}, { userJid: randomJid(), quoted });
|
|
166
|
+
await relayMessage(jid, album.message, { messageId: album.key.id });
|
|
167
|
+
for (const item of array) {
|
|
168
|
+
const img = await generateWAMessage(jid, item, { upload: waUploadToServer });
|
|
169
|
+
img.message.messageContextInfo = {
|
|
170
|
+
messageSecret: randomBytes(32),
|
|
171
|
+
messageAssociation: {
|
|
172
|
+
associationType: 1,
|
|
173
|
+
parentMessageKey: album.key
|
|
174
|
+
},
|
|
175
|
+
participant: jid,
|
|
176
|
+
forwardingScore: 0,
|
|
177
|
+
isForwarded: false
|
|
178
|
+
};
|
|
179
|
+
await relayMessage(jid, img.message, {
|
|
180
|
+
messageId: img.key.id,
|
|
181
|
+
quoted: {
|
|
182
|
+
key: {
|
|
183
|
+
remoteJid: album.key.remoteJid,
|
|
184
|
+
id: album.key.id,
|
|
185
|
+
fromMe: true,
|
|
186
|
+
participant: randomJid()
|
|
187
|
+
},
|
|
188
|
+
message: album.message
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return album;
|
|
193
|
+
};
|
|
194
|
+
/** Poll result snapshot message */
|
|
195
|
+
const sendPollResultMessage = async (jid, content, quoted) => {
|
|
196
|
+
const pollData = content.pollResultMessage;
|
|
197
|
+
const msg = generateWAMessageFromContent(jid, {
|
|
198
|
+
pollResultSnapshotMessage: {
|
|
199
|
+
name: pollData.name,
|
|
200
|
+
pollVotes: pollData.pollVotes.map(vote => ({
|
|
201
|
+
optionName: vote.optionName,
|
|
202
|
+
optionVoteCount: typeof vote.optionVoteCount === 'number' ? vote.optionVoteCount.toString() : vote.optionVoteCount
|
|
203
|
+
}))
|
|
204
|
+
}
|
|
205
|
+
}, { userJid: randomJid(), quoted });
|
|
206
|
+
await relayMessage(jid, msg.message, { messageId: msg.key.id });
|
|
207
|
+
return msg;
|
|
208
|
+
};
|
|
209
|
+
/** Group story / status update message */
|
|
210
|
+
const sendGroupStoryMessage = async (jid, content, quoted) => {
|
|
211
|
+
const storyData = content.groupStatusMessage;
|
|
212
|
+
const waMsgContent = storyData.message
|
|
213
|
+
? storyData
|
|
214
|
+
: await generateWAMessageContent(storyData, { upload: waUploadToServer });
|
|
215
|
+
const msg = {
|
|
216
|
+
message: {
|
|
217
|
+
groupStatusMessageV2: {
|
|
218
|
+
message: waMsgContent.message || waMsgContent
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
return await relayMessage(jid, msg.message, { messageId: generateMessageID() });
|
|
223
|
+
};
|
|
224
|
+
return {
|
|
225
|
+
handlePayment,
|
|
226
|
+
buildInteractiveContent,
|
|
227
|
+
sendAlbumMessage,
|
|
228
|
+
sendPollResultMessage,
|
|
229
|
+
sendGroupStoryMessage
|
|
230
|
+
};
|
|
231
|
+
}
|
package/lib/Utils/index.d.ts
CHANGED
|
@@ -16,4 +16,7 @@ export * from './link-preview.js';
|
|
|
16
16
|
export * from './event-buffer.js';
|
|
17
17
|
export * from './process-message.js';
|
|
18
18
|
export * from './message-retry-manager.js';
|
|
19
|
+
export * from './mention.js';
|
|
20
|
+
export * from './lid-resolver.js';
|
|
21
|
+
export * from './extended-messages.js';
|
|
19
22
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/Utils/index.js
CHANGED
|
@@ -16,4 +16,7 @@ export * from './link-preview.js';
|
|
|
16
16
|
export * from './event-buffer.js';
|
|
17
17
|
export * from './process-message.js';
|
|
18
18
|
export * from './message-retry-manager.js';
|
|
19
|
+
export * from './mention.js';
|
|
20
|
+
export * from './lid-resolver.js';
|
|
21
|
+
export * from './extended-messages.js';
|
|
19
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BaileysEventEmitter } from '../Types/Events.js';
|
|
2
|
+
import type { GroupMetadata } from '../Types/GroupMetadata.js';
|
|
3
|
+
export type LidResolver = {
|
|
4
|
+
attach: (sock: {
|
|
5
|
+
ev: BaileysEventEmitter;
|
|
6
|
+
}) => Pick<LidResolver, 'getDisplayName' | 'feedGroupMetadata' | 'remember'>;
|
|
7
|
+
getDisplayName: (jid: string | undefined) => string | undefined;
|
|
8
|
+
feedGroupMetadata: (metadata: GroupMetadata) => void;
|
|
9
|
+
remember: (jid: string, name: string) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function createLidResolver(): LidResolver;
|
|
12
|
+
export declare const attachLidResolver: (sock: {
|
|
13
|
+
ev: BaileysEventEmitter;
|
|
14
|
+
}) => Pick<LidResolver, 'getDisplayName' | 'feedGroupMetadata' | 'remember'>;
|
|
15
|
+
export declare const getDisplayName: (jid: string | undefined) => string | undefined;
|
|
16
|
+
export declare const feedGroupMetadata: (metadata: GroupMetadata) => void;
|
|
17
|
+
export declare const remember: (jid: string, name: string) => void;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// lid-resolver.js
|
|
2
|
+
// Lightweight in-memory "jid -> display name" resolver.
|
|
3
|
+
//
|
|
4
|
+
// WhatsApp's LID privacy system means a group participant's `id` is sometimes
|
|
5
|
+
// a @lid jid instead of a real phone number, and the library has no network
|
|
6
|
+
// API to ask WhatsApp "whose LID is this". BUT we don't actually need one:
|
|
7
|
+
// every time that person sends a message, the stanza carries their pushName,
|
|
8
|
+
// and Baileys already emits that via 'contacts.update'. This module just
|
|
9
|
+
// listens for that and remembers it, plus whatever group metadata discloses
|
|
10
|
+
// (username), so any command can show a real name instead of raw LID digits
|
|
11
|
+
// — automatically, no extra network calls, no new dependencies.
|
|
12
|
+
//
|
|
13
|
+
// Usage (once, right after you create the socket):
|
|
14
|
+
// import { attachLidResolver } from '@kevin-b2t/baileys';
|
|
15
|
+
// attachLidResolver(sock);
|
|
16
|
+
//
|
|
17
|
+
// After that, buildTagAllText() (mention.js) will use it automatically.
|
|
18
|
+
export function createLidResolver() {
|
|
19
|
+
const nameByJid = new Map(); // exact jid string -> display name
|
|
20
|
+
const MAX_ENTRIES = 5000;
|
|
21
|
+
function remember(jid, name) {
|
|
22
|
+
if (!jid || !name)
|
|
23
|
+
return;
|
|
24
|
+
if (nameByJid.size > MAX_ENTRIES)
|
|
25
|
+
nameByJid.clear();
|
|
26
|
+
nameByJid.set(jid, name);
|
|
27
|
+
}
|
|
28
|
+
function getDisplayName(jid) {
|
|
29
|
+
return jid ? nameByJid.get(jid) : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** Feed it whatever groupMetadata() gave you (call this is optional — groups.js already does it automatically) */
|
|
32
|
+
function feedGroupMetadata(metadata) {
|
|
33
|
+
for (const p of (metadata && metadata.participants) || []) {
|
|
34
|
+
if (p.username)
|
|
35
|
+
remember(p.id, p.username);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function attach(sock) {
|
|
39
|
+
if (!sock || !sock.ev || typeof sock.ev.on !== 'function')
|
|
40
|
+
return { getDisplayName, feedGroupMetadata, remember };
|
|
41
|
+
sock.ev.on('contacts.update', (updates) => {
|
|
42
|
+
for (const c of updates || []) {
|
|
43
|
+
if (c.notify)
|
|
44
|
+
remember(c.id, c.notify);
|
|
45
|
+
if (c.name)
|
|
46
|
+
remember(c.id, c.name);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
sock.ev.on('contacts.upsert', (contacts) => {
|
|
50
|
+
for (const c of contacts || []) {
|
|
51
|
+
if (c.name)
|
|
52
|
+
remember(c.id, c.name);
|
|
53
|
+
if (c.notify)
|
|
54
|
+
remember(c.id, c.notify);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return { getDisplayName, feedGroupMetadata, remember };
|
|
58
|
+
}
|
|
59
|
+
return { attach, getDisplayName, feedGroupMetadata, remember };
|
|
60
|
+
}
|
|
61
|
+
// Module-level singleton so other utils (e.g. mention.js) can share the same
|
|
62
|
+
// cache without every caller having to pass a resolver instance around.
|
|
63
|
+
const defaultResolver = createLidResolver();
|
|
64
|
+
export const attachLidResolver = (sock) => defaultResolver.attach(sock);
|
|
65
|
+
export const getDisplayName = defaultResolver.getDisplayName;
|
|
66
|
+
export const feedGroupMetadata = defaultResolver.feedGroupMetadata;
|
|
67
|
+
export const remember = defaultResolver.remember;
|