refactor(provider): twilio + hook

This commit is contained in:
Leifer Mendez
2022-12-07 18:06:27 +01:00
parent 73caf090ba
commit eda8a67718
6 changed files with 46 additions and 22 deletions

View File

@@ -11,7 +11,7 @@
- [X] sensitivy viene activado por defecto
- [X] fallback respuesta en hijo: Se puede colocar en option el ref de la answer fallback
- [X] Cuando Envian Sticket devuelve mensaje raro
- [ ] addAnswer agregar delay
- [x] addAnswer agregar delay
- [ ] colocar mensaje esperando conectando whatsapp (provider)
- [ ] createDatabase validar implementacion de funciones
- [ ] limitar caracteres de mensajes 4000

View File

@@ -22,6 +22,8 @@ const createFlow = (args) => {
/**
* Crear instancia de clase Provider
* Depdendiendo del Provider puedes pasar argumentos
* Ver Documentacion
* @param {*} args
* @returns
*/

View File

@@ -1,13 +0,0 @@
# @bot-whatsapp/provider
```js
// bootstrap.js Como iniciar el provider
const { inout, provider, database } = require('@bot-whatsapp')
provider.start()
provider.close()
```
- [ ] whatsapp-web.js _verificar update_
- [ ] Meta _verificar tokens_
- [ ] Twilio _verificar tokens_

View File

@@ -11,10 +11,10 @@ class TwilioProvider extends ProviderClass {
twilioHook
vendor
vendorNumber
constructor({ accountSid, authToken, vendorNumber }) {
constructor({ accountSid, authToken, vendorNumber }, _port = 3000) {
super()
this.vendor = new twilio(accountSid, authToken)
this.twilioHook = new TwilioWebHookServer()
this.twilioHook = new TwilioWebHookServer(_port)
this.vendorNumber = vendorNumber
this.twilioHook.start()

View File

@@ -3,7 +3,24 @@ const polka = require('polka')
const { urlencoded } = require('body-parser')
const { parseNumber } = require('./utils')
/**
* Encargado de levantar un servidor HTTP con una hook url
* [POST] /twilio-hook
*/
class TwilioWebHookServer extends EventEmitter {
twilioServer
twilioPort
constructor(_twilioPort) {
this.twilioServer = this.buildHTTPServer()
this.twilioPort = _twilioPort
}
/**
* Mensaje entrante
* emit: 'message'
* @param {*} req
* @param {*} res
*/
incomingMsg = (req, res) => {
const { body } = req
this.emit('message', {
@@ -15,13 +32,31 @@ class TwilioWebHookServer extends EventEmitter {
res.end(json)
}
start = () => {
polka()
/**
* Contruir HTTP Server
* @returns
*/
buildHTTPServer = () => {
return polka()
.use(urlencoded({ extended: true }))
.post('/hook', this.incomingMsg)
.listen(3000, () => {
console.log(`> Running on localhost:3000 /hook`)
})
.post('/twilio-hook', this.incomingMsg)
}
/**
* Puerto del HTTP
* @param {*} port default 3000
*/
start = () => {
this.twilioServer.listen(this.twilioPort, () => {
console.log(``)
console.log(`[Twilio]: Agregar esta url "WHEN A MESSAGE COMES IN"`)
console.log(
`[Twilio]: http://localhost:${this.twilioPort}/twilio-hook`
)
console.log(`[Twilio]: Más información en la documentacion`)
console.log(``)
})
this.emit('ready')
}
}