Title: WLA: Random answers loaded from an XML file
Slug: wla-random-answers-loaded-from-an-xml-file
Date: 2007-09-20 12:54:00
Author: Kartones
Lang: en
Tags: Development, XML, Windows Live Agents
Description: A guide on loading random answers from an XML file to enhance a Windows Live Agents robot's responses.

<p> One of the nice features of developing a messenger robot/agent is that you can easily load data from XML files. Combining this with few lines of code, you can make your agent more rich by giving a different response each time. Here is an example:</p> <p align="justify"> </p> <p align="justify">We'll use this XML file to store answers for specific topics:</p> <p><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">?xml</font><font color="red"> version</font><font color="blue">="1.0"</font><font color="red"> encoding</font><font color="blue">="ISO-8859-1"</font><font color="red"> ?</font><font color="blue">&gt;</font></font><font color="black"><br></font><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">answers</font><font color="blue">&gt;</font></font><font color="black"><br></font><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">topicGeek</font><font color="blue">&gt;</font></font><font color="black"><br><font face="Courier New">        </font></font><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">text</font><font color="blue">&gt;</font><font color="black">Yeah, of course I am!</font><font color="blue">&lt;/</font><font color="maroon">text</font><font color="blue">&gt;</font></font><font color="black"><br><font face="Courier New">        </font></font><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">text</font><font color="blue">&gt;</font><font color="black">Indeed, my friend</font><font color="blue">&lt;/</font><font color="maroon">text</font><font color="blue">&gt;</font></font><font color="black"><br><font face="Courier New">        </font></font><font face="Courier New"><font color="blue">&lt;</font><font color="maroon">text</font><font color="blue">&gt;</font><font color="black">You are a geek too!</font><font color="blue">&lt;/</font><font color="maroon">text</font><font color="blue">&gt;</font></font><font color="black"><br></font><font face="Courier New"><font color="blue">&lt;/</font><font color="maroon">topicGeek</font><font color="blue">&gt;</font></font><font color="black"><br></font><font face="Courier New"><font color="blue">&lt;/</font><font color="maroon">answers</font><font color="blue">&gt;</font><font color="black"></font></font></p> <p>  </p><p>The first step is to define how to load our desired topic answers into a datasource: </p><p><font face="Courier New">datasource AnswersXMLFileLoader() =&gt; text<br>  file<br>    answers.xml<br>  simple xml<br>    answers<br>      topicGeek<br>        text {loop=content}</font>  </p><p align="justify"> </p> <p align="justify">Then, we create a datatable to load those answers, and keep the desired fiels (in this example, just the <b>text</b> field)</p> <p><font face="Courier New">datatable AnswersGeekTopicTable {expire=never}<br>  load text {index=exact} from datasource<br>    AnswersXMLFileLoader()</font>  </p><p align="justify"> </p> <p align="justify">With this simple code, we count the number of elements of a datatable, randomly choose one, find it and return its <b>text</b> field:</p> <p><font face="Courier New">function RandText(TABLE)<br>  COUNT = get count in TABLE<br>  ITEM = Rand(COUNT)<br>  VAR = 0<br>  TEXT = get text in TABLE<br>    if VAR == ITEM<br>      return TEXT<br>    VAR = VAR + 1<br>  return TEXT</font>  </p><p align="justify"> </p> <p align="justify">Now we setup the procedure that will call our RandText() function and output to the user the answer:</p> <p><font face="Courier New">procedure RandomAnswerGeekTopic()<br>  ANSWER = RandText(AnswersGeekTopicTable)<br>  - ANSWER</font>  </p><p>  </p><p>And finally, we create the pattern which will be answered (notice that "<i>You are a</i>" it's optional): </p><p><font face="Courier New">+ [You are a] geek<br>call RandomAnswerGeekTopic()</font>  </p><p> </p> <p>And that's all!</p>
