Skip to content

Commit

Permalink
feat(provider): ⚡ added new venom provider
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcasta0398 committed Dec 13, 2022
1 parent fee7c2e commit 01fe9eb
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 472 deletions.
3 changes: 2 additions & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"exports": {
"./mock": "./lib/mock/index.cjs",
"./twilio": "./lib/twilio/index.cjs",
"./web-whatsapp": "./lib/web-whatsapp/index.cjs"
"./web-whatsapp": "./lib/web-whatsapp/index.cjs",
"./venom": "./lib/venom/index.cjs"
}
}
9 changes: 9 additions & 0 deletions packages/provider/rollup-provider.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ module.exports = [
},
plugins: [commonjs()],
},
{
input: join(__dirname, 'src', 'venom', 'index.js'),
output: {
banner: banner['banner.output'].join(''),
file: join(__dirname, 'lib', 'venom', 'index.cjs'),
format: 'cjs',
},
plugins: [commonjs()],
},
]
51 changes: 45 additions & 6 deletions packages/provider/src/venom/index.js
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
14 changes: 14 additions & 0 deletions packages/provider/src/venom/utils/utils.js
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 }
1 change: 1 addition & 0 deletions packages/provider/src/web-whatsapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class WebWhatsappProvider extends ProviderClass {
for (const { event, func } of listEvents) {
this.vendor.on(event, func)
}

this.vendor.emit('preinit')
this.vendor.initialize().catch((e) => {
logger.log(e)
Expand Down
Loading

0 comments on commit 01fe9eb

Please sign in to comment.