Skip to content

Commit

Permalink
feat(bot): ✨ .addAction feat
Browse files Browse the repository at this point in the history
feat(bot): ✨ `.addAction` feat
  • Loading branch information
leifermendez committed Mar 4, 2023
2 parents d1bff8b + 37b3aa4 commit d94459a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CoreClass {
flowClass
databaseClass
providerClass
generalArgs = { blackList: [], listEvents: {} }
generalArgs = { blackList: [], listEvents: {}, delay: 0 }
constructor(_flow, _database, _provider, _args) {
this.flowClass = _flow
this.databaseClass = _database
Expand Down Expand Up @@ -77,7 +77,6 @@ class CoreClass {
let fallBackFlag = false
if (this.generalArgs.blackList.includes(from)) return
if (!body) return
if (!body.length) return

let prevMsg = await this.databaseClass.getPrevByNumber(from)
const refToContinue = this.flowClass.findBySerialize(prevMsg?.refSerialize)
Expand Down Expand Up @@ -131,7 +130,7 @@ class CoreClass {
const queue = []
for (const ctxMessage of messageToSend) {
if (endFlowFlag) return
const delayMs = ctxMessage?.options?.delay || 0
const delayMs = ctxMessage?.options?.delay ?? this.generalArgs.delay ?? 0
if (delayMs) await delay(delayMs)
await QueuePrincipal.enqueue(() =>
this.sendProviderAndSave(numberOrId, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage))
Expand Down Expand Up @@ -302,8 +301,12 @@ class CoreClass {
*/
sendProviderAndSave = async (numberOrId, ctxMessage) => {
const { answer } = ctxMessage
await this.providerClass.sendMessage(numberOrId, answer, ctxMessage)
await this.databaseClass.save({ ...ctxMessage, from: numberOrId })

if (answer && answer.length && answer !== '__call_action__') {
await this.providerClass.sendMessage(numberOrId, answer, ctxMessage)
await this.databaseClass.save({ ...ctxMessage, from: numberOrId })
}

return
}

Expand Down Expand Up @@ -332,7 +335,7 @@ class CoreClass {
sendFlowSimple = async (messageToSend, numberOrId) => {
const queue = []
for (const ctxMessage of messageToSend) {
const delayMs = ctxMessage?.options?.delay || 0
const delayMs = ctxMessage?.options?.delay ?? this.generalArgs.delay ?? 0
if (delayMs) await delay(delayMs)
QueuePrincipal.enqueue(() => this.sendProviderAndSave(numberOrId, ctxMessage))
}
Expand Down
3 changes: 3 additions & 0 deletions packages/bot/io/methods/addAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const addAction = () => async () => {}

module.exports = { addAction }
1 change: 1 addition & 0 deletions packages/bot/io/methods/addAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const addAnswer =
ctx,
ref: ctx.ref,
addAnswer: addAnswer(ctx),
addAction: (cb = () => null) => addAnswer(ctx)('__call_action__', null, cb),
toJson: toJson(ctx),
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/bot/io/methods/addKeyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const addKeyword = (keyword, options) => {
ctx,
ref: ctx.ref,
addAnswer: addAnswer(ctx),
addAction: (cb = () => null) => addAnswer(ctx)('__call_action__', null, cb),
toJson: toJson(ctx),
}
}
Expand Down

0 comments on commit d94459a

Please sign in to comment.