botpress - increment vlaue

Viewed 145

I am trying to get a custom action running to simply incrementing a value on passing a specific node on the flow.

My custom actions looks like this:

function action(bp: typeof sdk, event: sdk.IO.IncomingEvent, args: any, { user, temp, session } = event.state) {
  /** Your code starts below */

  let i = undefined

  const p = new Promise((resolve, reject) => {
    if (i === undefined) {
      resolve((i = 0))
    } else if (i >= 0) {
      resolve(i + 1)
    } else {
      reject('i cannot be < 0')
    }
  })

  const runCount = async () => {
    try {
      const counter = await p
      i = counter
      return (session.count = counter)
    } catch (err) {
      console.log(err)
    }
  }

  return runCount()

  /** Your code ends here */
}

When I runCount() variable i will be set to 0. But then, after in rerun runCount() it does not increment further.

What do I need to do to save the variable so it increments on every runCount() call.

Greetings Lorenz

1 Answers

I just managed to solve the problem.

I had to declare i = session.count at the beginning.

Now it gets the value out of the session state and increments the state on every call.

Maybe someone gets some help out of this.

Lorenz

Related