Skip to content

Commit

Permalink
WEB WAM IMPLEMENTATION!!!!! (#708)
Browse files Browse the repository at this point in the history
* feat(WAM): initial commit

* fix(wam-development): I made a whoopsie

* Update Example/example.ts

Co-authored-by: Javier Cuevas <[email protected]>

---------

Co-authored-by: Javier Cuevas <[email protected]>
  • Loading branch information
PurpShell and javiercr authored Apr 28, 2024
1 parent e52570b commit 306e1c1
Show file tree
Hide file tree
Showing 8 changed files with 16,546 additions and 1 deletion.
924 changes: 924 additions & 0 deletions Example/boot_analytics_test.json

Large diffs are not rendered by default.

32 changes: 31 additions & 1 deletion Example/example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Boom } from '@hapi/boom'
import NodeCache from 'node-cache'
import readline from 'readline'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, makeCacheableSignalKeyStore, makeInMemoryStore, PHONENUMBER_MCC, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
import makeWASocket, { AnyMessageContent, BinaryInfo, delay, DisconnectReason, encodeWAM, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, makeCacheableSignalKeyStore, makeInMemoryStore, PHONENUMBER_MCC, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
import MAIN_LOGGER from '../src/Utils/logger'
import open from 'open'
import fs from 'fs'
Expand Down Expand Up @@ -176,6 +176,36 @@ const startSock = async() => {
console.log('Connection closed. You are logged out.')
}
}

// WARNING: THIS WILL SEND A WAM EXAMPLE AND THIS IS A ****CAPTURED MESSAGE.****
// DO NOT ACTUALLY ENABLE THIS UNLESS YOU MODIFIED THE FILE.JSON!!!!!
// THE ANALYTICS IN THE FILE ARE OLD. DO NOT USE THEM.
// YOUR APP SHOULD HAVE GLOBALS AND ANALYTICS ACCURATE TO TIME, DATE AND THE SESSION
// THIS FILE.JSON APPROACH IS JUST AN APPROACH I USED, BE FREE TO DO THIS IN ANOTHER WAY.
// THE FIRST EVENT CONTAINS THE CONSTANT GLOBALS, EXCEPT THE seqenceNumber(in the event) and commitTime
// THIS INCLUDES STUFF LIKE ocVersion WHICH IS CRUCIAL FOR THE PREVENTION OF THE WARNING
const sendWAMExample = false;
if(connection === 'open' && sendWAMExample) {
/// sending WAM EXAMPLE
const {
header: {
wamVersion,
eventSequenceNumber,
},
events,
} = JSON.parse(await fs.promises.readFile("./boot_analytics_test.json", "utf-8"))

const binaryInfo = new BinaryInfo({
protocolVersion: wamVersion,
sequence: eventSequenceNumber,
events: events
})

const buffer = encodeWAM(binaryInfo);

const result = await sock.sendWAMBuffer(buffer)
console.log(result)
}

console.log('connection update', update)
}
Expand Down
19 changes: 19 additions & 0 deletions src/Socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,24 @@ export const makeSocket = (config: SocketConfig) => {
return Buffer.concat([salt, randomIv, ciphered])
}

const sendWAMBuffer = (wamBuffer: Buffer) => {
return query({
tag: 'iq',
attrs: {
to: S_WHATSAPP_NET,
id: generateMessageTag(),
xmlns: 'w:stats'
},
content: [
{
tag: 'add',
attrs: {},
content: wamBuffer
}
]
})
}

ws.on('message', onMessageRecieved)
ws.on('open', async() => {
try {
Expand Down Expand Up @@ -723,6 +741,7 @@ export const makeSocket = (config: SocketConfig) => {
requestPairingCode,
/** Waits for the connection to WA to reach a state */
waitForConnectionUpdate: bindWaitForConnectionUpdate(ev),
sendWAMBuffer,
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/WAM/BinaryInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EventInputType } from './constants'

export class BinaryInfo {
protocolVersion = 5
sequence = 0
events = [] as EventInputType[]
buffer: Buffer[] = []

constructor(options: Partial<BinaryInfo> = {}) {
Object.assign(this, options)
}
}
Loading

0 comments on commit 306e1c1

Please sign in to comment.