forked from codigoencasa/builderbot
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Baileys WebSocket - Fast
- Loading branch information
Joseph VTX
committed
Dec 13, 2022
1 parent
f799498
commit 23b2e8e
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const { ProviderClass } = require('@bot-whatsapp/bot') | ||
const PINO = require('pino') | ||
const makeWASocket = require('@adiwajshing/baileys').default | ||
const { useMultiFileAuthState } = require('@adiwajshing/baileys') | ||
|
||
class Baileys extends ProviderClass { | ||
constructor() { | ||
super() | ||
this.sock | ||
} | ||
|
||
async baileys() { | ||
const { state, saveCreds } = await useMultiFileAuthState( | ||
'baileys_auth_whatsapp' | ||
) | ||
|
||
this.sock = await makeWASocket({ | ||
printQRInTerminal: true, | ||
auth: state, | ||
logger: PINO({ level: 'error' }), | ||
}) | ||
|
||
this.sock.ev.on( | ||
'connection.update', | ||
({ connection, lastDisconnect }) => { | ||
if (lastDisconnect?.error) { | ||
saveCreds() | ||
|
||
this.baileys() | ||
} | ||
|
||
if (connection === 'open') { | ||
console.log('Baileys is connected') | ||
} | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} number | ||
* @param {string} message | ||
* @example await sendMessage('+51925465621', 'Hello World') | ||
*/ | ||
async sendMessage(number, message) { | ||
const numberClean = number.replace('+', '') | ||
await this.sock.sendMessage(`${numberClean}@c.us`, { text: message }) | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} number | ||
* @param {string} message | ||
* @example await sendMessage('+51925465621', 'https://dominio.com/imagen.jpg' | 'img/imagen.jpg') | ||
*/ | ||
|
||
async sendImage(number, imageUrl) { | ||
const numberClean = number.replace('+', '') | ||
await this.sock.sendMessage(`${numberClean}@c.us`, { | ||
image: { url: imageUrl }, | ||
}) | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} number | ||
* @param {string} message | ||
* @param {boolean} voiceNote optional | ||
* @example await sendMessage('+51925465621', 'audio.mp3') | ||
*/ | ||
|
||
async sendAudio(number, audioUrl, voiceNote = false) { | ||
const numberClean = number.replace('+', '') | ||
await this.sock.sendMessage(`${numberClean}@c.us`, { | ||
audio: { url: audioUrl }, | ||
ptt: voiceNote, | ||
}) | ||
} | ||
} |