Skip to content

Commit

Permalink
build: mejorando
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Sep 21, 2023
1 parent eade995 commit 751594f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 27 deletions.
10 changes: 8 additions & 2 deletions packages/bot/context/state.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}

/**
Expand Down
27 changes: 13 additions & 14 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +28,8 @@ class CoreClass {
databaseClass
providerClass
queuePrincipal
stateHandler = new SingleState()
globalStateHandler = new GlobalState()
generalArgs = {
blackList: [],
listEvents: {},
Expand All @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
@@ -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": [],
Expand Down
5 changes: 2 additions & 3 deletions packages/provider/src/meta/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions packages/provider/src/twilio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ const { parseNumber } = require('./utils')
const PORT = process.env.PORT || 3000

class TwilioProvider extends ProviderClass {
twilioHook
twilioServer
vendor
vendorNumber
publicUrl
constructor({ accountSid, authToken, vendorNumber, port = PORT, publicUrl = '' }) {
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)
}
}

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/provider/src/twilio/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 751594f

Please sign in to comment.