on 05-29-2021 5:46 PM
Hello, once again i need help with scripting in CAI...
i have an array of objects (a list of values, organized by categories)
facts = [
{ cat:0, id:0, value:"blabla", seen:0}
, {cat:0, id:1, value:"xyz", seen:0}
, {cat:0, id:2, value:"this and that", seen:0}
, {cat:1, id:0, value:"whatever", seen:0}
, ... ]
i also have a counter that specifies which item in this array i'm currently at (=which one is supposed to be used/checked next) and a variable that defines what the current category of interest is
facts_counter = 0
facts_cat = 1
two questions:
A. how do i loop through the facts-array, starting at the item defined by facts_counter, to show the value of the next object that matches the current category defined in facts_cat?
B. and how do i set the value of "seen" to 1, for this specific item just found?
all hints&help highly appreciated!
thx+best regards
matthias
This seems like the kind of thing that you should just create a webhook -- which is designed to do this sort of calculation and can easily return data to the memory. The scripting is really designed to read data, select pieces of it, and display it -- as opposed to updating data.
Given all that, you could do something like this:
"{{#each memory.facts}}{{#gte @index ../memory.facts_counter}}
{{#eq ../memory.facts_cat this.cat}}{{@index}};{{/eq}}{{/gte}}{{/each}}"
{{{{first (split memory.answer ";")}}
{{#with (itemAt memory.facts memory.answer2)}} {{value}} {{/with}}
[{{#eachJoin memory.facts}}{{#eq @index ../memory.answer2}}
... here you would recreate this object manually updating seen property{{else}}{{this}}{{/eq}}{{/eachJoin}}]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just because it was fun and I wanted to show it is easy:
The Kyma function was easy to create and update (debugging was not so good but maybe I don;t know how to set it up). Here's the code of the function:
module.exports = {
main: function (event, context) {
var memory = event.data["conversation"]["memory"]
var facts_counter = memory["facts_counter"]
var facts_cat = memory["facts_cat"];
for (i = facts_counter; i < memory["facts"].length; i++) {
if (memory["facts"][i]["cat"] == facts_cat) {
memory["facts"][i]["seen"] = 1;
break;
}
}
var reply = {replies : [{type : "text", content : "No match"}]};
if (i < memory["facts"].length) {
reply["replies"][0]["content"] = memory["facts"][i]["value"];
reply["conversation"] = { "memory" : memory };
}
return JSON.stringify(reply);
}
}
I'm sure I could have coded it better 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure, BTP would be our choice, but so far we haven't found anyone who'd give us access to a non-trial version. (we are a no-budget, no-time, no-knowledge project 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, thank you Daniel, in the meantime i implemented the whole thing slightly differently, also including copying over the whole structure as i simply couldn't figure out how assigning values inside a script would be done. But, as always, i learned something from your response that i didn't know before (hadn't seen the ../ syntax before - that's probably the reason while my attempts failed. a variable that outside the loop had a value, didn't exist inside the loop..)
And yes, certainly: If we did have access to a Platform where we could develop logic to be used by CAI as a Webhook or API-call, that would simplify thing enormously. Unfortunately we don't have such a Platform, so for the time being we have to do everything inside CAI.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.