mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 04:59:15 +00:00
feat(bot): 🔥 improvement provider handler
This commit is contained in:
12
TODO.md
12
TODO.md
@@ -9,10 +9,8 @@
|
|||||||
- [X] .addKeyword('1') no funciona con 1 caracter
|
- [X] .addKeyword('1') no funciona con 1 caracter
|
||||||
- [X] sensitivy viene activado por defecto
|
- [X] sensitivy viene activado por defecto
|
||||||
- [X] fallback respuesta en hijo: Se puede colocar en option el ref de la answer fallback
|
- [X] fallback respuesta en hijo: Se puede colocar en option el ref de la answer fallback
|
||||||
- [X] envia imagen
|
- [X] Cuando Envian Sticket devuelve mensaje raro
|
||||||
- [ ] envia nota de voz
|
|
||||||
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
||||||
- [ ] Cuando Envian Sticket devuelve mensaje raro
|
|
||||||
- [ ] createDatabase validar implementacion de funciones
|
- [ ] createDatabase validar implementacion de funciones
|
||||||
- [ ] limitar caracteres de mensajes
|
- [ ] limitar caracteres de mensajes
|
||||||
|
|
||||||
@@ -26,8 +24,9 @@
|
|||||||
### @bot-whatsapp/provider
|
### @bot-whatsapp/provider
|
||||||
- [X] agregar export package
|
- [X] agregar export package
|
||||||
- [ ] __(doc):__ Video para explicar como implementar nuevos providers
|
- [ ] __(doc):__ Video para explicar como implementar nuevos providers
|
||||||
- [ ] WhatsappWeb provider enviar imagenes
|
- [X] WhatsappWeb provider enviar imagenes
|
||||||
- [ ] WhatsappWeb provider enviar audio
|
- [X] WhatsappWeb provider enviar audio
|
||||||
|
- [X] WhatsappWeb botones (Tiene truco) github:leifermendez/whatsapp-web.js
|
||||||
- [ ] Twilio adapter
|
- [ ] Twilio adapter
|
||||||
- [ ] Meta adapter
|
- [ ] Meta adapter
|
||||||
|
|
||||||
@@ -37,3 +36,6 @@
|
|||||||
|
|
||||||
### @bot-whatsapp/create-bot
|
### @bot-whatsapp/create-bot
|
||||||
- [ ]
|
- [ ]
|
||||||
|
|
||||||
|
### Extra
|
||||||
|
- [X] Crear CI mantener fork update https://stackoverflow.com/questions/23793062/can-forks-be-synced-automatically-in-github
|
||||||
@@ -55,6 +55,9 @@ class CoreClass {
|
|||||||
const { body, from } = messageInComming
|
const { body, from } = messageInComming
|
||||||
let msgToSend = []
|
let msgToSend = []
|
||||||
let fallBackFlag = false
|
let fallBackFlag = false
|
||||||
|
|
||||||
|
if (!body.length) return
|
||||||
|
|
||||||
const prevMsg = await this.databaseClass.getPrevByNumber(from)
|
const prevMsg = await this.databaseClass.getPrevByNumber(from)
|
||||||
const refToContinue = this.flowClass.findBySerialize(
|
const refToContinue = this.flowClass.findBySerialize(
|
||||||
prevMsg?.refSerialize
|
prevMsg?.refSerialize
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js')
|
const {
|
||||||
|
Client,
|
||||||
|
LocalAuth,
|
||||||
|
MessageMedia,
|
||||||
|
Buttons,
|
||||||
|
List,
|
||||||
|
} = require('whatsapp-web.js')
|
||||||
const { ProviderClass } = require('@bot-whatsapp/bot')
|
const { ProviderClass } = require('@bot-whatsapp/bot')
|
||||||
const { Console } = require('console')
|
const { Console } = require('console')
|
||||||
const { createWriteStream, existsSync } = require('fs')
|
const { createWriteStream, existsSync } = require('fs')
|
||||||
@@ -8,6 +14,11 @@ const logger = new Console({
|
|||||||
stdout: createWriteStream('./log'),
|
stdout: createWriteStream('./log'),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebWhatsappProvider: Es una clase tipo adaptor
|
||||||
|
* que extiende clases de ProviderClass (la cual es como interfaz para sber que funciones rqueridas)
|
||||||
|
* https://github.com/pedroslopez/whatsapp-web.js
|
||||||
|
*/
|
||||||
class WebWhatsappProvider extends ProviderClass {
|
class WebWhatsappProvider extends ProviderClass {
|
||||||
vendor
|
vendor
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -26,8 +37,9 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
logger.log(e)
|
logger.log(e)
|
||||||
this.emit('require_action', {
|
this.emit('require_action', {
|
||||||
instructions: [
|
instructions: [
|
||||||
`Debes eliminar la carpeta .wwebjs_auth`,
|
`(Opcion 1): Debes eliminar la carpeta .wwebjs_auth y reiniciar nuevamente el bot. `,
|
||||||
`y reiniciar nuevamente el bot `,
|
`(Opcion 2): Intenta actualizar el paquete [npm install whatsapp-web.js] `,
|
||||||
|
`(Opcion 3): Ir FORO de discord https://link.codigoencasa.com/DISCORD `,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -80,20 +92,85 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enviar un archivo multimedia
|
||||||
|
* https://docs.wwebjs.dev/MessageMedia.html
|
||||||
|
* @private
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} mediaInput
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
sendMedia = async (number, mediaInput = null) => {
|
sendMedia = async (number, mediaInput = null) => {
|
||||||
if (!existsSync(mediaInput))
|
if (!existsSync(mediaInput))
|
||||||
throw new Error(`NO_SE_ENCONTRO: ${mediaInput}`)
|
throw new Error(`NO_SE_ENCONTRO: ${mediaInput}`)
|
||||||
const media = MessageMedia.fromFilePath(mediaInput)
|
const media = MessageMedia.fromFilePath(mediaInput)
|
||||||
return this.vendor.sendMessage(number, media)
|
return this.vendor.sendMessage(number, media, {
|
||||||
|
sendAudioAsVoice: true,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enviar botones
|
||||||
|
* https://docs.wwebjs.dev/Buttons.html
|
||||||
|
* @private
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} message
|
||||||
|
* @param {*} buttons []
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
sendButtons = async (number, message, buttons = []) => {
|
||||||
|
const buttonMessage = new Buttons(message, buttons, '', '')
|
||||||
|
return this.vendor.sendMessage(number, buttonMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enviar lista
|
||||||
|
* https://docs.wwebjs.dev/List.html
|
||||||
|
* @private
|
||||||
|
* @alpha No funciona en whatsapp bussines
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} message
|
||||||
|
* @param {*} buttons []
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
sendList = async (number, message, listInput = []) => {
|
||||||
|
let sections = [
|
||||||
|
{
|
||||||
|
title: 'sectionTitle',
|
||||||
|
rows: [
|
||||||
|
{ title: 'ListItem1', description: 'desc' },
|
||||||
|
{ title: 'ListItem2' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
let list = new List('List body', 'btnText', sections, 'Title', 'footer')
|
||||||
|
return this.vendor.sendMessage(number, list)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enviar un mensaje solo texto
|
||||||
|
* https://docs.wwebjs.dev/Message.html
|
||||||
|
* @private
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} message
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
sendText = async (number, message) => {
|
sendText = async (number, message) => {
|
||||||
return this.vendor.sendMessage(number, message)
|
return this.vendor.sendMessage(number, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} userId
|
||||||
|
* @param {*} message
|
||||||
|
* @param {*} param2
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
sendMessage = async (userId, message, { options }) => {
|
sendMessage = async (userId, message, { options }) => {
|
||||||
const number = cleanNumber(userId)
|
const number = cleanNumber(userId)
|
||||||
if (options?.media) return this.sendMedia(number, options.media)
|
if (options?.media) return this.sendMedia(number, options.media)
|
||||||
|
if (options?.buttons?.length)
|
||||||
|
return this.sendButtons(number, message, options.buttons)
|
||||||
return this.sendText(number, message)
|
return this.sendText(number, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user