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,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')
}
}

View File

@@ -0,0 +1,7 @@
```
throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails));
```
Problema sucede cuando usas la misma sesion luego de reiniciar el bot más de 3 veces lo mejor es seguir los pasos
- Eliminar **.wwebjs_auth**