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