How to increment a variable

Hello,

I created my first bot a few month ago and I want to count how many times a flow is reached. I created a variable but I cannot find the syntax to increment it in the specific node. (see what I did below)

I hope I am at the right place, please tell me if I can improve my post :slight_smile:
Thank you for your help !

Use a custom action and call it with below code. Here it will persist the count to kvs db

  const count = await bp.kvs.get(botId, userId + '_counter')
  if (count === undefined) {
    count = 0;
  } else {
    count++;
  }
  await bp.kvs.set(botId, userId + '_counter', count)

For more Botpress related guides please follow this blog

1 Like

Thank you for your quick reply, I will try this solution. But can’t we just increment it in the “Edit action” box ? Is there any way to call a variable in this window ?

Hello @abhisheksimion ,

I tried to create a custom action which call the code you gave me. But I do not know how to use it, even with your tutorial on custom component.
How to access in a flow the variable you increment in the action ? I called the getGlobalVariable buitin action while renaming the variable without the userId, but it seems not working.

Here the first node call my custom action. The second one use getGlobalVariable to put the data from the action to a flow variable. Then it has to say two different things depending on the value of my counter. When I try it it goes into my node but answer that he do not understand…
Do you have a clue ? :slight_smile:

Thank you very much for your help !

@abhisheksimion ? Any idea ? :slight_smile:

Hi @Robert, when you updated the counter, simply assign it to the temporary memory, ex:

  ... 
  await bp.kvs.set(botId, userId + '_counter', count)
  temp.counter = count

then you can use in your transition temp.counter === 0

Please refer to the memory documentation for more details on that: https://botpress.io/docs/main/memory#variables