how to access Botpress variables in Execute code n...
# šŸ¤help
c
I could really use some help understanding how we can access variables declared in the Botpress main workspace in the Execute code node. For debug purposes I have made a simple node setup where I: 1. created a variable called "test" , number, default value of 10 2. written the following code in the Execute code: > // Access the value of "test" from flow variables > const initialValue = bp.flow.getVariable('test'); > > // Add 10 to the initial value > const result = initialValue + 10; > > // Set the result as a flow variable > bp.flow.setVariable('result', result); > > // The event handler will automatically return the result to the Botpress flow. The error Im getting now is the following: [TypeError, Cannot read properties of undefined (reading 'getVariable')] My guess is that the variable were trying to "get" (test) is not a flow variable and therefore doesn't register in the code. But if so, how can we actually make flow variables ?

https://cdn.discordapp.com/attachments/1134911192314425424/1134911192507351090/brave_2023-07-29_20-03-42.jpgā–¾

https://cdn.discordapp.com/attachments/1134911192314425424/1134911192826134558/brave_2023-07-29_20-03-25.jpgā–¾

n
Bro just click any where in your workflow except nodes then in right inspector window you can create variable of desired type and it can be accessed in code using the following syntax {workflow.variablename} change the variable name with yours
c
@nice-gold-26629 Thank you for thinking along! I will write a simple example to see if I understand it correctly cause Im still unsuccesful despite many hours of debugging. 1. I have declared test1 and test2 inside the main workflow of Botpress 2. I write the code below. The errors are " EMPTY > not set " and [SyntaxError, Unexpected token '.']

https://cdn.discordapp.com/attachments/1134911192314425424/1135236969274810378/Code_2023-07-30_17-41-46.jpgā–¾

https://cdn.discordapp.com/attachments/1134911192314425424/1135236969509699736/brave_2023-07-30_17-42-49.jpgā–¾

https://cdn.discordapp.com/attachments/1134911192314425424/1135236969799110797/brave_2023-07-30_17-45-36.jpgā–¾

In the Documentation it says we do not need the brackets. Still an error: "[ReferenceError, test1 is not defined]" const test1 = workflow.test1; const test2 = workflow.test2; const Combined = test1 + test2; bp.kvs.set('Result', Combined);
@careful-keyboard-68830 sorry to bother you but could you take a look ?
c
I got this to work. You need to define a new workflow variable In the GUI and then save to that variable (workflow.combined = result) in the code. I tried session variables but that is apparently not working or the docs are wrong.
I have no idea where bp.kvs.set is from? Hallucinated from AI? Here is what I used. const result = Number(workflow.test1) + Number(workflow.test2) workflow.testDone = result
c
@careful-solstice-2146 my man! finally a concrete result after hours of debugging. The documentation is not very clear on this and yes ChatGPT gave me 10 different propositions which did not work in the slightest.
c
I feel like the docs aren’t keeping up with the pace of the project development. Anyway glad it’s working for you, this is basic functionality that should be straightforward.
c
yes 100%. Hopefully the community can find the loopholes in the current document so future users won't have to go trough the same troubles.
@careful-solstice-2146 just for interest. if the test1/test2 variables where already set as numbers do we still need the "number" prefix in Nuimber(workf.test) ?
c
I don’t think it should be necessary to recast them as numbers unless it’s possible they were input as strings, so yeah probably works without that.
c
ah just checked and it seems we DO need to recast them as numbers despite them being initialized as numbers in the GUI
else it prints it as a string 10 + 5 = 105, instead of 10 + 5 = 15
c
I think it’s a JavaScript thing. JavaScript is an untyped language…. I don’t actually know what the GUI types do. I guess perform input validation on capture?
c
Apologies was sleeping, glad you found a solution šŸ™‚
70 Views