@zervonex/baileys 0.0.0-stage → 3.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 +21 -0
- package/README.md +3203 -2
- package/WAProto/WAProtoCompile.js +291 -0
- package/WAProto/fetcher.js +69 -0
- package/WAProto/index.js +88 -0
- package/WAProto/parser.js +313 -0
- package/engine-requirements.js +10 -0
- package/lib/Builders/AIRich.js +590 -0
- package/lib/Builders/base.js +130 -0
- package/lib/Builders/builders.js +448 -0
- package/lib/Builders/index.js +3 -0
- package/lib/Defaults/index.js +225 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/index.js +2 -0
- package/lib/Signal/libsignal.js +523 -0
- package/lib/Signal/lid-mapping.js +283 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +84 -0
- package/lib/Socket/business.js +152 -0
- package/lib/Socket/chats.js +855 -0
- package/lib/Socket/communities.js +147 -0
- package/lib/Socket/groups.js +294 -0
- package/lib/Socket/index.js +15 -0
- package/lib/Socket/messages-recv.js +904 -0
- package/lib/Socket/messages-send.js +693 -0
- package/lib/Socket/mex.js +34 -0
- package/lib/Socket/newsletter.js +752 -0
- package/lib/Socket/nexus-handler.js +55 -0
- package/lib/Socket/registration.js +155 -0
- package/lib/Socket/socket.js +798 -0
- package/lib/Socket/spam-report.js +65 -0
- package/lib/Socket/username.js +82 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.js +425 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Store/reporting-info-store.js +38 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +15 -0
- package/lib/Types/Newsletter.js +100 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +30 -0
- package/lib/Utils/auth-utils.js +142 -0
- package/lib/Utils/baileys-event-stream.js +33 -0
- package/lib/Utils/bot-capabilities.js +40 -0
- package/lib/Utils/bot-planning-replay.js +44 -0
- package/lib/Utils/browser-utils.js +26 -0
- package/lib/Utils/business.js +75 -0
- package/lib/Utils/chat-utils.js +316 -0
- package/lib/Utils/companion-reg-client-utils.js +20 -0
- package/lib/Utils/consumer-application.js +38 -0
- package/lib/Utils/crypto.js +52 -0
- package/lib/Utils/decode-wa-message.js +197 -0
- package/lib/Utils/event-buffer.js +273 -0
- package/lib/Utils/extras.js +186 -0
- package/lib/Utils/generics.js +195 -0
- package/lib/Utils/history.js +51 -0
- package/lib/Utils/identity-change-handler.js +19 -0
- package/lib/Utils/index.js +39 -0
- package/lib/Utils/key-store.js +3 -0
- package/lib/Utils/link-preview.js +51 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +3 -0
- package/lib/Utils/make-mutex.js +16 -0
- package/lib/Utils/message-composer.js +59 -0
- package/lib/Utils/message-retry-manager.js +48 -0
- package/lib/Utils/messages-media.js +520 -0
- package/lib/Utils/messages.js +742 -0
- package/lib/Utils/meta-ai-msmsg.js +122 -0
- package/lib/Utils/meta-compositing.js +75 -0
- package/lib/Utils/modern-messages.js +235 -0
- package/lib/Utils/native-flow.js +55 -0
- package/lib/Utils/noise-handler.js +161 -0
- package/lib/Utils/pre-key-manager.js +76 -0
- package/lib/Utils/process-message.js +399 -0
- package/lib/Utils/reporting-utils.js +171 -0
- package/lib/Utils/signal.js +167 -0
- package/lib/Utils/spam-report-utils.js +142 -0
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +102 -0
- package/lib/Utils/use-multi-file-auth-state.js +307 -0
- package/lib/Utils/validate-connection.js +181 -0
- package/lib/WABinary/constants.js +1321 -0
- package/lib/WABinary/decode.js +152 -0
- package/lib/WABinary/encode.js +140 -0
- package/lib/WABinary/generic-utils.js +179 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +235 -0
- package/lib/WABinary/types.js +4 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +100 -0
- package/lib/WAUSync/USyncUser.js +20 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +68 -0
- package/package.json +216 -5
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import crypto from 'crypto'
|
|
2
|
+
import * as Builders from '../Builders/index.js'
|
|
3
|
+
import { buildDispatch, buildPlugins, norm, handleGroupStatus, handleStatusMention, handlePollResult, PRIMITIVE_SHORTCUTS, HANDLER_SHORTCUTS, attachAIRich } from '../Builders/index.js'
|
|
4
|
+
import { makeUsernameSocket } from './username.js'
|
|
5
|
+
|
|
6
|
+
export const makeMessageBuilderSocket = (config) => {
|
|
7
|
+
const sock = makeUsernameSocket(config)
|
|
8
|
+
const _send = sock.sendMessage.bind(sock)
|
|
9
|
+
|
|
10
|
+
const ALL_BUILDERS = Object.values(Builders).filter(B => typeof B === 'function' && (B.dispatchKeys || B.shortcuts || B.name))
|
|
11
|
+
|
|
12
|
+
const DISPATCH = buildDispatch(ALL_BUILDERS, {
|
|
13
|
+
groupStatus: (sock, jid, content, opts, config) => handleGroupStatus(sock, jid, norm(content.groupStatus), config),
|
|
14
|
+
statusMentionMessage: (sock, jid, content, opts, config) => handleStatusMention(sock, jid, norm(content.statusMentionMessage), config),
|
|
15
|
+
pollResultMessage: (sock, jid, content) => handlePollResult(sock, jid, norm(content.pollResultMessage))
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const PLUGINS = buildPlugins(ALL_BUILDERS, { ...PRIMITIVE_SHORTCUTS, ...HANDLER_SHORTCUTS })
|
|
19
|
+
|
|
20
|
+
const result = { ...sock }
|
|
21
|
+
|
|
22
|
+
for (const [name, factory] of Object.entries(PLUGINS)) result[name] = factory(result, config)
|
|
23
|
+
|
|
24
|
+
result.sendMessage = async (jid, content, options = {}) => {
|
|
25
|
+
// aiRich shorthand — multiple aliases supported
|
|
26
|
+
const aiData = content.aiRich ?? content.airich ?? content.richResponse ?? content.AIRich
|
|
27
|
+
if (aiData) return result.sendRichFromObject(jid, aiData, options)
|
|
28
|
+
|
|
29
|
+
// dispatch to registered builder handlers
|
|
30
|
+
for (const [key, handler] of Object.entries(DISPATCH)) {
|
|
31
|
+
if (key in content) return handler(result, jid, content, options, config)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// bare nativeFlow array/object — wrap into interactiveMessage before sending
|
|
35
|
+
if (content.nativeFlow && !content.interactiveMessage) {
|
|
36
|
+
const buttons = Array.isArray(content.nativeFlow) ? content.nativeFlow : [content.nativeFlow]
|
|
37
|
+
const { generateWAMessageFromContent } = await import('../Utils/index.js')
|
|
38
|
+
const msg = await generateWAMessageFromContent(jid, {
|
|
39
|
+
interactiveMessage: { body: { text: content.text || '' }, footer: { text: content.footer || '' }, header: { hasMediaAttachment: false }, nativeFlowMessage: { buttons } }
|
|
40
|
+
}, { userJid: sock.user?.id })
|
|
41
|
+
await result.relayMessage(jid, msg.message, { messageId: msg.key.id, aiLabel: !!(content.ai ?? content.aiLabel ?? options.aiLabel ?? options.ai ?? config.aiLabel ?? config.ai) })
|
|
42
|
+
return msg
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { ai, aiLabel: contentAiLabel, ...cleanContent } = content
|
|
46
|
+
const isAi = !!(ai ?? contentAiLabel ?? options.aiLabel ?? options.ai ?? config.aiLabel)
|
|
47
|
+
if (isAi) {
|
|
48
|
+
const secret = cleanContent.messageContextInfo?.messageSecret || crypto.randomBytes(32)
|
|
49
|
+
cleanContent.messageContextInfo = { ...(cleanContent.messageContextInfo || {}), messageSecret: secret, supportPayload: JSON.stringify({ version: 2, is_ai_message: true, should_show_system_message: true, ticket_id: crypto.randomBytes(16).toString('hex') }) }
|
|
50
|
+
}
|
|
51
|
+
return _send(jid, isAi ? cleanContent : content, { ...options, aiLabel: isAi })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return attachAIRich(result, config)
|
|
55
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import axios from 'axios'
|
|
3
|
+
import { MOBILE_TOKEN, MOBILE_REGISTRATION_ENDPOINT, MOBILE_USERAGENT, REGISTRATION_PUBLIC_KEY, GRAPHQL_ENDPOINT, GRAPHQL_ACCESS_TOKEN, GRAPHQL_BAN_STATUS_DOC_ID } from '../Defaults/index.js'
|
|
4
|
+
import { md5, Curve, aesEncryptGCM } from '../Utils/index.js'
|
|
5
|
+
import { jidEncode } from '../WABinary/index.js'
|
|
6
|
+
import { makeCommunitiesSocket } from './communities.js'
|
|
7
|
+
|
|
8
|
+
function urlencode(str) {
|
|
9
|
+
return str.replace(/-/g, '%2d').replace(/_/g, '%5f').replace(/~/g, '%7e')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function convertBufferToUrlHex(buffer) {
|
|
13
|
+
let id = ''
|
|
14
|
+
buffer.forEach((x) => { id += `%${x.toString(16).padStart(2, '0').toLowerCase()}` })
|
|
15
|
+
return id
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const validRegistrationOptions = (config) =>
|
|
19
|
+
config?.phoneNumberCountryCode &&
|
|
20
|
+
config.phoneNumberNationalNumber &&
|
|
21
|
+
config.phoneNumberMobileCountryCode
|
|
22
|
+
|
|
23
|
+
export const makeRegistrationSocket = (config) => {
|
|
24
|
+
const sock = makeCommunitiesSocket(config)
|
|
25
|
+
|
|
26
|
+
const register = async (code) => {
|
|
27
|
+
if (!validRegistrationOptions(config.auth.creds.registration)) {
|
|
28
|
+
throw new Error('please specify the registration options')
|
|
29
|
+
}
|
|
30
|
+
const result = await mobileRegister(
|
|
31
|
+
{ ...sock.authState.creds, ...sock.authState.creds.registration, code },
|
|
32
|
+
config.options
|
|
33
|
+
)
|
|
34
|
+
sock.authState.creds.me = { id: jidEncode(result.login, 's.whatsapp.net'), name: '~' }
|
|
35
|
+
sock.authState.creds.registered = true
|
|
36
|
+
sock.ev.emit('creds.update', sock.authState.creds)
|
|
37
|
+
return result
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const requestRegistrationCode = async (registrationOptions) => {
|
|
41
|
+
registrationOptions = registrationOptions || config.auth.creds.registration
|
|
42
|
+
if (!validRegistrationOptions(registrationOptions)) {
|
|
43
|
+
throw new Error('Invalid registration options')
|
|
44
|
+
}
|
|
45
|
+
sock.authState.creds.registration = registrationOptions
|
|
46
|
+
sock.ev.emit('creds.update', sock.authState.creds)
|
|
47
|
+
return mobileRegisterCode({ ...config.auth.creds, ...registrationOptions }, config.options)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { ...sock, register, requestRegistrationCode }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function getBanDetails(appealToken) {
|
|
54
|
+
const res = await axios.post(GRAPHQL_ENDPOINT, {
|
|
55
|
+
variables: JSON.stringify({ app_id: 'dev.app.id', request_token: appealToken }),
|
|
56
|
+
access_token: GRAPHQL_ACCESS_TOKEN,
|
|
57
|
+
doc_id: GRAPHQL_BAN_STATUS_DOC_ID,
|
|
58
|
+
lang: 'en_GB',
|
|
59
|
+
'Content-Type': 'application/json'
|
|
60
|
+
}, {
|
|
61
|
+
headers: { 'User-Agent': MOBILE_USERAGENT, 'Content-Type': 'application/json' }
|
|
62
|
+
})
|
|
63
|
+
return res.data?.data?.whatsapp_support_ban_appeal_status || null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function registrationParams(params) {
|
|
67
|
+
const e_regid = Buffer.alloc(4)
|
|
68
|
+
e_regid.writeInt32BE(params.registrationId)
|
|
69
|
+
const e_skey_id = Buffer.alloc(3)
|
|
70
|
+
e_skey_id.writeInt16BE(params.signedPreKey.keyId)
|
|
71
|
+
params.phoneNumberCountryCode = params.phoneNumberCountryCode.replace('+', '').trim()
|
|
72
|
+
params.phoneNumberNationalNumber = params.phoneNumberNationalNumber.replace(/[/\-\s)(]/g, '').trim()
|
|
73
|
+
return {
|
|
74
|
+
cc: params.phoneNumberCountryCode,
|
|
75
|
+
in: params.phoneNumberNationalNumber,
|
|
76
|
+
Rc: '0',
|
|
77
|
+
lg: 'en',
|
|
78
|
+
lc: 'GB',
|
|
79
|
+
mistyped: '6',
|
|
80
|
+
authkey: Buffer.from(params.noiseKey.public).toString('base64url'),
|
|
81
|
+
e_regid: e_regid.toString('base64url'),
|
|
82
|
+
e_keytype: 'BQ',
|
|
83
|
+
e_ident: Buffer.from(params.signedIdentityKey.public).toString('base64url'),
|
|
84
|
+
e_skey_id: e_skey_id.toString('base64url'),
|
|
85
|
+
e_skey_val: Buffer.from(params.signedPreKey.keyPair.public).toString('base64url'),
|
|
86
|
+
e_skey_sig: Buffer.from(params.signedPreKey.signature).toString('base64url'),
|
|
87
|
+
fdid: params.phoneId,
|
|
88
|
+
network_ratio_type: '1',
|
|
89
|
+
expid: params.deviceId,
|
|
90
|
+
simnum: '1',
|
|
91
|
+
hasinrc: '1',
|
|
92
|
+
pid: Math.floor(Math.random() * 1000).toString(),
|
|
93
|
+
id: convertBufferToUrlHex(params.identityId),
|
|
94
|
+
backup_token: convertBufferToUrlHex(params.backupToken),
|
|
95
|
+
token: Buffer.from(md5(Buffer.concat([MOBILE_TOKEN, Buffer.from(params.phoneNumberNationalNumber)]))).toString('hex'),
|
|
96
|
+
fraud_checkpoint_code: params.captcha,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function mobileRegisterCode(params, fetchOptions) {
|
|
101
|
+
return mobileRegisterFetch('/code', {
|
|
102
|
+
params: {
|
|
103
|
+
...registrationParams(params),
|
|
104
|
+
mcc: `${params.phoneNumberMobileCountryCode}`.padStart(3, '0'),
|
|
105
|
+
mnc: `${params.phoneNumberMobileNetworkCode || '001'}`.padStart(3, '0'),
|
|
106
|
+
sim_mcc: '000',
|
|
107
|
+
sim_mnc: '000',
|
|
108
|
+
method: params?.method || 'sms',
|
|
109
|
+
reason: '',
|
|
110
|
+
hasav: '1',
|
|
111
|
+
},
|
|
112
|
+
...fetchOptions,
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function mobileRegisterExists(params, fetchOptions) {
|
|
117
|
+
return mobileRegisterFetch('/exist', { params: registrationParams(params), ...fetchOptions })
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function mobileRegister(params, fetchOptions) {
|
|
121
|
+
return mobileRegisterFetch('/register', {
|
|
122
|
+
params: { ...registrationParams(params), code: params.code.replace('-', '') },
|
|
123
|
+
...fetchOptions,
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function mobileRegisterEncrypt(data) {
|
|
128
|
+
const keypair = Curve.generateKeyPair()
|
|
129
|
+
const key = Curve.sharedKey(keypair.private, REGISTRATION_PUBLIC_KEY)
|
|
130
|
+
const buffer = aesEncryptGCM(Buffer.from(data), new Uint8Array(key), Buffer.alloc(12), Buffer.alloc(0))
|
|
131
|
+
return Buffer.concat([Buffer.from(keypair.public), buffer]).toString('base64url')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function mobileRegisterFetch(path, opts = {}) {
|
|
135
|
+
let url = `${MOBILE_REGISTRATION_ENDPOINT}${path}`
|
|
136
|
+
if (opts.params) {
|
|
137
|
+
const parameter = []
|
|
138
|
+
for (const param in opts.params) {
|
|
139
|
+
if (opts.params[param] !== null && opts.params[param] !== undefined) {
|
|
140
|
+
parameter.push(param + '=' + urlencode(opts.params[param]))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
url += `?${parameter.join('&')}`
|
|
144
|
+
delete opts.params
|
|
145
|
+
}
|
|
146
|
+
if (!opts.headers) opts.headers = {}
|
|
147
|
+
opts.headers['User-Agent'] = MOBILE_USERAGENT
|
|
148
|
+
const response = await axios(url, opts)
|
|
149
|
+
const json = response.data
|
|
150
|
+
if (response.status > 300 || json.reason) throw json
|
|
151
|
+
if (json.status && !['ok', 'sent'].includes(json.status)) throw json
|
|
152
|
+
return json
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { registrationParams }
|