mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-19 20:19:15 +00:00
fix(cli): ⚡ working flowDynamic test
This commit is contained in:
2
.vscode/extensions.json
vendored
2
.vscode/extensions.json
vendored
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"recommendations": ["xyc.vscode-mdx-preview"]
|
"recommendations": ["xyc.vscode-mdx-preview", "vivaxy.vscode-conventional-commits", "mhutchie.git-graph"]
|
||||||
}
|
}
|
||||||
|
|||||||
92
__test__/07-case.test.js
Normal file
92
__test__/07-case.test.js
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
const { test } = require('uvu')
|
||||||
|
const assert = require('uvu/assert')
|
||||||
|
const MOCK_DB = require('../packages/database/src/mock')
|
||||||
|
const PROVIDER_DB = require('../packages/provider/src/mock')
|
||||||
|
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Falsear peticion async
|
||||||
|
* @param {*} fakeData
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const fakeHTTP = async (fakeData = []) => {
|
||||||
|
await delay(5)
|
||||||
|
const data = fakeData.map((u, i) => ({ body: `${i + 1} ${u}` }))
|
||||||
|
return Promise.resolve(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
let STATE_APP = {}
|
||||||
|
|
||||||
|
test(`[Caso - 07] Retornar estado`, async () => {
|
||||||
|
const MOCK_VALUES = ['¿Cual es tu nombre?', '¿Cual es tu edad?', 'Tu datos son:']
|
||||||
|
const provider = createProvider(PROVIDER_DB)
|
||||||
|
const database = new MOCK_DB()
|
||||||
|
|
||||||
|
const flujoPrincipal = addKeyword(['hola'])
|
||||||
|
.addAnswer(
|
||||||
|
MOCK_VALUES[0],
|
||||||
|
{
|
||||||
|
capture: true,
|
||||||
|
},
|
||||||
|
async (ctx, { flowDynamic, fallBack }) => {
|
||||||
|
STATE_APP[ctx.from] = { ...STATE_APP[ctx.from], name: ctx.body }
|
||||||
|
|
||||||
|
flowDynamic('Gracias por tu nombre!')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.addAnswer(
|
||||||
|
MOCK_VALUES[1],
|
||||||
|
{
|
||||||
|
capture: true,
|
||||||
|
},
|
||||||
|
async (ctx, { flowDynamic, endFlow }) => {
|
||||||
|
STATE_APP[ctx.from] = { ...STATE_APP[ctx.from], age: ctx.body }
|
||||||
|
|
||||||
|
await flowDynamic('Gracias por tu edad!')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.addAnswer(MOCK_VALUES[2], null, async (ctx, { flowDynamic }) => {
|
||||||
|
flowDynamic(`Nombre: ${STATE_APP[ctx.from].name} Edad: ${STATE_APP[ctx.from].age}`)
|
||||||
|
})
|
||||||
|
.addAnswer('🤖🤖 Gracias por tu participacion')
|
||||||
|
|
||||||
|
createBot({
|
||||||
|
database,
|
||||||
|
flow: createFlow([flujoPrincipal]),
|
||||||
|
provider,
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(0, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: 'hola',
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(20, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: 'Leifer',
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(40, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: '90',
|
||||||
|
})
|
||||||
|
|
||||||
|
await delay(1200)
|
||||||
|
const getHistory = database.listHistory.map((i) => i.answer)
|
||||||
|
assert.is(MOCK_VALUES[0], getHistory[0])
|
||||||
|
assert.is('Leifer', getHistory[1])
|
||||||
|
assert.is('Gracias por tu nombre!', getHistory[2])
|
||||||
|
assert.is('¿Cual es tu edad?', getHistory[3])
|
||||||
|
assert.is('90', getHistory[4])
|
||||||
|
assert.is('Gracias por tu edad!', getHistory[5])
|
||||||
|
assert.is('Tu datos son:', getHistory[6])
|
||||||
|
assert.is('Nombre: Leifer Edad: 90', getHistory[7])
|
||||||
|
assert.is('🤖🤖 Gracias por tu participacion', getHistory[8])
|
||||||
|
assert.is(undefined, getHistory[9])
|
||||||
|
})
|
||||||
|
|
||||||
|
test.run()
|
||||||
|
|
||||||
|
function delay(ms) {
|
||||||
|
return new Promise((res) => setTimeout(res, ms))
|
||||||
|
}
|
||||||
@@ -175,7 +175,7 @@ class CoreClass {
|
|||||||
|
|
||||||
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
||||||
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
||||||
|
console.log(`🚩🚩🚩`, filterNextFlow?.answer)
|
||||||
return sendFlow(filterNextFlow, from, { prev: undefined })
|
return sendFlow(filterNextFlow, from, { prev: undefined })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,7 @@ const { Client, LocalAuth, MessageMedia, Buttons } = require('whatsapp-web.js')
|
|||||||
const { ProviderClass } = require('@bot-whatsapp/bot')
|
const { ProviderClass } = require('@bot-whatsapp/bot')
|
||||||
const { Console } = require('console')
|
const { Console } = require('console')
|
||||||
const { createWriteStream, readFileSync } = require('fs')
|
const { createWriteStream, readFileSync } = require('fs')
|
||||||
const {
|
const { wwebCleanNumber, wwebGenerateImage, wwebIsValidNumber } = require('./utils')
|
||||||
wwebCleanNumber,
|
|
||||||
wwebGenerateImage,
|
|
||||||
wwebIsValidNumber,
|
|
||||||
} = require('./utils')
|
|
||||||
|
|
||||||
const logger = new Console({
|
const logger = new Console({
|
||||||
stdout: createWriteStream('./log'),
|
stdout: createWriteStream('./log'),
|
||||||
@@ -32,11 +28,7 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
}),
|
}),
|
||||||
puppeteer: {
|
puppeteer: {
|
||||||
headless: true,
|
headless: true,
|
||||||
args: [
|
args: ['--no-sandbox', '--disable-setuid-sandbox', '--unhandled-rejections=strict'],
|
||||||
'--no-sandbox',
|
|
||||||
'--disable-setuid-sandbox',
|
|
||||||
'--unhandled-rejections=strict',
|
|
||||||
],
|
|
||||||
//executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
|
//executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -80,10 +72,7 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
`Necesitas ayuda: https://link.codigoencasa.com/DISCORD`,
|
`Necesitas ayuda: https://link.codigoencasa.com/DISCORD`,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
await wwebGenerateImage(
|
await wwebGenerateImage(qr, `${this.globalVendorArgs.name}.qr.png`)
|
||||||
qr,
|
|
||||||
`${this.globalVendorArgs.name}.qr.png`
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -116,6 +105,9 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
sendButtons = async (number, message, buttons = []) => {
|
sendButtons = async (number, message, buttons = []) => {
|
||||||
|
console.log(`🚩 ¿No te funciona los botones? Intenta instalar`)
|
||||||
|
console.log(`npm i github:pedroslopez/whatsapp-web.js#fix-buttons-list`)
|
||||||
|
|
||||||
const buttonMessage = new Buttons(message, buttons, '', '')
|
const buttonMessage = new Buttons(message, buttons, '', '')
|
||||||
return this.vendor.sendMessage(number, buttonMessage)
|
return this.vendor.sendMessage(number, buttonMessage)
|
||||||
}
|
}
|
||||||
@@ -230,12 +222,9 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
const fileDownloaded = await generalDownload(mediaUrl)
|
const fileDownloaded = await generalDownload(mediaUrl)
|
||||||
const mimeType = mime.lookup(fileDownloaded)
|
const mimeType = mime.lookup(fileDownloaded)
|
||||||
|
|
||||||
if (mimeType.includes('image'))
|
if (mimeType.includes('image')) return this.sendImage(number, fileDownloaded, text)
|
||||||
return this.sendImage(number, fileDownloaded, text)
|
if (mimeType.includes('video')) return this.sendVideo(number, fileDownloaded)
|
||||||
if (mimeType.includes('video'))
|
if (mimeType.includes('audio')) return this.sendAudio(number, fileDownloaded)
|
||||||
return this.sendVideo(number, fileDownloaded)
|
|
||||||
if (mimeType.includes('audio'))
|
|
||||||
return this.sendAudio(number, fileDownloaded)
|
|
||||||
|
|
||||||
return this.sendFile(number, fileDownloaded)
|
return this.sendFile(number, fileDownloaded)
|
||||||
}
|
}
|
||||||
@@ -249,8 +238,7 @@ class WebWhatsappProvider extends ProviderClass {
|
|||||||
*/
|
*/
|
||||||
sendMessage = async (userId, message, { options }) => {
|
sendMessage = async (userId, message, { options }) => {
|
||||||
const number = wwebCleanNumber(userId)
|
const number = wwebCleanNumber(userId)
|
||||||
if (options?.buttons?.length)
|
if (options?.buttons?.length) return this.sendButtons(number, message, options.buttons)
|
||||||
return this.sendButtons(number, message, options.buttons)
|
|
||||||
if (options?.media) return this.sendMedia(number, options.media)
|
if (options?.media) return this.sendMedia(number, options.media)
|
||||||
return this.sendText(number, message)
|
return this.sendText(number, message)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user