mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add WA Web version auto fetch
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import P from 'pino'
|
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
|
// the store maintains the data of the WA connection in memory
|
||||||
// can be written out to a file & read from it
|
// can be written out to a file & read from it
|
||||||
@@ -14,9 +14,13 @@ setInterval(() => {
|
|||||||
const { state, saveState } = useSingleFileLegacyAuthState('./auth_info.json')
|
const { state, saveState } = useSingleFileLegacyAuthState('./auth_info.json')
|
||||||
|
|
||||||
// start a connection
|
// 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({
|
const sock = makeWALegacySocket({
|
||||||
|
version,
|
||||||
logger: P({ level: 'debug' }),
|
logger: P({ level: 'debug' }),
|
||||||
printQRInTerminal: true,
|
printQRInTerminal: true,
|
||||||
auth: state
|
auth: state
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import P from 'pino'
|
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
|
// the store maintains the data of the WA connection in memory
|
||||||
// can be written out to a file & read from it
|
// can be written out to a file & read from it
|
||||||
@@ -14,9 +14,13 @@ setInterval(() => {
|
|||||||
const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json')
|
const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json')
|
||||||
|
|
||||||
// start a connection
|
// 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({
|
const sock = makeWASocket({
|
||||||
|
version,
|
||||||
logger: P({ level: 'trace' }),
|
logger: P({ level: 'trace' }),
|
||||||
printQRInTerminal: true,
|
printQRInTerminal: true,
|
||||||
auth: state,
|
auth: state,
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
|
import axios from 'axios'
|
||||||
import { randomBytes } from 'crypto'
|
import { randomBytes } from 'crypto'
|
||||||
import { platform, release } from 'os'
|
import { platform, release } from 'os'
|
||||||
import { Logger } from 'pino'
|
import { Logger } from 'pino'
|
||||||
import { proto } from '../../WAProto'
|
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 { Binary } from '../WABinary'
|
||||||
import { ConnectionState } from '..'
|
|
||||||
|
|
||||||
const PLATFORM_MAP = {
|
const PLATFORM_MAP = {
|
||||||
'aix': 'AIX',
|
'aix': 'AIX',
|
||||||
@@ -40,7 +41,6 @@ export const BufferJSON = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const writeRandomPadMax16 = (e: Binary) => {
|
export const writeRandomPadMax16 = (e: Binary) => {
|
||||||
function r(e: Binary, t: number) {
|
function r(e: Binary, t: number) {
|
||||||
for(var r = 0; r < t; r++) {
|
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 delay = (ms: number) => delayCancellable (ms).delay
|
||||||
|
|
||||||
export const delayCancellable = (ms: number) => {
|
export const delayCancellable = (ms: number) => {
|
||||||
const stack = new Error().stack
|
const stack = new Error().stack
|
||||||
let timeout: NodeJS.Timeout
|
let timeout: NodeJS.Timeout
|
||||||
@@ -231,4 +232,25 @@ export const printQRIfNecessaryListener = (ev: CommonBaileysEventEmitter<any>, l
|
|||||||
QR?.generate(qr, { small: true })
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user