mysql-baileys
v1.5.1
Published
Implementation of MySQL in Baileys.
Downloads
435
Readme
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
password: 'Password123#', // required
database: 'baileys', // required
})
5. All parameters for useMySQLAuthState()
type MySQLConfig = {
/* The hostname of the database you are connecting to. (Default: localhost) */
host?: string,
/* The port number to connect to. (Default: 3306) */
port?: number,
/* The MySQL user to authenticate as. (Default: root) */
user?: string,
/* The password of that MySQL user */
password: string,
/* Alias for the MySQL user password. Makes a bit more sense in a multifactor authentication setup (see "password2" and "password3") */
password1?: string,
/* 2nd factor authentication password. Mandatory when the authentication policy for the MySQL user account requires an additional authentication method that needs a password. */
password2?: string,
/* 3rd factor authentication password. Mandatory when the authentication policy for the MySQL user account requires two additional authentication methods and the last one needs a password. */
password3?: string,
/* Name of the database to use for this connection. (Default: base) */
database: string,
/* MySql 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) */
maxtRetries?: number,
/* Session name to identify the connection, allowing multisessions with mysql. */
session: string,
/* The source IP address to use for TCP connection. */
localAddress?: string,
/* The path to a unix domain socket to connect to. When used host and port are ignored. */
socketPath?: string,
/* Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) */
insecureAuth?: boolean,
/* If your connection is a server. (Default: false) */
isServer?: boolean,
/* Use the config SSL. (Default: disabled) */
ssl?: string | SslOptions
}
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,
host: 'localhost',
port: 3306,
user: 'bob',
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')