我正在尝试使用Microsoft bot框架构建一个聊天机器人

时间:2016-05-07 19:31:43

标签: javascript node.js botframework botbuilder

我在这里定义了一个词典

var dict = {'English 101?': 'Room 205', 'English 102?': 'Room 309',
  'Math 301': 'Room 705', 'Math 302': 'Room 704'};

当用户询问“英语101在哪里”时,我希望机器人“在205室”回复。

我用以下方式硬编码:

var builder = require('botbuilder');
var helloBot = new builder.TextBot();
var dialog = new builder.CommandDialog();


dialog.matches('^Where is English 101?', builder.DialogAction.send('In     Room 205'));
dialog.matches('^Where is English 102?', builder.DialogAction.send('In     Room 309'));
dialog.matches('^Where is Math 301?', builder.DialogAction.send('In    Room 705'));
dialog.matches('^Where is Math 302?', builder.DialogAction.send('In     Room 704'));

dialog.onDefault(builder.DialogAction.send("I'm sorry. I didn't    understand."));

helloBot.listenStdin();

而不是硬编码每个问题我想将一些正则表达式传递给dialog.matches()函数第一个参数并使用它作为键,Bot应该能够从字典中获取值并发送回用户

我尝试了以下操作,但它不起作用:

var str = ""
dialog.matches(str = ? , builder.DialogAction.send(dict[str.slice(9)]))

我怎样才能将标准输入传递给“str”并从字典中获取值?

2 个答案:

答案 0 :(得分:1)

您想使用意图或触发动作匹配。

对于意图,首先,初始化你的意图:

var intents = new builder.IntentDialog();

然后将意图对话框传递给您的机器人:

bot.dialog('/', intents);

然后你可以使用正则表达式构建你的匹配:

intents.matches(/English 101/i, (session) => {
    //
});

您需要确保有一些东西来处理没有匹配的消息:

intents.onDefault([
    (session, args, next) => {
        //Do something by default
}]);

另一种方法是在对话框上使用triggerAction(也使用正则表达式):

bot.dialog("/English", (sess, args, next) => {
        //Handle English 101
    }).triggerAction({
        matches: /English 101/i
});

使用其中任何一种,您将能够使用标准的JavaScript正则表达式来捕获用户数据,并提供适当的对话框。

您还可以只查找/where is/i,然后从session.message.text的会话中获取消息,然后解析该消息以获取目标。

理想情况下,您的机器人非常适合使用LUIS。您可以为类位置创建单个意图,为该类创建单个实体。然后,您可以使用各种短语训练您的LUIS应用程序,同时用实体令牌替换确切的类名。然后,当LUIS达到您的意图时,该实体将可用,您可以使用该实体访问您的词典并获取该位置。如果你有兴趣的话我最近就wrote a post了。

(注意:这是用TypeScript编写的,但你得到了要点。)

答案 1 :(得分:0)

根据文档here,您只需在data:base64;image/(type),方法中使用Javascript正则表达式格式。

相关问题