forked from codigoencasa/builderbot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): ⚡ added new venom provider
- Loading branch information
1 parent
fee7c2e
commit 01fe9eb
Showing
6 changed files
with
583 additions
and
472 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
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
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 |
---|---|---|
@@ -1,25 +1,64 @@ | ||
const { ProviderClass } = require('@bot-whatsapp/bot') | ||
const venom = require('venom-bot') | ||
const { cleanNumber } = require('../web-whatsapp/utils') | ||
const { cleanNumber, generateImage } = require('./utils/utils') | ||
|
||
class VenomProvider extends ProviderClass { | ||
constructor() { | ||
super() | ||
this.client | ||
this.vendor | ||
venom | ||
.create({ | ||
session: 'session-1', //nombre de la sesion o id | ||
multidevice: true, // Para el funcinamiento de multiusuarios.(default: true) | ||
.create( | ||
{ | ||
session: 'session-1', //nombre de la sesion o id | ||
multidevice: true, | ||
}, | ||
(base64Qrimg) => this.generateQr(base64Qrimg) | ||
) | ||
.then((client) => { | ||
this.vendor = client | ||
this.start() | ||
}) | ||
.then((client) => (this.client = client)) | ||
.catch((erro) => { | ||
console.log(erro) | ||
}) | ||
} | ||
|
||
generateQr = (qr) => { | ||
this.emit('require_action', { | ||
instructions: [ | ||
`Debes escanear el QR Code para iniciar session reivsa qr.svg`, | ||
`Recuerda que el QR se actualiza cada minuto `, | ||
`Necesitas ayuda: https://link.codigoencasa.com/DISCORD`, | ||
], | ||
}) | ||
//generateImage(qr) | ||
} | ||
|
||
start = () => { | ||
this.vendor.onMessage((payload) => { | ||
payload.from = cleanNumber(payload.from, true) | ||
this.emit('message', payload) | ||
}) | ||
} | ||
|
||
sendMessage = async (number, message) => { | ||
const numero = cleanNumber(number) | ||
return this.client.sendText(numero, message) | ||
} | ||
|
||
sendButtons = async (number, message, buttons = []) => { | ||
const buttonMessage = new Buttons(message, buttons, '', '') | ||
return this.vendor.sendMessage(number, buttonMessage) | ||
} | ||
|
||
sendMessage = async (userId, message, { options }) => { | ||
console.log('entramos') | ||
const number = cleanNumber(userId) | ||
if (options?.buttons?.length) | ||
return this.sendButtons(number, message, options.buttons) | ||
if (options?.media) return this.sendMedia(number, options.media) | ||
return this.vendor.sendText(number, message) | ||
} | ||
} | ||
|
||
module.exports = VenomProvider |
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,14 @@ | ||
const qr = require('qr-image') | ||
|
||
const cleanNumber = (number, full = false) => { | ||
number = number.replace('@c.us', '') | ||
number = !full ? `${number}@c.us` : `${number}` | ||
return number | ||
} | ||
|
||
const generateImage = (base64) => { | ||
let qr_svg = qr.image(base64, { type: 'svg', margin: 4 }) | ||
qr_svg.pipe(createWriteStream(`${process.cwd()}/qr.svg`)) | ||
} | ||
|
||
module.exports = { cleanNumber, generateImage } |
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
Oops, something went wrong.