feat: add WA Web version auto fetch

This commit is contained in:
Adhiraj Singh
2022-02-20 19:43:15 +05:30
parent 7c5071d8d5
commit 75582e4541
3 changed files with 39 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
import { Boom } from '@hapi/boom'
import P from 'pino'
import { AnyMessageContent, delay, DisconnectReason, makeInMemoryStore, makeWALegacySocket, useSingleFileLegacyAuthState } from '../src'
import { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, makeWALegacySocket, useSingleFileLegacyAuthState } from '../src'
// the store maintains the data of the WA connection in memory
// can be written out to a file & read from it
@@ -14,9 +14,13 @@ setInterval(() => {
const { state, saveState } = useSingleFileLegacyAuthState('./auth_info.json')
// start a connection
const startSock = () => {
const startSock = async() => {
// fetch latest version of WA Web
const { version, isLatest } = await fetchLatestBaileysVersion()
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`)
const sock = makeWALegacySocket({
version,
logger: P({ level: 'debug' }),
printQRInTerminal: true,
auth: state

View File

@@ -1,6 +1,6 @@
import { Boom } from '@hapi/boom'
import P from 'pino'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, makeInMemoryStore, useSingleFileAuthState } from '../src'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, useSingleFileAuthState } from '../src'
// the store maintains the data of the WA connection in memory
// can be written out to a file & read from it
@@ -14,9 +14,13 @@ setInterval(() => {
const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json')
// start a connection
const startSock = () => {
const startSock = async() => {
// fetch latest version of WA Web
const { version, isLatest } = await fetchLatestBaileysVersion()
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`)
const sock = makeWASocket({
version,
logger: P({ level: 'trace' }),
printQRInTerminal: true,
auth: state,

View File

@@ -1,11 +1,12 @@
import { Boom } from '@hapi/boom'
import axios from 'axios'
import { randomBytes } from 'crypto'
import { platform, release } from 'os'
import { Logger } from 'pino'
import { proto } from '../../WAProto'
import { CommonBaileysEventEmitter, DisconnectReason } from '../Types'
import { version as baileysVersion } from '../Defaults/baileys-version.json'
import { CommonBaileysEventEmitter, ConnectionState, DisconnectReason, WAVersion } from '../Types'
import { Binary } from '../WABinary'
import { ConnectionState } from '..'
const PLATFORM_MAP = {
'aix': 'AIX',
@@ -40,7 +41,6 @@ export const BufferJSON = {
}
}
export const writeRandomPadMax16 = (e: Binary) => {
function r(e: Binary, t: number) {
for(var r = 0; r < t; r++) {
@@ -144,6 +144,7 @@ export const debouncedTimeout = (intervalMs: number = 1000, task: () => void = u
}
export const delay = (ms: number) => delayCancellable (ms).delay
export const delayCancellable = (ms: number) => {
const stack = new Error().stack
let timeout: NodeJS.Timeout
@@ -231,4 +232,25 @@ export const printQRIfNecessaryListener = (ev: CommonBaileysEventEmitter<any>, l
QR?.generate(qr, { small: true })
}
})
}
/**
* utility that fetches latest baileys version from the master branch.
* Use to ensure your WA connection is always on the latest version
*/
export const fetchLatestBaileysVersion = async() => {
const URL = 'https://raw.githubusercontent.com/adiwajshing/Baileys/master/src/Defaults/baileys-version.json'
try {
const result = await axios.get<{ version: WAVersion }>(URL, { responseType: 'json' })
return {
version: result.data.version,
isLatest: true
}
} catch(error) {
return {
version: baileysVersion as WAVersion,
isLatest: false,
error
}
}
}