我正在开发Skype机器人应用程序。我在从树模型中读取节点时遇到问题。树模型示例遵循子节点,但我的节点具有差异选项卡供系统读取。
XML 示例-
<?xml version="1.0" encoding="UTF-8" ?>
<rootmenu id="1" title="menu" description="What scenario would you like to run?">
<MenuOption id="1.1" title="Company Data" response="You entered company data" description="What action would you like to perform">
<HierarchyMenuItem id="1.1.1.1" title="Select new data " response="You entered select new filter">
<action>Filter</action>
</HierarchyMenuItem>
<HierarchyMenuItem id="1.1.1.2" title="Navigate node" response="You entered select node">
<action description="Current Filter is ">Hierarchy</action>
<HierarchyLevel level="1" name="One" navigateHierarchy="true">
<action>RootAction</action>
<Option title="Select a Country">
<OptionChoices id="1" title="One1" refNode="ForOne1" />
<OptionChoices id="2" title="One2" refNode="ForOne2" />
<OptionChoices id="3" title="One3" refNode="ForOne3" />
<OptionChoices id="4" title="One4" refNode="ForOne4" />
</Option>
</HierarchyLevel>
</HierarchyMenuItem>
</MenuOption>
<MenuOption id="1.2" title="Adhoc Data">
<Option>
<OptionChoices id="1" title="Ad1" refNode="ForAdOne1" />
<OptionChoices id="2" title="Ad2" refNode="ForAdOne2" />
<OptionChoices id="3" title="Ad3" refNode="ForAdOne3" />
<OptionChoices id="4" title="Ad4" refNode="ForAdOne4" />
</Option>
</MenuOption>
<MenuOption id="1.3" title="(quit)">
</MenuOption>
</rootmenu>
从服务器读取 xml -
function getXMLData(callback) {
var request = require('request');
var DOMParser = require('xmldom').DOMParser;
var simpleconvertxml = require('simpleconvert-xml');
request('http://demo.in/RefactoredXML.xml', function (error, response, body) {
if (!error && response.statusCode == 200) {
var xmlnode = new DOMParser().parseFromString([body].join('n'), 'text/xml');
var myNumber = simpleconvertxml.getXMLAsObj(xmlnode);
treeRoot = tree.parse(myNumber.rootmenu);
callback(treeRoot);
}
})
}
我的第一个机器人调用
bot.dialog('/menu', [
function (session, args) {
getXMLData(function (treeRoot) {
//session.send('node place:'+treeRoot.model.title);
var firstChild = [];
for (var i = 0; i < treeRoot.model.MenuOption.length; i++) {
if(treeRoot.model.MenuOption[i].title !='' && treeRoot.model.MenuOption[i].title != undefined && treeRoot.model.MenuOption[i].title != null) {
firstChild.push(treeRoot.model.MenuOption[i].title);
}
}
if(firstChild.length > 0) {
builder.Prompts.choice(session, treeRoot.model.description,firstChild );
// it shows builder.Prompts.choice(session, "What scenario would you like to run? ", "company data|adhoc data|(quit)");
} else {
session.send('Something went wrong. You can use the back or top command.');
}
});
},
function (session, results) {
if (results.response && results.response.entity != '(quit)') {
session.userData.profile.treeSelectdNodeTitle = results.response.entity;
getXMLData(function (treeRoot) {
for (var i = 0; i < treeRoot.model.MenuOption.length; i++) {
if(treeRoot.model.MenuOption[i].title == session.userData.profile.treeSelectdNodeTitle) {
session.userData.profile.treeSelectdNodeId = treeRoot.model.MenuOption[i].id;
session.userData.profile.treeSelectdResponse = treeRoot.model.MenuOption[i].response;
}
}
session.send('resp ' + session.userData.profile.treeSelectdResponse);
if(treeRoot.hasChildren()) {
session.send('in the children');
session.beginDialog('/get Tree Node');
} else {
session.send('in the title');
session.beginDialog('/'+ treeRoot.model.title);
}
});
} else {
// Exit the menu
session.endDialog();
}
},
function (session, results) {
// The menu runs a loop until the user chooses to (quit).
session.replaceDialog('/menu');
}
]).reloadAction('reloadMenu', null, {matches: /^menu|show menu|top|top menu/i});
菜单对话框调用应显示 1.公司数据 2. 临时数据 3.(退出)但它只显示 1.公司数据 2.临时数据和菜单中的问题,如果用户选择 1 选项作为公司数据,它将不会进入 treeRoot.hasChildren() 条件。
bot.dialog('/get Tree Node', [
function(session,agrs) {
getTreeNode(session);
}
]);
function getTreeNode(session) {
session.send("in tree "+ session.userData.profile.treeSelectdNodeId);
getXMLData(function(treeRoot) {
var nextLevel = treeRoot.first(function (node) {
return node.model.id === session.userData.profile.treeSelectdNodeId;
});
session.send('selected title '+ nextLevel.model.HierarchyMenuItem[0].title);
/* var secondListChild = [];
for(var i = 0; i < nextLevel.model.HierarchyMenuItem.length; i++) {
if(nextLevel.model.HierarchyMenuItem[i].title !='' && nextLevel.model.HierarchyMenuItem[i].title != undefined && nextLevel.model.HierarchyMenuItem[i].title != null) {
secondListChild.push(nextLevel.model.HierarchyMenuItem[i].title);
}
}
if(secondListChild.length > 0) {
builder.Prompts.choice(session, nextLevel.model.description,secondListChild);
} else {
session.send('Something went wrong. You can use the back or top command.');
} */
});
}
getTreeNode的问题是session.send('selected title '+ nextLevel.model.HierarchyMenuItem[0].title); ^类型错误: 无法读取未定义的属性"模型"
有解决方案....
我已经更改了XML解析NPM及其对我的工作。
var parse = require('xml-parser');
var inspect = require('util').inspect;
function getXMLData(callback) {
var request = require('request');
request('http://ztdemo.headfitted.in/RefactoredXML.xml', function (error, response, body) {
if (!error && response.statusCode == 200) {
var obj = parse(body);
//console.log(inspect(obj, { colors: true, depth: Infinity }));
//callback(treeRoot);
treeRoot = tree.parse(obj.root);
callback(treeRoot);
}
})
}