Skip to content

Commit

Permalink
chore(release): 0.1.23
Browse files Browse the repository at this point in the history
chore(release): 0.1.23
  • Loading branch information
leifermendez authored Feb 19, 2023
2 parents 5766001 + cb1f0f9 commit 535c05e
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 27 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.1.22](https://github.com/leifermendez/bot-whatsapp/compare/v0.1.21...v0.1.22) (2023-02-19)


### Features

* :sparkles: list Eventos ([7a5c4a2](https://github.com/leifermendez/bot-whatsapp/commit/7a5c4a2c415edd1f41e7830f257e5ef55f98764b))
* :zap: events list ([cd47cc5](https://github.com/leifermendez/bot-whatsapp/commit/cd47cc5f849e7b9fa04b20b75f251df3418734c9))
* **provider:** :zap: add listener EVENTS ([d3ce1e0](https://github.com/leifermendez/bot-whatsapp/commit/d3ce1e0f718ac573a562e84bd64b59fa0e4f23a0))

### [0.1.21](https://github.com/leifermendez/bot-whatsapp/compare/v0.1.20...v0.1.21) (2023-02-12)


Expand Down
90 changes: 85 additions & 5 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')
const suiteCase = suite('Flujo: enviando eventos')

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 All @@ -28,4 +28,84 @@ suiteCase(`Responder a "CURRENT_LOCATION"`, async ({ database, provider }) => {
assert.is(undefined, getHistory[1])
})

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

createBot({
database,
provider,
flow: createFlow([flow]),
})

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

await delay(200)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is('Gracias por tu documento', getHistory[0])
assert.is(undefined, getHistory[1])
})

suiteCase(`Responder a "EVENTS.WELCOME"`, async ({ database, provider }) => {
const flow = addKeyword(EVENTS.WELCOME).addAnswer('Bienvenido!')

createBot({
database,
provider,
flow: createFlow([flow]),
})

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

await delay(200)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is('Bienvenido!', getHistory[0])
assert.is(undefined, getHistory[1])
})

suiteCase(`Responder a "EVENTS.MEDIA"`, async ({ database, provider }) => {
const flow = addKeyword(EVENTS.MEDIA).addAnswer('gracias por la imagen o video!')

createBot({
database,
provider,
flow: createFlow([flow]),
})

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

await delay(200)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is('gracias por la imagen o video!', getHistory[0])
assert.is(undefined, getHistory[1])
})

suiteCase(`Responder a "EVENTS.VOICE_NOTE"`, async ({ database, provider }) => {
const flow = addKeyword(EVENTS.VOICE_NOTE).addAnswer('gracias por la nota de voz!')

createBot({
database,
provider,
flow: createFlow([flow]),
})

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

await delay(200)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is('gracias por la nota de voz!', getHistory[0])
assert.is(undefined, getHistory[1])
})

suiteCase.run()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/root",
"version": "0.1.21",
"version": "0.1.22",
"description": "Bot de wahtsapp open source para MVP o pequeños negocios",
"main": "app.js",
"private": true,
Expand Down
27 changes: 24 additions & 3 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { delay } = require('../utils/delay')
const Queue = require('../utils/queue')
const { Console } = require('console')
const { createWriteStream } = require('fs')
const { LIST_REGEX } = require('../io/events')

const logger = new Console({
stdout: createWriteStream(`${process.cwd()}/core.class.log`),
Expand All @@ -21,7 +22,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 +118,6 @@ class CoreClass {
endFlowFlag = true
if (message) this.sendProviderAndSave(from, createCtxMessage(message))
clearQueue()
sendFlow([])
return
}

Expand Down Expand Up @@ -249,7 +249,28 @@ 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 (LIST_REGEX.REGEX_EVENT_LOCATION.test(body)) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.LOCATION) || []
}

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

if (LIST_REGEX.REGEX_EVENT_DOCUMENT.test(body)) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.DOCUMENT) || []
}

if (LIST_REGEX.REGEX_EVENT_VOICE_NOTE.test(body)) {
msgToSend = this.flowClass.find(this.generalArgs.listEvents.VOICE_NOTE) || []
}
}
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 { LIST_ALL: 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,
}
9 changes: 9 additions & 0 deletions packages/bot/io/events/eventDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { generateRef } = require('../../utils/hash')

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

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

module.exports = { eventDocument, REGEX_EVENT_DOCUMENT }
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 }
9 changes: 9 additions & 0 deletions packages/bot/io/events/eventVoiceNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { generateRef } = require('../../utils/hash')

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

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

module.exports = { eventVoiceNote, REGEX_EVENT_VOICE_NOTE }
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 }
22 changes: 22 additions & 0 deletions packages/bot/io/events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { eventDocument, REGEX_EVENT_DOCUMENT } = require('./eventDocument')
const { eventLocation, REGEX_EVENT_LOCATION } = require('./eventLocation')
const { eventMedia, REGEX_EVENT_MEDIA } = require('./eventMedia')
const { eventVoiceNote, REGEX_EVENT_VOICE_NOTE } = require('./eventVoiceNote')
const { eventWelcome } = require('./eventWelcome')

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

const LIST_REGEX = {
REGEX_EVENT_DOCUMENT,
REGEX_EVENT_LOCATION,
REGEX_EVENT_MEDIA,
REGEX_EVENT_VOICE_NOTE,
}

module.exports = { LIST_ALL, LIST_REGEX }
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
25 changes: 22 additions & 3 deletions 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 All @@ -23,7 +24,7 @@ const logger = new Console({
* https://github.com/adiwajshing/Baileys
*/
class BaileysProvider extends ProviderClass {
globalVendorArgs = { name: `bot` }
globalVendorArgs = { name: `bot`, gifPlayback: false }
vendor
saveCredsGlobal = null
constructor(args) {
Expand Down Expand Up @@ -122,13 +123,31 @@ 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_') }
}
}

console.log(messageCtx.message)

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

//Detectar file
if (messageCtx.message?.documentMessage) {
payload = { ...payload, body: generateRefprovider('_event_document_') }
}

//Detectar voice note
if (messageCtx.message?.audioMessage) {
payload = { ...payload, body: generateRefprovider('_event_voice_note_') }
}

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

if (payload?.key?.fromMe) return
Expand Down Expand Up @@ -204,7 +223,7 @@ class BaileysProvider extends ProviderClass {
return this.vendor.sendMessage(number, {
video: readFileSync(filePath),
caption: text,
gifPlayback: true,
gifPlayback: this.globalVendorArgs.gifPlayback,
})
}

Expand Down
Loading

0 comments on commit 535c05e

Please sign in to comment.