diff --git a/Example/example-legacy.ts b/Example/example-legacy.ts index bae2b89..07acebd 100644 --- a/Example/example-legacy.ts +++ b/Example/example-legacy.ts @@ -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 diff --git a/Example/example.ts b/Example/example.ts index 2a89d22..b8820db 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -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, diff --git a/src/Utils/generics.ts b/src/Utils/generics.ts index 566c377..35183aa 100644 --- a/src/Utils/generics.ts +++ b/src/Utils/generics.ts @@ -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, 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 + } + } } \ No newline at end of file