Skip to content

Commit

Permalink
feat: ⚡ events list
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Feb 16, 2023
1 parent 8d46862 commit cd47cc5
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 15 deletions.
8 changes: 4 additions & 4 deletions __test__/0.0.1-case.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { suite } = require('uvu')
const assert = require('uvu/assert')
const { addKeyword, createBot, createFlow } = require('../packages/bot/index')
const { addKeyword, createBot, createFlow, EVENTS } = require('../packages/bot/index')
const { setup, clear, delay } = require('../__mocks__/env')

const suiteCase = suite('Flujo: Provider envia un location')

suiteCase.before.each(setup)
suiteCase.after.each(clear)

suiteCase(`Responder a "CURRENT_LOCATION"`, async ({ database, provider }) => {
const flow = addKeyword('#CURRENT_LOCATION#').addAnswer('Gracias por tu location')
suiteCase(`Responder a "EVENTS.LOCATION"`, async ({ database, provider }) => {
const flow = addKeyword(EVENTS.LOCATION).addAnswer('Gracias por tu location')

createBot({
database,
Expand All @@ -19,7 +19,7 @@ suiteCase(`Responder a "CURRENT_LOCATION"`, async ({ database, provider }) => {

await provider.delaySendMessage(0, 'message', {
from: '000',
body: '#CURRENT_LOCATION#',
body: '_event_location__f405d946-cf07-uutt-l7e0-b6d475bc7f81',
})

await delay(200)
Expand Down
20 changes: 17 additions & 3 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { delay } = require('../utils/delay')
const Queue = require('../utils/queue')
const { Console } = require('console')
const { createWriteStream } = require('fs')
const { REGEX_EVENT_LOCATION } = require('../io/events/eventLocation')
const { REGEX_EVENT_MEDIA } = require('../io/events/eventMedia')

const logger = new Console({
stdout: createWriteStream(`${process.cwd()}/core.class.log`),
Expand All @@ -21,7 +23,7 @@ class CoreClass {
flowClass
databaseClass
providerClass
generalArgs = { blackList: [] }
generalArgs = { blackList: [], listEvents: {} }
constructor(_flow, _database, _provider, _args) {
this.flowClass = _flow
this.databaseClass = _database
Expand Down Expand Up @@ -117,7 +119,6 @@ class CoreClass {
endFlowFlag = true
if (message) this.sendProviderAndSave(from, createCtxMessage(message))
clearQueue()
sendFlow([])
return
}

Expand Down Expand Up @@ -249,7 +250,20 @@ class CoreClass {
}

msgToSend = this.flowClass.find(body) || []
sendFlow(msgToSend, from)
if (msgToSend.length) return sendFlow(msgToSend, from)

if (!prevMsg?.options?.capture) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.WELCOME) || []

if (REGEX_EVENT_LOCATION.test(body)) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.LOCATION) || []
}

if (REGEX_EVENT_MEDIA.test(body)) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.MEDIA) || []
}
}
return sendFlow(msgToSend, from)
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const CoreClass = require('./core/core.class')
const ProviderClass = require('./provider/provider.class')
const FlowClass = require('./io/flow.class')
const { addKeyword, addAnswer, addChild, toSerialize } = require('./io/methods')
const EVENTS = require('./io/events')

/**
* Crear instancia de clase Bot
* @param {*} args
* @returns
*/
const createBot = async ({ flow, database, provider }, args = {}) => new CoreClass(flow, database, provider, args)
const createBot = async ({ flow, database, provider }, args = {}) =>
new CoreClass(flow, database, provider, { ...args, listEvents: EVENTS })

/**
* Crear instancia de clase Io (Flow)
Expand Down Expand Up @@ -42,4 +44,5 @@ module.exports = {
toSerialize,
ProviderClass,
CoreClass,
EVENTS,
}
7 changes: 7 additions & 0 deletions packages/bot/io/events/eventDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { generateRef } = require('../../utils/hash')

const eventDocument = () => {
return generateRef('_event_document_')
}

module.exports = { eventDocument }
9 changes: 9 additions & 0 deletions packages/bot/io/events/eventLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { generateRef } = require('../../utils/hash')

const eventLocation = () => {
return generateRef('_event_location_')
}

const REGEX_EVENT_LOCATION = /^_event_location__[\w\d]{8}-(?:[\w\d]{4}-){3}[\w\d]{12}$/

module.exports = { eventLocation, REGEX_EVENT_LOCATION }
9 changes: 9 additions & 0 deletions packages/bot/io/events/eventMedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { generateRef } = require('../../utils/hash')

const eventMedia = () => {
return generateRef('_event_media_')
}

const REGEX_EVENT_MEDIA = /^_event_media__[\w\d]{8}-(?:[\w\d]{4}-){3}[\w\d]{12}$/

module.exports = { eventMedia, REGEX_EVENT_MEDIA }
7 changes: 7 additions & 0 deletions packages/bot/io/events/eventVoiceNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { generateRef } = require('../../utils/hash')

const eventVoiceNote = () => {
return generateRef('_event_voice_note_')
}

module.exports = { eventVoiceNote }
7 changes: 7 additions & 0 deletions packages/bot/io/events/eventWelcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { generateRef } = require('../../utils/hash')

const eventWelcome = () => {
return generateRef('_event_welcome_')
}

module.exports = { eventWelcome }
15 changes: 15 additions & 0 deletions packages/bot/io/events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { eventDocument } = require('./eventDocument')
const { eventLocation } = require('./eventLocation')
const { eventMedia } = require('./eventMedia')
const { eventVoiceNote } = require('./eventVoiceNote')
const { eventWelcome } = require('./eventWelcome')

const LIST_ALL = {
WELCOME: eventWelcome(),
MEDIA: eventMedia(),
LOCATION: eventLocation(),
DOCUMENT: eventDocument(),
VOICE_NOTE: eventVoiceNote(),
}

module.exports = LIST_ALL
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.100-alpha.0",
"version": "0.0.105-alpha.0",
"description": "",
"main": "./lib/bundle.bot.cjs",
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions packages/provider/common/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const crypto = require('crypto')

/**
* Generamos un UUID unico con posibilidad de tener un prefijo
* @param {*} prefix
* @returns
*/
const generateRefprovider = (prefix = false) => {
const id = crypto.randomUUID()
return prefix ? `${prefix}_${id}` : id
}

module.exports = { generateRefprovider }
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.70-alpha.0",
"version": "0.0.92-alpha.0",
"description": "Esto es el conector a Twilio, Meta, etc...",
"main": "./lib/mock/index.cjs",
"keywords": [],
Expand Down
9 changes: 8 additions & 1 deletion packages/provider/src/baileys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { default: makeWASocket, useMultiFileAuthState, Browsers, DisconnectReason
const { baileyGenerateImage, baileyCleanNumber, baileyIsValidNumber } = require('./utils')

const { generalDownload } = require('../../common/download')
const { generateRefprovider } = require('../../common/hash')

const logger = new Console({
stdout: createWriteStream(`${process.cwd()}/baileys.log`),
Expand Down Expand Up @@ -122,13 +123,19 @@ class BaileysProvider extends ProviderClass {
from: messageCtx?.key?.remoteJid,
}

//Detectar location
if (messageCtx.message.locationMessage) {
const { degreesLatitude, degreesLongitude } = messageCtx.message.locationMessage
if (typeof degreesLatitude === 'number' && typeof degreesLongitude === 'number') {
payload = { ...payload, body: `#CURRENT_LOCATION#` }
payload = { ...payload, body: generateRefprovider('_event_location_') }
}
}

//Detectar media
if (messageCtx.message?.imageMessage) {
payload = { ...payload, body: generateRefprovider('_event_media_') }
}

if (payload.from === 'status@broadcast') return

if (payload?.key?.fromMe) return
Expand Down
8 changes: 7 additions & 1 deletion packages/provider/src/venom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const logger = new Console({
})

const { generalDownload } = require('../../common/download')
const { generateRefprovider } = require('../../common/hash')

/**
* ⚙️ VenomProvider: Es una clase tipo adaptor
Expand Down Expand Up @@ -89,11 +90,16 @@ class VenomProvider extends ProviderClass {
return
}
payload.from = venomCleanNumber(payload.from, true)

if (payload.hasOwnProperty('type') && ['image', 'video'].includes(payload.type)) {
payload = { ...payload, body: generateRefprovider('_event_media_') }
}

if (payload.hasOwnProperty('lat') && payload.hasOwnProperty('lng')) {
const lat = payload.lat
const lng = payload.lng
if (lat !== '' && lng !== '') {
payload = { ...payload, body: `#CURRENT_LOCATION#` }
payload = { ...payload, body: generateRefprovider('_event_location_') }
}
}
this.emit('message', payload)
Expand Down
8 changes: 7 additions & 1 deletion packages/provider/src/web-whatsapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const logger = new Console({

const { generalDownload } = require('../../common/download')
const mime = require('mime-types')
const { generateRefprovider } = require('../../common/hash')

/**
* ⚙️ WebWhatsappProvider: Es una clase tipo adaptor
Expand Down Expand Up @@ -91,8 +92,13 @@ class WebWhatsappProvider extends ProviderClass {
}
payload.from = wwebCleanNumber(payload.from, true)
if (payload._data.lat && payload._data.lng) {
payload = { ...payload, body: `#CURRENT_LOCATION#` }
payload = { ...payload, body: generateRefprovider('_event_location_') }
}

if (payload._data.hasOwnProperty('type') && ['image', 'video'].includes(payload._data.type)) {
payload = { ...payload, body: generateRefprovider('_event_media_') }
}

this.emit('message', payload)
},
},
Expand Down
4 changes: 2 additions & 2 deletions starters/apps/base-venom-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"keywords": [],
"dependencies": {
"venom-bot": "4.3.7",
"mime-types": "2.1.35",
"@bot-whatsapp/bot": "latest",
"@bot-whatsapp/cli": "latest",
"@bot-whatsapp/database": "latest",
"@bot-whatsapp/provider": "latest",
"@bot-whatsapp/portal": "latest",
"mime-types": "2.1.35"
"@bot-whatsapp/portal": "latest"
},
"author": "",
"license": "ISC"
Expand Down

0 comments on commit cd47cc5

Please sign in to comment.