Skip to content

Labity-LLC/pg-baileys

 
 

Repository files navigation

Authentication with PostgreSQL for Baileys based on https://github.com/bobslavtriev/mysql-baileys

Usage

1. Create table in PostgreSQL (optional)

If you want with your specifications, if you don't create it, the code will automatically create

CREATE TABLE "auth" (
	session VARCHAR(50) NOT NULL,
	id VARCHAR(80) NOT NULL,
	value TEXT DEFAULT NULL,
	UNIQUE (session, id)
);
CREATE INDEX idx_auth_session ON "auth" (session);
CREATE INDEX idx_auth_id ON "auth" (id);

2. Install pg-baileys

npm i pg-baileys

3. Import code

const { usePGAuthState } = require('pg-baileys')

4. Implement code

const { state, saveCreds, removeCreds } = await usePGAuthState({
	session: sessionName, // required
	password: 'Password123#', // required
	database: 'baileys', // required
})

5. All parameters for usePGAuthState()

type PGConfig = {
	/* The hostname of the database you are connecting to. (Default: localhost) */
	host?: string,
	/* The port number to connect to. (Default: 5432) */
	port?: number,
	/* The PostgreSQL user to authenticate as. (Default: postgres) */
	user?: string,
	/* The password of that PostgreSQL user */
	password: string,
	/* Name of the database to use for this connection. (Default: postgres) */
	database: string,
	/* PostgreSQL table name. (Default: auth) */
	tableName?: string,
	/* Retry the query at each interval if it fails. (Default: 200ms) */
	retryRequestDelayMs?: number,
	/* Maximum attempts if the query fails. (Default: 10) */
	maxRetries?: number,
	/* Session name to identify the connection, allowing multisessions with PostgreSQL. */
	session: string,
	/* Use the config SSL. (Default: disabled) */
	ssl?: boolean | { rejectUnauthorized: boolean }
}

Complete code for use

import { makeWASocket, makeCacheableSignalKeyStore, fetchLatestBaileysVersion } from 'baileys'
import { usePGAuthState } from 'pg-baileys'

async function startSock(sessionName){
	const { error, version } = await fetchLatestBaileysVersion()

	if (error){
		console.log(`Session: ${sessionName} | No connection, check your internet.`)
		return startSock(sessionName)
	}

	const { state, saveCreds, removeCreds } = await usePGAuthState({
		session: sessionName,
		host: 'localhost',
		port: 5432,
		user: 'postgres',
		password: 'Password123#',
		database: 'baileys',
		tableName: 'auth'
	})

	const sock = makeWASocket({
		auth: {
			creds: state.creds,
			keys: makeCacheableSignalKeyStore(state.keys, logger),
		},
		version: version,
		defaultQueryTimeoutMs: undefined
	})

	sock.ev.on('creds.update', saveCreds)

	sock.ev.on('connection.update', async({ connection, lastDisconnect }) => {
		// your code here
	})

	sock.ev.on('messages.upsert', async({ messages, type }) => {
		// your code here
	})
}

startSock('session1')

If you want to start other sessions in the same code, use this:

startSock('session1')
startSock('session2')
startSock('session3')
startSock('session4')

License

MIT

About

Authentication with PG for Baileys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%