diff --git a/packages/bot/context/state.class.js b/packages/bot/context/state.class.js index 23bbcd4b0..d724c8dae 100644 --- a/packages/bot/context/state.class.js +++ b/packages/bot/context/state.class.js @@ -8,8 +8,14 @@ class SingleState { * @returns */ updateState = (ctx = {}) => { - const currentStateByFrom = this.STATE.get(ctx.from) - return (keyValue) => this.STATE.set(ctx.from, { ...currentStateByFrom, ...keyValue }) + return (keyValue) => { + return new Promise((resolve) => { + const currentStateByFrom = this.STATE.get(ctx.from) + const updatedState = { ...currentStateByFrom, ...keyValue } + this.STATE.set(ctx.from, updatedState) + resolve() + }) + } } /** diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 7321c8978..68aa06e77 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -17,9 +17,6 @@ const loggerQueue = new Console({ stdout: createWriteStream(`${process.cwd()}/queue.class.log`), }) -const StateHandler = new SingleState() -const GlobalStateHandler = new GlobalState() - /** * [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos * [ ] Guardar historial en db @@ -31,6 +28,8 @@ class CoreClass { databaseClass providerClass queuePrincipal + stateHandler = new SingleState() + globalStateHandler = new GlobalState() generalArgs = { blackList: [], listEvents: {}, @@ -54,9 +53,9 @@ class CoreClass { this.generalArgs.queue.timeout ) - GlobalStateHandler.updateState()(this.generalArgs.globalState) + this.globalStateHandler.updateState()(this.generalArgs.globalState) - if (this.generalArgs.extensions) GlobalStateHandler.RAW = this.generalArgs.extensions + if (this.generalArgs.extensions) this.globalStateHandler.RAW = this.generalArgs.extensions for (const { event, func } of this.listenerBusEvents()) { this.providerClass.on(event, func) @@ -122,21 +121,21 @@ class CoreClass { // ๐Ÿ“„ Mantener estado de conversacion por numero const state = { - getMyState: StateHandler.getMyState(messageCtxInComming.from), - getAllState: StateHandler.getAllState, - update: StateHandler.updateState(messageCtxInComming), - clear: StateHandler.clear(messageCtxInComming.from), + getMyState: this.stateHandler.getMyState(messageCtxInComming.from), + getAllState: this.stateHandler.getAllState, + update: this.stateHandler.updateState(messageCtxInComming), + clear: this.stateHandler.clear(messageCtxInComming.from), } // ๐Ÿ“„ Mantener estado global const globalState = { - getMyState: GlobalStateHandler.getMyState(), - getAllState: GlobalStateHandler.getAllState, - update: GlobalStateHandler.updateState(messageCtxInComming), - clear: GlobalStateHandler.clear(), + getMyState: this.globalStateHandler.getMyState(), + getAllState: this.globalStateHandler.getAllState, + update: this.globalStateHandler.updateState(messageCtxInComming), + clear: this.globalStateHandler.clear(), } - const extensions = GlobalStateHandler.RAW + const extensions = this.globalStateHandler.RAW // ๐Ÿ“„ Crar CTX de mensaje (uso private) const createCtxMessage = (payload = {}, index = 0) => { diff --git a/packages/bot/package.json b/packages/bot/package.json index 7f59d959c..b089ec66f 100644 --- a/packages/bot/package.json +++ b/packages/bot/package.json @@ -1,6 +1,6 @@ { "name": "@bot-whatsapp/bot", - "version": "0.0.167-alpha.0", + "version": "0.0.171-alpha.0", "description": "", "main": "./lib/bundle.bot.cjs", "scripts": { diff --git a/packages/provider/package.json b/packages/provider/package.json index 48b0881d2..d55f16bb0 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -1,6 +1,6 @@ { "name": "@bot-whatsapp/provider", - "version": "0.0.132-alpha.0", + "version": "0.0.137-alpha.0", "description": "Esto es el conector a Twilio, Meta, etc...", "main": "./lib/mock/index.cjs", "keywords": [], diff --git a/packages/provider/src/meta/server.js b/packages/provider/src/meta/server.js index 9b72e2728..c9be1930b 100644 --- a/packages/provider/src/meta/server.js +++ b/packages/provider/src/meta/server.js @@ -7,14 +7,13 @@ const { getMediaUrl } = require('./utils') class MetaWebHookServer extends EventEmitter { constructor(jwtToken, numberId, version, token, metaPort = 3000) { super() - this.metaServer = polka() this.metaPort = metaPort this.token = token this.jwtToken = jwtToken this.numberId = numberId this.version = version - this.buildHTTPServer() + this.metaServer = this.buildHTTPServer() } /** @@ -232,7 +231,7 @@ class MetaWebHookServer extends EventEmitter { * Contruir HTTP Server */ buildHTTPServer() { - this.metaServer + return polka() .use(urlencoded({ extended: true })) .use(json()) .get('/', this.emptyCtrl) diff --git a/packages/provider/src/twilio/index.js b/packages/provider/src/twilio/index.js index e19d2c932..b7c5940b9 100644 --- a/packages/provider/src/twilio/index.js +++ b/packages/provider/src/twilio/index.js @@ -17,7 +17,7 @@ const { parseNumber } = require('./utils') const PORT = process.env.PORT || 3000 class TwilioProvider extends ProviderClass { - twilioHook + twilioServer vendor vendorNumber publicUrl @@ -25,14 +25,14 @@ class TwilioProvider extends ProviderClass { super() this.publicUrl = publicUrl this.vendor = new twilio(accountSid, authToken) - this.twilioHook = new TwilioWebHookServer(port) + this.twilioServer = new TwilioWebHookServer(port) this.vendorNumber = parseNumber(vendorNumber) - this.twilioHook.start() + this.twilioServer.start() const listEvents = this.busEvents() for (const { event, func } of listEvents) { - this.twilioHook.on(event, func) + this.twilioServer.on(event, func) } } @@ -75,6 +75,7 @@ class TwilioProvider extends ProviderClass { `[NOTA]: Estas intentando enviar una fichero que esta en local.`, `[NOTA]: Para que esto funcione con Twilio necesitas que el fichero este en una URL publica`, `[NOTA]: mรกs informacion aqui https://bot-whatsapp.netlify.app/docs/provider-twilio/`, + `[NOTA]: Esta es la url que se enviara a twilio (debe ser publica) ${urlEncode}`, ].join('\n') if ( @@ -117,12 +118,12 @@ class TwilioProvider extends ProviderClass { /** * - * @param {*} userId + * @param {*} number * @param {*} message * @param {*} param2 * @returns */ - sendMessage = async (number, message, { options }) => { + sendMessage = async (number, message, { options } = { options: {} }) => { number = parseNumber(number) if (options?.buttons?.length) this.sendButtons(number, message, options.buttons) if (options?.media) return this.sendMedia(number, message, options.media) diff --git a/packages/provider/src/twilio/server.js b/packages/provider/src/twilio/server.js index 2080bbe86..5906dafda 100644 --- a/packages/provider/src/twilio/server.js +++ b/packages/provider/src/twilio/server.js @@ -27,6 +27,7 @@ class TwilioWebHookServer extends EventEmitter { incomingMsg = (req, res) => { const { body } = req this.emit('message', { + ...body, from: parseNumber(body.From), to: parseNumber(body.To), body: body.Body,