mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-18 03:29:15 +00:00
work flow
This commit is contained in:
@@ -47,16 +47,30 @@ class CoreClass {
|
||||
* @param {*} ctxMessage
|
||||
*/
|
||||
handleMsg = ({ body, to, from }) => {
|
||||
this.databaseClass.save(body)
|
||||
const messageToSend = this.flowClass.find(body) || []
|
||||
console.log(messageToSend)
|
||||
if (Array.isArray(messageToSend)) this.sendFlow(messageToSend, from)
|
||||
let msgToSend = []
|
||||
console.log('____', this.databaseClass.listHistory)
|
||||
const prevMsg = [...this.databaseClass.listHistory].pop()
|
||||
|
||||
if (prevMsg?.ref && prevMsg?.options?.capture) {
|
||||
msgToSend = this.flowClass.find(prevMsg.ref, true) || []
|
||||
} else {
|
||||
msgToSend = this.flowClass.find(body) || []
|
||||
}
|
||||
if (Array.isArray(msgToSend)) this.sendFlow(msgToSend, from)
|
||||
}
|
||||
|
||||
sendProviderAndSave = (numberOrId, ctxMessage) => {
|
||||
const { answer } = ctxMessage
|
||||
return Promise.all([
|
||||
this.providerClass.sendMessage(numberOrId, answer),
|
||||
this.databaseClass.save(ctxMessage),
|
||||
])
|
||||
}
|
||||
|
||||
sendFlow = (messageToSend, numberOrId) => {
|
||||
const queue = []
|
||||
for (const message of messageToSend) {
|
||||
queue.push(this.providerClass.sendMessage(numberOrId, message))
|
||||
for (const ctxMessage of messageToSend) {
|
||||
queue.push(this.sendProviderAndSave(numberOrId, ctxMessage))
|
||||
}
|
||||
return Promise.all(queue)
|
||||
}
|
||||
|
||||
@@ -5,20 +5,20 @@ class FlowClass {
|
||||
}
|
||||
|
||||
find = (keyOrWord, symbol = false) => {
|
||||
let capture = false
|
||||
let messages = []
|
||||
const findIn = (keyOrWord, symbol = false, flow = this.flow) => {
|
||||
if (symbol) {
|
||||
const refSymbol = flow.find((c) => c.keyword === keyOrWord)
|
||||
if (refSymbol && refSymbol.answer)
|
||||
messages.push(refSymbol.answer)
|
||||
if (refSymbol && refSymbol.ref) findIn(refSymbol.ref, true)
|
||||
} else {
|
||||
const refSymbolByKeyworkd = flow.find((c) =>
|
||||
c.keyword.includes(keyOrWord)
|
||||
)
|
||||
if (refSymbolByKeyworkd && refSymbolByKeyworkd.ref)
|
||||
findIn(refSymbolByKeyworkd.ref, true)
|
||||
let refSymbol
|
||||
|
||||
const findIn = (keyOrWord, symbol = false, flow = this.flow) => {
|
||||
capture = refSymbol?.options?.capture || false
|
||||
if (capture) return messages
|
||||
if (symbol) {
|
||||
refSymbol = flow.find((c) => c.keyword === keyOrWord)
|
||||
if (refSymbol?.answer) messages.push(refSymbol)
|
||||
if (refSymbol?.ref) findIn(refSymbol.ref, true)
|
||||
} else {
|
||||
refSymbol = flow.find((c) => c.keyword.includes(keyOrWord))
|
||||
if (refSymbol?.ref) findIn(refSymbol.ref, true)
|
||||
return messages
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user