javascript

A place to post all your mods and snippets for Program O.

javascript

Postby knavatar » March 24th, 2015, 11:43 pm

Does ver 2 of Program O support embedding javascript? If so, do you have an example? Right now I'm primarily interested in incorporating URLs in my bot's responses.
knavatar
Casual Member
 
Posts: 15
Joined: March 4th, 2015, 12:50 am

Re: javascript

Postby GeekCaveCreations » March 25th, 2015, 12:41 am

You can embed JavaScript in your AIML with one condition to be mindful of. Any JavaScript that deals with URLs (specifically, the ampersand (&) must be contained within an XML CDATA tag in order for it to be considered valid XML (which is required in order to upload AIML files). If you're not going to upload an AIML file that contains such JavaScript (e.g. using the "Teach" interface to add the AIML to your bot), then you don't need to worry about this restriction.

That said, however, I would strongly suggest putting the bulk of the JavaScript code into the bot's web page, and just use function calls to do most of the heavy lifting. This will make for smaller response strings, which will net the best performance overall.

An example of AIML with embedded JavaScript follows:

Code: Select all
<category>
  <pattern>SCRIPTEXAMPLE</pattern>
  <template>
    This is a script test...
    <script type="text/javascript">
      //<![CDATA[
      var URL = 'http://www.example.com?arg1=foo&arg2=bar';
      var myWindow window.open(url, myWindow);
      // ]]>
    </script>
  </template>
</category>


Now this is just a simple example, but it illustrates how to go about making sure that the AIML file that contains this category is still valid XML. If you're not going to use URLs in the response, you can actually simplify things greatly.

I hope this helps. :)
Comforting the disturbed, and disturbing the comfortable
Chat with Morti
User avatar
GeekCaveCreations
Safe, Reliable Insanity, Since 1961
 
Posts: 1115
Joined: April 18th, 2011, 10:52 pm
Location: Nevada, USA

Re: javascript

Postby Alekz » March 25th, 2015, 3:21 am

GeekCaveCreations wrote: Any JavaScript that deals with URLs (specifically, the ampersand (&) must be contained within an XML CDATA tag in order for it to be considered valid XML (which is required in order to upload AIML files).

Ah... That's why it did not work! ;)
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: javascript

Postby knavatar » March 25th, 2015, 3:14 pm

Sorry but I am having some difficulty. Are arg1 and arg2 required? Also would this script go directly to the URL? An appropriate response would be to provide a link and give the user the option to click to leave the current page.
knavatar
Casual Member
 
Posts: 15
Joined: March 4th, 2015, 12:50 am

Re: javascript

Postby GeekCaveCreations » March 25th, 2015, 3:56 pm

That bit of code was just an example, and only served to illustrate what is required when embedding JavaScript code in an AIML category. Another example could look like this:

Code: Select all
<category>
  <pattern>SCRIPTEXAMPLE</pattern>
  <template>
    This is a script test...
    <script type="text/javascript">
      //<![CDATA[
     alert('I like ham & eggs');
      // ]]>
    </script>
  </template>
</category>


It's the ampersand symbol, and how to handle it in XML/AIML that's important here, not the example code itself.
Comforting the disturbed, and disturbing the comfortable
Chat with Morti
User avatar
GeekCaveCreations
Safe, Reliable Insanity, Since 1961
 
Posts: 1115
Joined: April 18th, 2011, 10:52 pm
Location: Nevada, USA

Re: javascript

Postby knavatar » March 29th, 2015, 5:26 pm

This is exactly what I have:

Code: Select all
<aiml version="1.0">

<category>
 <pattern>LOCATE KNOWLEDGE AVATARS</pattern>
 <template>
    Here we go…
    <script type="text/javascript">
      //<![CDATA[
      var hotText = ‘Home’;
      var URL = 'https://knowledgeavatars.com/';
      document.write('Click to go to ' + hotText.link(URL));
      // ]]>
    </script>
  </template>
</category>
</aiml>


It returns "Here we go..." and nothing else. Am I missing something?
knavatar
Casual Member
 
Posts: 15
Joined: March 4th, 2015, 12:50 am

Re: javascript

Postby knavatar » March 29th, 2015, 6:32 pm

The Javascript runs fine in the browser. The issue seems to be in the XML. I get these error message when I try to validate the code.

Error 515: Element script is not declared in template list of possible children on line 26, column 0
<template>
Error 534: No declaration for element script on line 28, column 0
<script type="text/javascript">
Error 533: No declaration for attribute type of element script on line 28, column 0
<script type="text/javascript">
knavatar
Casual Member
 
Posts: 15
Joined: March 4th, 2015, 12:50 am

Re: javascript

Postby GeekCaveCreations » March 29th, 2015, 6:53 pm

I'll have to look at the DTD (Document Type Declaration) file to verify, but it seems that the <script> tag isn't be included as a valid tag. If that's the case, then I'll have to amend the file to include it. The original DTD file was written by the nice folks at Pandorabots, and since they don't allow JavaScript to run in their system, it stands to reason that they would probably not put in support for the tag, It's a simple fox, though.
Comforting the disturbed, and disturbing the comfortable
Chat with Morti
User avatar
GeekCaveCreations
Safe, Reliable Insanity, Since 1961
 
Posts: 1115
Joined: April 18th, 2011, 10:52 pm
Location: Nevada, USA


Return to Mods and Snippets

Who is online

Users browsing this forum: No registered users and 0 guests

cron