可选择从使用 Botframework 构建的 NodeJS 机器人调用 LUIS
Optionally calling LUIS from a NodeJS bot built with Botframework
我正在尝试构建一个具有一组已知 'commands' 的机器人,如果它们完全匹配,将启动一个特定的对话框。如果无法识别命令,则会将其传递给 LUIS 进行自然语言处理。
我试过这种方法,似乎部分有效,但在启动提示时,未能激活第二个瀑布步骤。
import builder = require('botbuilder');
let restify = require('restify');
let luisUrl = "MY_LUIS_URL_HERE";
let server = restify.createServer();
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
let bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
let recognizer = new builder.LuisRecognizer(luisUrl);
let intentsDialog = new builder.IntentDialog({ recognizers: [recognizer] });
let mainLoopDlg = [
(session, results) => {
let input = session.message.text.trim();
if (input === "#mycmd") {
// Is a command
session.beginDialog('/mycmd');
} else {
// Else assume is a natural language utterance
// Have tried all of the following
// Need a way to open a dialog and forward on the original message ...
intentsDialog.replyReceived(session);
//session.beginDialog('/intents');
//session.beginDialog('/intents', { message: session.message });
//session.beginDialog('/intents', session.dialogData.args);
}
}
];
let commandDlg = [
(session, results) => {
session.send("Command launched ...");
}
];
let intentDlg = [
(session) => {
builder.Prompts.confirm(session, "hi");
},
(session, result) => {
// THIS IS NEVER REACHED ...
session.send("Hi" + result.response);
}
];
bot.dialog('/', mainLoopDlg);
bot.dialog('/mycmd', commandDlg);
bot.dialog('/intents', intentDialog);
intentsDialog.matches('Greeting', intentDlg);
// Grab some secret data from the deploy environment
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log("%s listening to %s", server.name, server.url);
});
好的,看来我需要使用 session.replaceDialog('/intents');
代替 intentsDialog.replyReceived(session);
这似乎解决了眼前的问题,我不确定这是否无懈可击。 This issue on the BotFramework Github(我提出的)对框架的 V1.0 做了类似的事情,似乎可能需要一些状态管理来控制事情。目前,尽管上述修复似乎有效。
我正在尝试构建一个具有一组已知 'commands' 的机器人,如果它们完全匹配,将启动一个特定的对话框。如果无法识别命令,则会将其传递给 LUIS 进行自然语言处理。
我试过这种方法,似乎部分有效,但在启动提示时,未能激活第二个瀑布步骤。
import builder = require('botbuilder');
let restify = require('restify');
let luisUrl = "MY_LUIS_URL_HERE";
let server = restify.createServer();
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
let bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
let recognizer = new builder.LuisRecognizer(luisUrl);
let intentsDialog = new builder.IntentDialog({ recognizers: [recognizer] });
let mainLoopDlg = [
(session, results) => {
let input = session.message.text.trim();
if (input === "#mycmd") {
// Is a command
session.beginDialog('/mycmd');
} else {
// Else assume is a natural language utterance
// Have tried all of the following
// Need a way to open a dialog and forward on the original message ...
intentsDialog.replyReceived(session);
//session.beginDialog('/intents');
//session.beginDialog('/intents', { message: session.message });
//session.beginDialog('/intents', session.dialogData.args);
}
}
];
let commandDlg = [
(session, results) => {
session.send("Command launched ...");
}
];
let intentDlg = [
(session) => {
builder.Prompts.confirm(session, "hi");
},
(session, result) => {
// THIS IS NEVER REACHED ...
session.send("Hi" + result.response);
}
];
bot.dialog('/', mainLoopDlg);
bot.dialog('/mycmd', commandDlg);
bot.dialog('/intents', intentDialog);
intentsDialog.matches('Greeting', intentDlg);
// Grab some secret data from the deploy environment
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log("%s listening to %s", server.name, server.url);
});
好的,看来我需要使用 session.replaceDialog('/intents');
代替 intentsDialog.replyReceived(session);
这似乎解决了眼前的问题,我不确定这是否无懈可击。 This issue on the BotFramework Github(我提出的)对框架的 V1.0 做了类似的事情,似乎可能需要一些状态管理来控制事情。目前,尽管上述修复似乎有效。