I see that there is a + function but I can only add two numeric attributes together. Is there a way to add 10 numeric attributes?
Do you want to add up exactly 10 numbers or sometimes you want to add up to N numbers?
Actually the function “+” allows the add-up of any number of numbers, but our current GUI just doesn’t allow users to add more than 2 now. I’m filing an issue to enable this in the near future.
In the meantime, you can use one of two ways to add up 10 numbers.
I. In a message, where you want to add up 10 numbers, just use the following expression (make sure you include the single back quotes to quote the function:
(+ 1 2 3 4 5 6 7 8 9 10)
II. Define your own add function that allows 10 variables. The benefit of this approach is that you can reuse this function. You can also easily insert attributes you want to use as arguments inside the function.
Go to “Juji IDE” by clicking on the red icon, see below.
Go to the chatbot you want to allow this. Click on the custom file, see below. Copy paste the function below into the custom file. MAKE SURE YOU SAVE IT by clicking on the “Save” button.
(defn my-add [x1 x2 x3 x4 x5 x6 x7 x8 x9 x10] (+ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10))
Now, go back to your chatbot where you want to use this function, click on “Add a function icon”
Select “Add External Function”
In the pop-up, fill in the info as below, and add all the 10 arguments, if you have attributes, you can use attributes as your arguments too. Make sure you choose the “Numeric” as the argument type. In this testing, it adds the 10 numbers from 1 to 10.
- Test this function.
@mzhou That’s perfect! Thank you so much!
@mzhou How will I be able to store the sum into an attribute so I can use it later for conditions?
You’re most welcome! My colleague just told me another way to do this if you want to have a more generic function. Just copy & paste the following function in the custom file instead of the function I mentioned above with 10 arguments. This function will allow you to add up ANY number of arguments.
(defn my-add [& ns] (apply + ns))
@mzhou I will definitely try that. Thanks again! Also, is there a way to store the sum into an attribute?
Yes, you can store anything including a function results into an attribute.
Select a topic (card) where you want to do the computation and store the value into an attribute.
On the right panel, scroll down to find the section called “Customize Chatbot Actions”
Decide whether you want to compute and store the attribute BEFORE or AFTER your chatbot sends a message or asks a question. If you want to do it BEFORE, select the block “Pre-Action”, otherwise “Post-Action”
Inside such a block, click on the green “+” button to select “Store an attribute”
Inside the pop-up window, you can define the attribute value using your “add” function. Note here, do NOT add the single back quotes
Now you can use this stored attribute somewhere else in your chat flow.