Skip to content

neoxr/session

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

BAILEYS SESSION

Saving multi session baileys into an external database to save resources.

Baileys Module Supported

  • baileys npm
  • @whiskeysockets/baileys npm
  • @neoxr/baileys npm (Recommended)

Database Provider

  • PostgreSQL
  • SQL Lite (Recommended)
  • MySQL
  • MongoDB
  • Firebase (Firestore) (Recommended)

Installation

The following is the installation for each database.

PostgreSQL

Add this to dependencies package.json :

"session": "github:neoxr/session#postgresql" 

Then call the default function in your bot connection file

const { usePostgresAuthState } = require('session')

async function start() {
   const { state, saveCreds, deleteCreds, autoDeleteOldData } = await usePostgresAuthState('postgres://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
   
   const client = makeWASocket({
      // your configuration
   })
   
   client.ev.on('connection.update', async (session) => {
      if (session.reason === 401) {
         await deleteCreds()
         throw new Error('Device Logout')
      }
   })
   
   client.ev.on('creds.update', saveCreds)
   
   setInterval(async () => {
      await autoDeleteOldData()
   }, 1 * 60 * 60 * 1000) // checking every 1 hour
   
   // your code ...
}

SQL Lite

Add this to dependencies package.json :

"session": "github:neoxr/session#sqlite" 

Then call the default function in your bot connection file

const { useSQLiteAuthState } = require('session')

async function start() {
   const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useSQLiteAuthState('session.db', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
   
   const client = makeWASocket({
      // your configuration
   })
   
   client.ev.on('connection.update', async (session) => {
      if (session.reason === 401) {
         await deleteCreds()
         throw new Error('Device Logout')
      }
   })
   
   client.ev.on('creds.update', saveCreds)
   
   setInterval(async () => {
      await autoDeleteOldData()
   }, 1 * 60 * 60 * 1000) // checking every 1 hour
   
   // your code ...
}

MySQL

Add this to dependencies package.json :

"session": "github:neoxr/session#mysql" 

Then call the default function in your bot connection file

const { useMySQLAuthState } = require('session')

async function start() {
   const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useMySQLAuthState('mysql://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
   
   const client = makeWASocket({
      // your configuration
   })
   
   client.ev.on('connection.update', async (session) => {
      if (session.reason === 401) {
         await deleteCreds()
         throw new Error('Device Logout')
      }
   })
   
   client.ev.on('creds.update', saveCreds)
   
   setInterval(async () => {
      await autoDeleteOldData()
   }, 1 * 60 * 60 * 1000) // checking every 1 hour
   
   // your code ...
}

MongoDB

Add this to dependencies package.json :

"session": "github:neoxr/session#mongo" 

Then call the default function in your bot connection file

const { useMongoAuthState } = require('session')

async function start() {
   const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useMongoAuthState('mongodb://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
   
   const client = makeWASocket({
      // your configuration
   })
   
   client.ev.on('connection.update', async (session) => {
      if (session.reason === 401) {
         await deleteCreds()
         throw new Error('Device Logout')
      }
   })
   
   client.ev.on('creds.update', saveCreds)
   
   setInterval(async () => {
      await autoDeleteOldData()
   }, 1 * 60 * 60 * 1000) // checking every 1 hour
   
   // your code ...
}

Firebase (Firestore)

Add this to dependencies package.json :

"session": "github:neoxr/session#firebase" 

Then call the default function in your bot connection file

const { useFirebaseAuthState } = require('session')
const fs = require('fs')
const firebaseConfig = JSON.parse(fs.readFileSync('./firebase.json', 'utf-8))

async function start() {
   const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useFirebaseAuthState(firebaseConfig, 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
   
   const client = makeWASocket({
      // your configuration
   })
   
   client.ev.on('connection.update', async (session) => {
      if (session.reason === 401) {
         await deleteCreds()
         throw new Error('Device Logout')
      }
   })
   
   client.ev.on('creds.update', saveCreds)
   
   setInterval(async () => {
      await autoDeleteOldData()
   }, 1 * 60 * 60 * 1000) // checking every 1 hour
   
   // your code ...
}

Explanation

Notes :

  • deleteCreds : used when the device logout or certain conditions require re-pairing

  • autoDeleteOldData : used to delete sessions by excluding creds and app-state-sync

If there is an error make an issue, this script was made by neoxr and I hope this script is useful for everyone.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published