Did you know Bot Libre also provides free hosted forums for your own website or mobile app?
Self, AIML, and scripting

Why does this not work

by dewhite57 posted May 11 2022, 9:17

My script seems ok, I can do this in Python, it doesn't throw errors but the bot never responds to a question like What is CVE-2022-29315.  I don't really know much about self and have been working from examples.  Maybe it is the JSON parsing?  I don't really know how that works in self.  I put the script in the bot and moved it to the top of the list.  I even tried it with just a return of "Duh" and it still didn't respond to the question (even without all the json stuff).  So, maybe I just don't know how to get this in the bot's brain?  Any examples would be helpful.  Thanks

state getCVE {
    pattern "(what) (was is) [CVE CVE-ID ID] *?" template foo();
    function foo() {
        temp = http.requestJSON("https://cve.circl.lu/api/CVE-" + star );
       //severityIs = temp.result.CVE_Items[0].impact.baseMetricV3.cvssV3.baseSeverity
       descIs = temp.result.CVE_Items[0].cve.description.description_data[0].value
       if (temp == null) {
           return "I don't think so";
        }
       return (descIs);
    }
}


by admin posted May 12 2022, 9:28
Looking at your bot I see several issues.

First you have learning enabled, this is not recommend and will make the bot learn EVERY reply you give it as a new response.

For your scripts, the best way to debug a script is to click on the "debug" checkbox in chat and set the log level to FINE. This would give you,

WARNING -- Language:Missing function: requestJSON on: <509 #null *,a:1,c:0,p>
This means the class or method you are using is wrong. This is because you are using http not Http.

Fixing this gives you the error,

WARNING -- Http:Illegal character in path at index 56: https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-3033 - 39315
This is because space characters are not allowed in URLs, you need to encode URL parameters.

Also traversing a deep JSON object can be tricky, I would debug it by traversing 1 level at a time and printing the result. If you traverse nested array elements you need to use "elements" in Self.

Try something like:

state getCVE {
    pattern "(what) (was is) [CVE CVE-ID ID] *" template foo();
    function foo() {
        temp = Http.requestJSON("https://cve.circl.lu/api/CVE-" + Http.encode(star.toString().replace(" ", "")) );
       //severityIs = temp.result.CVE_Items[0].impact.baseMetricV3.cvssV3.baseSeverity
       descIs = temp.result.CVE_Items.elements.[0].cve.description.description_data.elements.[0].value
       if (temp == null) {
           return "I don't think so";
        }
       return (descIs);
    }
}

Updated: May 12 2022, 9:30
Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 187, today: 0, week: 1, month: 0

by dewhite57 posted May 12 2022, 11:14

Thanks that debug mode is very helpful.   The JSON object "elements" command throws an error that says Invalid attribute name: [ which seems to be around the elements.[0].  Is there a doc somewhere that explains how json parsing in self works?  I can parse other things out of the json like in python but when I get to the nested elements, it throws an error.


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 186, today: 0, week: 2, month: 0

by dewhite57 posted May 12 2022, 11:27

I figured it out the .elements.[0] does not work.  But, if you use this syntax:

conversation.topic.description = resp.result.CVE_Items.get(#element, 0).cve.description.description_data.get(#element, 0).value;

it returns the nested string description.  So, the whole script which retrieves a description of a CVE inquiry is:

state cveLookUp {
pattern "(what) (was is) (CVE) *" template getCVE();

function getCVE() {
resp = Http.requestJSON("https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-" + Http.encode(star.toString().replace(" ", "")));
conversation.topic = star.toString();
conversation.topic.description = resp.result.CVE_Items.get(#element, 0).cve.description.description_data.get(#element, 0).value;

if (conversation.topic.description == null){
output = "CVE " + converstion.topic + " does not exist";
} else {
output = "CVE-" + conversation.topic + " is dedscribed as: " + conversation.topic.description;
}
return output;
}
}

Thanks


Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 180, today: 0, week: 2, month: 0

by admin posted May 13 2022, 14:26

Glad to hear you got it working.

For using element use,
resp.result.CVE_Items.element[0].cve.description.description_data.element[0].value
For Self see,
https://www.botlibre.com/forum-post?id=13020078

and, https://www.botlibre.com/forum-post?id=13020078
Updated: May 13 2022, 14:27
Thumbs up: 0, thumbs down: 0, stars: 0.0
Views: 181, today: 0, week: 1, month: 0


Id: 43012861
Posted: May 11 2022, 9:17
Updated: May 12 2022, 9:21
Replies: 4
Views: 212, today: 1, week: 2, month: 1
0 0 0.0/5