GithubHelp home page GithubHelp logo

queenenzaofc / mysql-baileys Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bobslavtriev/mysql-baileys

0.0 0.0 0.0 182 KB

Authentication with MySQL for Baileys

Home Page: https://bobslavtriev.github.io/mysql-baileys/

License: MIT License

TypeScript 100.00%

mysql-baileys's Introduction

Authentication with MySQL for Baileys

Usage

1. Create table in MySQL (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(100) NOT NULL,
	`value` json DEFAULT NULL,
	UNIQUE KEY `idxunique` (`session`,`id`),
	KEY `idxsession` (`session`),
	KEY `idxid` (`id`)
) ENGINE=MyISAM

2. Install mysql-baileys

Edge Version:

npm i github:bobslavtriev/mysql-baileys

Stable Version:

npm i bobslavtriev/mysql-baileys

3. Import code

const { useMySQLAuthState } = require('mysql-baileys')

4. Implement code

const { state, saveCreds, removeCreds } = await useMySQLAuthState({
	session: sessionName, // required
	host: 'localhost', // optional
	user: 'root', // optional
	password: 'Password123#', // required
	database: 'baileys', // required
	tableName: 'auth' // optional
})

5. All parameters for useMySQLAuthState()

type MySQLConfig = {
	/* Session name to identify the connection, allowing multisessions with mysql */
	session: string
	/* MySql host, by default localhost */
	host: string
	/* MySql user, by default root */
	user: string
	/* MySql password */
	password: string
	/* MySql database name */
	database: string
	/* MySql table name, by default auth */
	tableName: string | undefined
	/* Always keep active, by default 30s */
	keepAliveIntervalMs: number | undefined
	/* Retry the query at each interval if it fails, by default 200ms */
	retryRequestDelayMs: number | undefined
	/* Maximum attempts if the query fails, by default 10 */
	maxtRetries: number | undefined
	/* MySql SSL config */
	ssl?: string | SslOptions | undefined
}

Complete code for use

const { makeWASocket, makeCacheableSignalKeyStore, fetchLatestBaileysVersion } = require('@whiskeysockets/Baileys')
const { useMySQLAuthState } = require('mysql-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 useMySQLAuthState({
		session: sessionName, // required
		host: 'localhost', // optional
		user: 'root', // optional
		password: 'Password123#', // required
		database: 'baileys', // required
		tableName: 'auth' // optional
	})

	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')

mysql-baileys's People

Contributors

bobslavtriev avatar pedrohjp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.