Implementation of MySQL in Baileys.
npm install mysql-baileyssql
CREATE TABLE auth (
session varchar(50) NOT NULL,
id varchar(100) NOT NULL,
value LONGTEXT DEFAULT NULL,
UNIQUE KEY idxunique (session,id),
KEY idxsession (session),
KEY idxid (id)
) ENGINE=MyISAM
`$3
Edge Version:
`
npm i github:bobslavtriev/mysql-baileys
`
Stable Version:
`
npm i mysql-baileys
`$3
`ts
const { useMySQLAuthState } = require('mysql-baileys')
`$3
`ts
const { state, saveCreds, removeCreds } = await useMySQLAuthState({
session: sessionName, // required
password: 'Password123#', // required
database: 'baileys', // required
})
`$3
`ts
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
`ts
import { makeWASocket, makeCacheableSignalKeyStore, fetchLatestBaileysVersion } from 'baileys'
import { useMySQLAuthState } from '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')
`$3
`ts
startSock('session1')
startSock('session2')
startSock('session3')
startSock('session4')
``