@itznomo/baileys 0.0.0-stage → 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/BUTTONS.md +153 -0
- package/Example/call-bot.mjs +38 -0
- package/Example/example.mjs +36 -0
- package/Example/full-bot-commands.mjs +319 -0
- package/Example/full-bot.mjs +77 -0
- package/LICENSE +21 -0
- package/README.md +543 -2
- package/WAProto/@dnuzi +1 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +113300 -0
- package/assets/banner.svg +17 -0
- package/assets/devider.svg +1 -0
- package/assets/features.svg +21 -0
- package/docs/CALLS.md +106 -0
- package/docs/FEATURES.md +1152 -0
- package/engine-requirements.js +19 -0
- package/index.d.ts +815 -0
- package/package.json +51 -4
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +627 -0
- package/src/constants.js +147 -0
- package/src/defaults.js +106 -0
- package/src/foundation/ai-rich.js +654 -0
- package/src/foundation/audio-duration.js +470 -0
- package/src/foundation/audio-samples.js +250 -0
- package/src/foundation/boom.js +39 -0
- package/src/foundation/builders.js +851 -0
- package/src/foundation/cache.js +134 -0
- package/src/foundation/concurrency.js +69 -0
- package/src/foundation/curve25519.js +189 -0
- package/src/foundation/image-thumb.js +65 -0
- package/src/foundation/link-preview.js +278 -0
- package/src/foundation/logger.js +78 -0
- package/src/foundation/long.js +130 -0
- package/src/foundation/performance.js +385 -0
- package/src/foundation/protobuf-minimal.js +587 -0
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +47 -0
- package/src/foundation/zip.js +108 -0
- package/src/index.js +67 -0
- package/src/signal/libsignal.js +996 -0
- package/src/signal/signal-group.js +455 -0
- package/src/signal/signal-repository.js +635 -0
- package/src/socket/business-communities.js +811 -0
- package/src/socket/chats.js +702 -0
- package/src/socket/groups-newsletter.js +524 -0
- package/src/socket/messages-recv.js +1815 -0
- package/src/socket/messages-send.js +1526 -0
- package/src/socket/socket-builder.js +131 -0
- package/src/socket/socket-core.js +913 -0
- package/src/socket/usync.js +287 -0
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +170 -0
- package/src/utils/auth-state-core.js +731 -0
- package/src/utils/auth-state-storage.js +594 -0
- package/src/utils/chat-sync.js +1046 -0
- package/src/utils/check-status.js +159 -0
- package/src/utils/cstoken.js +17 -0
- package/src/utils/media.js +807 -0
- package/src/utils/message-compose.js +1747 -0
- package/src/utils/message-processing.js +912 -0
- package/src/utils/modern-messages.js +435 -0
- package/src/utils/nomo-buttons.js +138 -0
- package/src/utils/nomo-commands.js +168 -0
- package/src/utils/spam-report-utils.js +136 -0
- package/src/utils/use-keyv-auth-state.js +307 -0
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +1227 -0
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/auto.js +139 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1727 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +730 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +22852 -0
- package/src/wam/wam.js +146 -0
- package/voip.d.ts +144 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js'
|
|
2
|
+
|
|
3
|
+
const requireObject = (value, name) => {
|
|
4
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
5
|
+
throw new TypeError(`${name} must be an object`)
|
|
6
|
+
}
|
|
7
|
+
return value
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const requireKey = (value, name = 'key') => {
|
|
11
|
+
requireObject(value, name)
|
|
12
|
+
if (!value.id) {
|
|
13
|
+
throw new TypeError(`${name}.id is required`)
|
|
14
|
+
}
|
|
15
|
+
return value
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const enumValue = (enumObject, value, fallback) => {
|
|
19
|
+
if (typeof value === 'number') return value
|
|
20
|
+
if (typeof value === 'string') {
|
|
21
|
+
const key = value.trim().toUpperCase()
|
|
22
|
+
if (key in enumObject) return enumObject[key]
|
|
23
|
+
}
|
|
24
|
+
return fallback
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const STATUS_NOTIFICATION_TYPES = Object.freeze({
|
|
28
|
+
...(proto.Message.StatusNotificationMessage?.StatusNotificationType || {}),
|
|
29
|
+
STATUS_GROUP_STATUS_REPLY: 4
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const toSeconds = value => (value instanceof Date ? Math.floor(value.getTime() / 1000) : value)
|
|
33
|
+
const toMilliseconds = value => (value instanceof Date ? value.getTime() : value)
|
|
34
|
+
|
|
35
|
+
const makeInnerCommentMessage = content => {
|
|
36
|
+
const data = requireObject(content, 'comment.content')
|
|
37
|
+
if (data.raw && typeof data.raw === 'object') return data.raw
|
|
38
|
+
if (typeof data.text === 'string') {
|
|
39
|
+
return {
|
|
40
|
+
extendedTextMessage: proto.Message.ExtendedTextMessage.create({
|
|
41
|
+
text: data.text,
|
|
42
|
+
contextInfo: data.contextInfo
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const keys = Object.keys(data)
|
|
47
|
+
if (keys.some(key => key.endsWith('Message'))) return data
|
|
48
|
+
throw new TypeError('comment.content supports text, raw, or protobuf message fields')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const makeQuestionMessage = input => {
|
|
52
|
+
const data = requireObject(input, 'question')
|
|
53
|
+
if (typeof data.text !== 'string' || !data.text.length) throw new TypeError('question.text is required')
|
|
54
|
+
return {
|
|
55
|
+
questionMessage: proto.Message.FutureProofMessage.create({
|
|
56
|
+
message: {
|
|
57
|
+
extendedTextMessage: proto.Message.ExtendedTextMessage.create({
|
|
58
|
+
text: data.text,
|
|
59
|
+
contextInfo: { ...(data.contextInfo || {}), isQuestion: true }
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const makeQuestionResponseMessage = input => {
|
|
67
|
+
const data = requireObject(input, 'questionResponse')
|
|
68
|
+
requireKey(data.key, 'questionResponse.key')
|
|
69
|
+
return {
|
|
70
|
+
questionResponseMessage: proto.Message.QuestionResponseMessage.create({
|
|
71
|
+
key: data.key,
|
|
72
|
+
text: data.text ?? ''
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const makeQuestionReplyMessage = input => {
|
|
78
|
+
const data = requireObject(input, 'questionReply')
|
|
79
|
+
if (typeof data.text !== 'string' || !data.text.length) throw new TypeError('questionReply.text is required')
|
|
80
|
+
if (data.serverQuestionId === undefined || data.serverQuestionId === null) throw new TypeError('questionReply.serverQuestionId is required')
|
|
81
|
+
const quoted = { serverQuestionId: Number(data.serverQuestionId) }
|
|
82
|
+
if (data.quotedQuestion) quoted.quotedQuestion = data.quotedQuestion
|
|
83
|
+
if (data.quotedResponse) quoted.quotedResponse = data.quotedResponse
|
|
84
|
+
return {
|
|
85
|
+
questionReplyMessage: proto.Message.FutureProofMessage.create({
|
|
86
|
+
message: {
|
|
87
|
+
extendedTextMessage: proto.Message.ExtendedTextMessage.create({
|
|
88
|
+
text: data.text,
|
|
89
|
+
contextInfo: { ...(data.contextInfo || {}), questionReplyQuotedMessage: quoted }
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const makeStatusQuestionAnswerMessage = input => {
|
|
97
|
+
const data = requireObject(input, 'statusQuestionAnswer')
|
|
98
|
+
requireKey(data.key, 'statusQuestionAnswer.key')
|
|
99
|
+
return {
|
|
100
|
+
statusQuestionAnswerMessage: proto.Message.StatusQuestionAnswerMessage.create({
|
|
101
|
+
key: data.key,
|
|
102
|
+
text: data.text ?? ''
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const makeStatusQuotedMessage = input => {
|
|
108
|
+
const data = requireObject(input, 'statusQuoted')
|
|
109
|
+
requireKey(data.originalStatusId, 'statusQuoted.originalStatusId')
|
|
110
|
+
const type = enumValue(
|
|
111
|
+
proto.Message.StatusQuotedMessage?.StatusQuotedMessageType || {},
|
|
112
|
+
data.type,
|
|
113
|
+
proto.Message.StatusQuotedMessage?.StatusQuotedMessageType?.QUESTION_ANSWER || 1
|
|
114
|
+
)
|
|
115
|
+
return {
|
|
116
|
+
statusQuotedMessage: proto.Message.StatusQuotedMessage.create({
|
|
117
|
+
type,
|
|
118
|
+
text: data.text,
|
|
119
|
+
thumbnail: data.thumbnail,
|
|
120
|
+
originalStatusId: data.originalStatusId
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const makeStatusStickerInteractionMessage = input => {
|
|
126
|
+
const data = requireObject(input, 'statusStickerInteraction')
|
|
127
|
+
requireKey(data.key, 'statusStickerInteraction.key')
|
|
128
|
+
if (!data.stickerKey) throw new TypeError('statusStickerInteraction.stickerKey is required')
|
|
129
|
+
const type = enumValue(
|
|
130
|
+
proto.Message.StatusStickerInteractionMessage?.StatusStickerType || {},
|
|
131
|
+
data.type,
|
|
132
|
+
proto.Message.StatusStickerInteractionMessage?.StatusStickerType?.REACTION || 1
|
|
133
|
+
)
|
|
134
|
+
return {
|
|
135
|
+
statusStickerInteractionMessage: proto.Message.StatusStickerInteractionMessage.create({
|
|
136
|
+
key: data.key,
|
|
137
|
+
stickerKey: data.stickerKey,
|
|
138
|
+
type
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export const makeStatusNotificationMessage = input => {
|
|
144
|
+
const data = requireObject(input, 'statusNotification')
|
|
145
|
+
requireKey(data.responseMessageKey, 'statusNotification.responseMessageKey')
|
|
146
|
+
requireKey(data.originalMessageKey, 'statusNotification.originalMessageKey')
|
|
147
|
+
const type = enumValue(STATUS_NOTIFICATION_TYPES, data.type, STATUS_NOTIFICATION_TYPES.UNKNOWN || 0)
|
|
148
|
+
return {
|
|
149
|
+
statusNotificationMessage: proto.Message.StatusNotificationMessage.create({
|
|
150
|
+
responseMessageKey: data.responseMessageKey,
|
|
151
|
+
originalMessageKey: data.originalMessageKey,
|
|
152
|
+
type
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const makeNewsletterAdminInviteMessage = input => {
|
|
158
|
+
const data = requireObject(input, 'newsletterAdminInvite')
|
|
159
|
+
if (!data.newsletterJid) throw new TypeError('newsletterAdminInvite.newsletterJid is required')
|
|
160
|
+
return {
|
|
161
|
+
newsletterAdminInviteMessage: proto.Message.NewsletterAdminInviteMessage.create({
|
|
162
|
+
newsletterJid: data.newsletterJid,
|
|
163
|
+
newsletterName: data.newsletterName,
|
|
164
|
+
jpegThumbnail: data.jpegThumbnail,
|
|
165
|
+
caption: data.caption,
|
|
166
|
+
inviteExpiration: data.inviteExpiration,
|
|
167
|
+
contextInfo: data.contextInfo
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export const makeNewsletterFollowerInviteMessage = input => {
|
|
173
|
+
const data = requireObject(input, 'newsletterFollowerInvite')
|
|
174
|
+
if (!data.newsletterJid) throw new TypeError('newsletterFollowerInvite.newsletterJid is required')
|
|
175
|
+
return {
|
|
176
|
+
newsletterFollowerInviteMessageV2: proto.Message.NewsletterFollowerInviteMessage.create({
|
|
177
|
+
newsletterJid: data.newsletterJid,
|
|
178
|
+
newsletterName: data.newsletterName,
|
|
179
|
+
jpegThumbnail: data.jpegThumbnail,
|
|
180
|
+
caption: data.caption,
|
|
181
|
+
contextInfo: data.contextInfo
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export const makePollAddOptionMessage = input => {
|
|
187
|
+
const data = requireObject(input, 'pollAddOption')
|
|
188
|
+
requireKey(data.pollCreationMessageKey, 'pollAddOption.pollCreationMessageKey')
|
|
189
|
+
const addOption = typeof data.option === 'string' ? { optionName: data.option } : data.addOption || data.option
|
|
190
|
+
if (!addOption?.optionName) throw new TypeError('pollAddOption.option is required')
|
|
191
|
+
return {
|
|
192
|
+
pollAddOptionMessage: proto.Message.PollAddOptionMessage.create({
|
|
193
|
+
pollCreationMessageKey: data.pollCreationMessageKey,
|
|
194
|
+
addOption,
|
|
195
|
+
metadata: data.metadata
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export const makeCommentMessage = input => {
|
|
201
|
+
const data = requireObject(input, 'comment')
|
|
202
|
+
requireKey(data.targetMessageKey, 'comment.targetMessageKey')
|
|
203
|
+
const message = data.message || (data.content ? makeInnerCommentMessage(data.content) : null)
|
|
204
|
+
requireObject(message, 'comment.message')
|
|
205
|
+
return {
|
|
206
|
+
commentMessage: proto.Message.CommentMessage.create({
|
|
207
|
+
message,
|
|
208
|
+
targetMessageKey: data.targetMessageKey
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export const makeEventInviteMessage = input => {
|
|
214
|
+
const data = requireObject(input, 'eventInvite')
|
|
215
|
+
if (!data.eventId) throw new TypeError('eventInvite.eventId is required')
|
|
216
|
+
return {
|
|
217
|
+
eventInviteMessage: proto.Message.EventInviteMessage.create({
|
|
218
|
+
contextInfo: data.contextInfo,
|
|
219
|
+
eventId: data.eventId,
|
|
220
|
+
eventTitle: data.eventTitle,
|
|
221
|
+
jpegThumbnail: data.jpegThumbnail,
|
|
222
|
+
startTime: toSeconds(data.startTime),
|
|
223
|
+
caption: data.caption,
|
|
224
|
+
isCanceled: data.isCanceled,
|
|
225
|
+
endTime: toSeconds(data.endTime),
|
|
226
|
+
callLink: data.callLink
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export const makeScheduledCallCreationMessage = input => {
|
|
232
|
+
const data = requireObject(input, 'scheduledCall')
|
|
233
|
+
if (!data.scheduledTimestampMs) throw new TypeError('scheduledCall.scheduledTimestampMs is required')
|
|
234
|
+
const callType = enumValue(
|
|
235
|
+
proto.Message.ScheduledCallCreationMessage?.CallType || {},
|
|
236
|
+
data.callType,
|
|
237
|
+
proto.Message.ScheduledCallCreationMessage?.CallType?.UNKNOWN || 0
|
|
238
|
+
)
|
|
239
|
+
return {
|
|
240
|
+
scheduledCallCreationMessage: proto.Message.ScheduledCallCreationMessage.create({
|
|
241
|
+
scheduledTimestampMs: toMilliseconds(data.scheduledTimestampMs),
|
|
242
|
+
callType,
|
|
243
|
+
title: data.title
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export const makeScheduledCallEditMessage = input => {
|
|
249
|
+
const data = requireObject(input, 'scheduledCallEdit')
|
|
250
|
+
requireKey(data.key, 'scheduledCallEdit.key')
|
|
251
|
+
const editType = enumValue(
|
|
252
|
+
proto.Message.ScheduledCallEditMessage?.EditType || {},
|
|
253
|
+
data.editType,
|
|
254
|
+
proto.Message.ScheduledCallEditMessage?.EditType?.CANCEL || 1
|
|
255
|
+
)
|
|
256
|
+
return {
|
|
257
|
+
scheduledCallEditMessage: proto.Message.ScheduledCallEditMessage.create({
|
|
258
|
+
key: data.key,
|
|
259
|
+
editType
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export const makeGroupStatusReactionMessage = input => {
|
|
265
|
+
const data = requireObject(input, 'groupStatusReaction')
|
|
266
|
+
requireKey(data.key, 'groupStatusReaction.key')
|
|
267
|
+
return {
|
|
268
|
+
groupStatusMessageV2: proto.Message.FutureProofMessage.create({
|
|
269
|
+
message: {
|
|
270
|
+
reactionMessage: proto.Message.ReactionMessage.create({
|
|
271
|
+
key: data.key,
|
|
272
|
+
text: data.text ?? '',
|
|
273
|
+
groupingKey: data.groupingKey,
|
|
274
|
+
senderTimestampMs: data.senderTimestampMs || Date.now()
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export const makeNewsletterStatusAttribution = input => {
|
|
282
|
+
const data = requireObject(input, 'newsletterStatus')
|
|
283
|
+
if (!data.newsletterJid) throw new TypeError('newsletterStatus.newsletterJid is required')
|
|
284
|
+
if (data.messageId === undefined || data.messageId === null) throw new TypeError('newsletterStatus.messageId is required')
|
|
285
|
+
return proto.StatusAttribution.create({
|
|
286
|
+
type: proto.StatusAttribution?.Type?.NEWSLETTER_STATUS || 1,
|
|
287
|
+
actionUrl: data.actionUrl,
|
|
288
|
+
statusReshare: {
|
|
289
|
+
source: proto.StatusAttribution?.StatusReshare?.Source?.CHANNEL_RESHARE || 1,
|
|
290
|
+
metadata: {
|
|
291
|
+
duration: data.duration,
|
|
292
|
+
channelJid: data.newsletterJid,
|
|
293
|
+
channelMessageId: Number(data.messageId),
|
|
294
|
+
hasMultipleReshares: data.hasMultipleReshares ?? false
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export const makeGroupStatusAttribution = input => {
|
|
301
|
+
const data = requireObject(input, 'groupStatusAttribution')
|
|
302
|
+
if (!data.authorJid) throw new TypeError('groupStatusAttribution.authorJid is required')
|
|
303
|
+
return proto.StatusAttribution.create({
|
|
304
|
+
type: proto.StatusAttribution?.Type?.GROUP_STATUS || 2,
|
|
305
|
+
actionUrl: data.actionUrl,
|
|
306
|
+
groupStatus: { authorJid: data.authorJid }
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export const STATUS_AUDIENCE_DEFAULT_LIST_NAME = 'Close friends'
|
|
311
|
+
export const STATUS_AUDIENCE_DEFAULT_EMOJI = '⭐'
|
|
312
|
+
|
|
313
|
+
export const makeStatusAudienceMetadata = input => {
|
|
314
|
+
const data = typeof input === 'string' ? { listName: input } : requireObject(input, 'statusAudience')
|
|
315
|
+
const audienceType = enumValue(
|
|
316
|
+
proto.ContextInfo.StatusAudienceMetadata?.AudienceType || {},
|
|
317
|
+
data.audienceType,
|
|
318
|
+
proto.ContextInfo.StatusAudienceMetadata?.AudienceType?.CLOSE_FRIENDS || 1
|
|
319
|
+
)
|
|
320
|
+
const listName = data.listName === undefined ? STATUS_AUDIENCE_DEFAULT_LIST_NAME : String(data.listName)
|
|
321
|
+
const listEmoji = data.listEmoji === undefined ? STATUS_AUDIENCE_DEFAULT_EMOJI : String(data.listEmoji)
|
|
322
|
+
return proto.ContextInfo.StatusAudienceMetadata.create({ audienceType, listName, listEmoji })
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export const makeMessageAssociation = input => {
|
|
326
|
+
const data = requireObject(input, 'messageAssociation')
|
|
327
|
+
const parentMessageKey = data.parentMessageKey ?? data.key
|
|
328
|
+
requireKey(parentMessageKey, 'messageAssociation.parentMessageKey')
|
|
329
|
+
const associationType = enumValue(
|
|
330
|
+
proto.MessageAssociation?.AssociationType || {},
|
|
331
|
+
data.associationType ?? data.type,
|
|
332
|
+
proto.MessageAssociation?.AssociationType?.UNKNOWN || 0
|
|
333
|
+
)
|
|
334
|
+
return proto.MessageAssociation.create({
|
|
335
|
+
associationType,
|
|
336
|
+
parentMessageKey,
|
|
337
|
+
messageIndex: data.messageIndex
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export const makeStatusAddYoursAssociation = input => {
|
|
342
|
+
const data = input && typeof input === 'object' && !Array.isArray(input) && input.id ? { key: input } : requireObject(input, 'addYours')
|
|
343
|
+
const parentMessageKey = data.key ?? data.parentMessageKey
|
|
344
|
+
requireKey(parentMessageKey, 'addYours.key')
|
|
345
|
+
const associationType = enumValue(
|
|
346
|
+
proto.MessageAssociation?.AssociationType || {},
|
|
347
|
+
data.type ?? data.associationType,
|
|
348
|
+
proto.MessageAssociation?.AssociationType?.STATUS_ADD_YOURS || 1
|
|
349
|
+
)
|
|
350
|
+
return proto.MessageAssociation.create({
|
|
351
|
+
associationType,
|
|
352
|
+
parentMessageKey,
|
|
353
|
+
messageIndex: data.messageIndex
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export const makeStatusMentionMessage = input => {
|
|
358
|
+
const data = requireObject(input, 'statusMention')
|
|
359
|
+
requireKey(data.key, 'statusMention.key')
|
|
360
|
+
const field = data.group ? 'groupStatusMentionMessage' : 'statusMentionMessage'
|
|
361
|
+
return {
|
|
362
|
+
[field]: proto.Message.FutureProofMessage.create({
|
|
363
|
+
message: {
|
|
364
|
+
protocolMessage: proto.Message.ProtocolMessage.create({
|
|
365
|
+
key: data.key,
|
|
366
|
+
type: proto.Message.ProtocolMessage.Type.STATUS_MENTION_MESSAGE || 25
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const modernBuilders = [
|
|
374
|
+
['question', makeQuestionMessage],
|
|
375
|
+
['questionReply', makeQuestionReplyMessage],
|
|
376
|
+
['questionResponse', makeQuestionResponseMessage],
|
|
377
|
+
['statusQuestionAnswer', makeStatusQuestionAnswerMessage],
|
|
378
|
+
['statusQuoted', makeStatusQuotedMessage],
|
|
379
|
+
['statusStickerInteraction', makeStatusStickerInteractionMessage],
|
|
380
|
+
['statusNotification', makeStatusNotificationMessage],
|
|
381
|
+
['newsletterAdminInvite', makeNewsletterAdminInviteMessage],
|
|
382
|
+
['newsletterFollowerInvite', makeNewsletterFollowerInviteMessage],
|
|
383
|
+
['pollAddOption', makePollAddOptionMessage],
|
|
384
|
+
['comment', makeCommentMessage],
|
|
385
|
+
['eventInvite', makeEventInviteMessage],
|
|
386
|
+
['scheduledCall', makeScheduledCallCreationMessage],
|
|
387
|
+
['scheduledCallEdit', makeScheduledCallEditMessage],
|
|
388
|
+
['groupStatusReaction', makeGroupStatusReactionMessage],
|
|
389
|
+
['statusMention', makeStatusMentionMessage]
|
|
390
|
+
]
|
|
391
|
+
|
|
392
|
+
export const prepareModernMessageContent = content => {
|
|
393
|
+
if (!content || typeof content !== 'object' || Array.isArray(content)) return content
|
|
394
|
+
let output = { ...content }
|
|
395
|
+
const attributions = Array.isArray(output.contextInfo?.statusAttributions) ? [...output.contextInfo.statusAttributions] : []
|
|
396
|
+
if (output.newsletterStatus) {
|
|
397
|
+
attributions.push(makeNewsletterStatusAttribution(output.newsletterStatus))
|
|
398
|
+
delete output.newsletterStatus
|
|
399
|
+
}
|
|
400
|
+
if (output.groupStatusAttribution) {
|
|
401
|
+
attributions.push(makeGroupStatusAttribution(output.groupStatusAttribution))
|
|
402
|
+
delete output.groupStatusAttribution
|
|
403
|
+
}
|
|
404
|
+
if (Array.isArray(output.statusAttributions)) {
|
|
405
|
+
attributions.push(...output.statusAttributions)
|
|
406
|
+
delete output.statusAttributions
|
|
407
|
+
}
|
|
408
|
+
const carried = []
|
|
409
|
+
const audience = output.statusAudience
|
|
410
|
+
if (audience !== undefined && audience !== null) {
|
|
411
|
+
carried.push('statusAudience')
|
|
412
|
+
output.contextInfo = { ...(output.contextInfo || {}), statusAudienceMetadata: makeStatusAudienceMetadata(audience) }
|
|
413
|
+
delete output.statusAudience
|
|
414
|
+
}
|
|
415
|
+
if (attributions.length) {
|
|
416
|
+
output.contextInfo = { ...(output.contextInfo || {}), statusAttributions: attributions }
|
|
417
|
+
}
|
|
418
|
+
if (output.addYours !== undefined && output.addYours !== null) {
|
|
419
|
+
carried.push('addYours')
|
|
420
|
+
output.messageAssociation = makeStatusAddYoursAssociation(output.addYours)
|
|
421
|
+
delete output.addYours
|
|
422
|
+
} else if (output.messageAssociation !== undefined && output.messageAssociation !== null) {
|
|
423
|
+
carried.push('messageAssociation')
|
|
424
|
+
output.messageAssociation = makeMessageAssociation(output.messageAssociation)
|
|
425
|
+
}
|
|
426
|
+
for (const [key, builder] of modernBuilders) {
|
|
427
|
+
if (output[key] !== undefined && output[key] !== null) {
|
|
428
|
+
if (carried.length) {
|
|
429
|
+
throw new TypeError(`${carried.join(' and ')} cannot be combined with ${key}`)
|
|
430
|
+
}
|
|
431
|
+
return { raw: true, ...builder(output[key]) }
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return output
|
|
435
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// nomo-baileys button helpers.
|
|
2
|
+
// These sit on top of the native flow / list / carousel support in this package and keep the
|
|
3
|
+
// nomo-baileys 1.x API working: interactiveButtons, quickReplyButton(), getButtonReply(), ...
|
|
4
|
+
|
|
5
|
+
const nativeFlowButton = (name, params) => ({
|
|
6
|
+
name,
|
|
7
|
+
buttonParamsJson: JSON.stringify(params)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
/** button that sends `id` back to the bot when tapped */
|
|
11
|
+
export const quickReplyButton = (displayText, id) => nativeFlowButton('quick_reply', { display_text: displayText, id })
|
|
12
|
+
|
|
13
|
+
/** button that opens a link */
|
|
14
|
+
export const urlButton = (displayText, url, merchantUrl = url) =>
|
|
15
|
+
nativeFlowButton('cta_url', { display_text: displayText, url, merchant_url: merchantUrl })
|
|
16
|
+
|
|
17
|
+
/** button that copies `code` to the clipboard */
|
|
18
|
+
export const copyButton = (displayText, code, id = code) =>
|
|
19
|
+
nativeFlowButton('cta_copy', { display_text: displayText, id, copy_code: code })
|
|
20
|
+
|
|
21
|
+
/** button that dials a phone number */
|
|
22
|
+
export const callButton = (displayText, phoneNumber) =>
|
|
23
|
+
nativeFlowButton('cta_call', { display_text: displayText, phone_number: phoneNumber })
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* native flow drop-down list; use inside `interactiveButtons` (or `nativeFlow`)
|
|
27
|
+
* sections: [{ title?, highlightLabel?, rows: [{ id, title, header?, description? }] }]
|
|
28
|
+
*/
|
|
29
|
+
export const singleSelectButton = (title, sections) =>
|
|
30
|
+
nativeFlowButton('single_select', {
|
|
31
|
+
title,
|
|
32
|
+
sections: sections.map(section => ({
|
|
33
|
+
title: section.title ?? '',
|
|
34
|
+
highlight_label: section.highlightLabel ?? '',
|
|
35
|
+
rows: section.rows.map(row => ({
|
|
36
|
+
header: row.header ?? '',
|
|
37
|
+
title: row.title,
|
|
38
|
+
description: row.description ?? '',
|
|
39
|
+
id: row.id
|
|
40
|
+
}))
|
|
41
|
+
}))
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/** convenience for the classic `sections` list message */
|
|
45
|
+
export const listSection = (title, rows) => ({ title, rows })
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Maps nomo-baileys 1.x content shapes onto the shapes the composer understands.
|
|
49
|
+
* interactiveButtons -> nativeFlow
|
|
50
|
+
* cards[i].buttons -> cards[i].nativeFlow
|
|
51
|
+
* cards[i].body -> cards[i].caption
|
|
52
|
+
* Returns the same object when nothing needs mapping.
|
|
53
|
+
*/
|
|
54
|
+
export const withNomoAliases = message => {
|
|
55
|
+
if (!message || typeof message !== 'object') return message
|
|
56
|
+
let out = message
|
|
57
|
+
if (Array.isArray(out.interactiveButtons) && !out.nativeFlow) {
|
|
58
|
+
const { interactiveButtons, ...rest } = out
|
|
59
|
+
out = { ...rest, nativeFlow: interactiveButtons }
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(out.cards)) {
|
|
62
|
+
const needsMap = out.cards.some(card => card && ((card.buttons && !card.nativeFlow) || (card.body && !card.text && !card.caption)))
|
|
63
|
+
if (needsMap) {
|
|
64
|
+
out = {
|
|
65
|
+
...out,
|
|
66
|
+
cards: out.cards.map(card => {
|
|
67
|
+
if (!card) return card
|
|
68
|
+
const { buttons, body, ...rest } = card
|
|
69
|
+
const mapped = { ...rest }
|
|
70
|
+
if (buttons && !card.nativeFlow) mapped.nativeFlow = buttons
|
|
71
|
+
else if (buttons) mapped.buttons = buttons
|
|
72
|
+
if (body && !card.text && !card.caption) mapped.caption = body
|
|
73
|
+
else if (body) mapped.body = body
|
|
74
|
+
return mapped
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return out
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const FUTURE_PROOF = ['ephemeralMessage', 'viewOnceMessage', 'viewOnceMessageV2', 'viewOnceMessageV2Extension', 'documentWithCaptionMessage', 'editedMessage']
|
|
83
|
+
|
|
84
|
+
const unwrap = content => {
|
|
85
|
+
for (let i = 0; i < 5 && content; i++) {
|
|
86
|
+
const key = FUTURE_PROOF.find(k => content[k]?.message)
|
|
87
|
+
if (!key) break
|
|
88
|
+
content = content[key].message
|
|
89
|
+
}
|
|
90
|
+
return content
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const parseJson = raw => {
|
|
94
|
+
if (!raw) return {}
|
|
95
|
+
try {
|
|
96
|
+
const parsed = JSON.parse(raw)
|
|
97
|
+
return parsed && typeof parsed === 'object' ? parsed : {}
|
|
98
|
+
} catch {
|
|
99
|
+
return {}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Reads which button / list row a user tapped from an incoming message.
|
|
105
|
+
* Works for classic buttons, templates, list messages and native flow replies.
|
|
106
|
+
* Returns undefined when the message is not a button reply.
|
|
107
|
+
* @returns {{ kind: 'buttons'|'template'|'list'|'native_flow', id: string, displayText?: string, name?: string } | undefined}
|
|
108
|
+
*/
|
|
109
|
+
export const getButtonReply = message => {
|
|
110
|
+
const content = unwrap(message && typeof message === 'object' && 'message' in message ? message.message : message)
|
|
111
|
+
if (!content) return undefined
|
|
112
|
+
|
|
113
|
+
if (content.buttonsResponseMessage) {
|
|
114
|
+
const reply = content.buttonsResponseMessage
|
|
115
|
+
return { kind: 'buttons', id: reply.selectedButtonId ?? '', displayText: reply.selectedDisplayText ?? undefined }
|
|
116
|
+
}
|
|
117
|
+
if (content.templateButtonReplyMessage) {
|
|
118
|
+
const reply = content.templateButtonReplyMessage
|
|
119
|
+
return { kind: 'template', id: reply.selectedId ?? '', displayText: reply.selectedDisplayText ?? undefined }
|
|
120
|
+
}
|
|
121
|
+
if (content.listResponseMessage) {
|
|
122
|
+
const reply = content.listResponseMessage
|
|
123
|
+
return { kind: 'list', id: reply.singleSelectReply?.selectedRowId ?? '', displayText: reply.title ?? undefined }
|
|
124
|
+
}
|
|
125
|
+
const flow = content.interactiveResponseMessage?.nativeFlowResponseMessage
|
|
126
|
+
if (flow) {
|
|
127
|
+
const params = parseJson(flow.paramsJson)
|
|
128
|
+
const id = typeof params.id === 'string' ? params.id : ''
|
|
129
|
+
const displayText =
|
|
130
|
+
typeof params.display_text === 'string'
|
|
131
|
+
? params.display_text
|
|
132
|
+
: typeof params.title === 'string'
|
|
133
|
+
? params.title
|
|
134
|
+
: (content.interactiveResponseMessage?.body?.text ?? undefined)
|
|
135
|
+
return { kind: 'native_flow', id, displayText, name: flow.name ?? undefined }
|
|
136
|
+
}
|
|
137
|
+
return undefined
|
|
138
|
+
}
|