@ -3,7 +3,6 @@ const {
Browsers ,
Browsers ,
DisconnectReason ,
DisconnectReason ,
makeCacheableSignalKeyStore ,
makeCacheableSignalKeyStore ,
makeInMemoryStore ,
getMessageFromStore ,
getMessageFromStore ,
useMultiFileAuthState ,
useMultiFileAuthState ,
downloadMediaMessage ,
downloadMediaMessage ,
@ -19,7 +18,9 @@ const generateId = require('../../utils/generateId.util');
const NodeCache = require ( 'node-cache' ) ;
const NodeCache = require ( 'node-cache' ) ;
const P = require ( 'pino' ) ;
const P = require ( 'pino' ) ;
// Ref: https://github.com/WhiskeySockets/Baileys/blob/v6.7.19/README.md
// Ref:
// https://github.com/WhiskeySockets/Baileys/blob/v6.7.19/README.md
// https://github.com/WhiskeySockets/Baileys/blob/v6.7.19/Example/example.ts
const createWhatsApp = async phone => {
const createWhatsApp = async phone => {
let qrCode = null ;
let qrCode = null ;
const channelId = generateId ( ) ;
const channelId = generateId ( ) ;
@ -31,21 +32,12 @@ const createWhatsApp = async phone => {
const logger = P ( { timestamp : ( ) => ` ,"time":" ${ new Date ( ) . toJSON ( ) } " ` } , P . destination ( './logs/wa-logs-' + phone + '.txt' ) ) ;
const logger = P ( { timestamp : ( ) => ` ,"time":" ${ new Date ( ) . toJSON ( ) } " ` } , P . destination ( './logs/wa-logs-' + phone + '.txt' ) ) ;
logger . level = 'warn' ;
logger . level = 'warn' ;
const msgRetryCounterCache = new NodeCache ( ) ;
const msgRetryCounterCache = new NodeCache ( ) ;
const storeFilename = './baileys_auth_info/baileys_store_' + phone + '.json'
const store = makeInMemoryStore ( { logger } ) ;
store ? . readFromFile ( storeFilename ) ;
// save every 10s
setInterval ( ( ) => {
store ? . writeToFile ( storeFilename ) ;
} , 10_000 ) ;
const authStateFolder = './baileys_auth_info/' + phone ;
const authStateFolder = './baileys_auth_info/' + phone ;
const { state , saveCreds } = await useMultiFileAuthState ( authStateFolder ) ;
const { state , saveCreds } = await useMultiFileAuthState ( authStateFolder ) ;
// https://git.../WhiskeySockets/.../src/Defaults/baileys-version.json
// https://git.../WhiskeySockets/.../src/Utils/generics.ts
// https://git.../WhiskeySockets/.../src/Utils/generics.ts
// const fetchLatestWaWebVersion
// https://web.whatsapp.com/sw.js, client_revision: 1027426813
// https://web.whatsapp.com/sw.js, client_revision: 1027426813
const { version , isLatest , } = { version : [ 2 , 3000 , 1027426813 ] , isLatest : true } ;
const whatsAppVersion = [ 2 , 3000 , 1027426813 ] ;
const waVersion = version . join ( '.' ) + ', ' + ( isLatest ? 'latest' : 'maybe' ) ;
const stop = ( ) => {
const stop = ( ) => {
@ -57,21 +49,13 @@ const createWhatsApp = async phone => {
}
}
} ) ;
} ) ;
fs . unlink ( storeFilename , ( err ) => {
if ( err && err . code !== 'ENOENT' ) {
console . error ( ` Error deleting storeFilename file: ${ err . message } ` ) ;
} else if ( ! err ) {
console . log ( 'Successfully deleted storeFilename file: ' , storeFilename ) ;
}
} ) ;
waEmitter . emit ( 'request.' + whatsAppNo + '.stop' , { } ) ;
waEmitter . emit ( 'request.' + whatsAppNo + '.stop' , { } ) ;
} ;
} ;
const start = ( ) => {
const start = ( ) => {
const waSocket = makeWASocket ( {
const waSocket = makeWASocket ( {
version ,
version : whatsAppVersion ,
logger ,
logger ,
auth : {
auth : {
creds : state . creds ,
creds : state . creds ,
@ -89,8 +73,6 @@ const createWhatsApp = async phone => {
markOnlineOnConnect : false // Receive Notifications in Whatsapp App
markOnlineOnConnect : false // Receive Notifications in Whatsapp App
} ) ;
} ) ;
store ? . bind ( waSocket . ev ) ;
const getGroupSubject = async jid => {
const getGroupSubject = async jid => {
const cachedMatadata = groupCache . get ( jid ) ;
const cachedMatadata = groupCache . get ( jid ) ;
@ -449,7 +431,7 @@ const createWhatsApp = async phone => {
return {
return {
createTimestamp : Date . now ( ) ,
createTimestamp : Date . now ( ) ,
status : 'offline' ,
status : 'offline' ,
version : w aVersion,
version : w h atsApp Version. join ( '.' ) + ', maybe' ,
channelId : channelId ,
channelId : channelId ,
phone : phone ,
phone : phone ,
start ,
start ,