我正在尝试使用Microsoft bot框架来构build聊天机器人

我在这里定义了一本字典

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(); 

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

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

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

我怎么能够将标准input传递给“str”并从字典中获取值?

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

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

 var intents = new builder.IntentDialog(); 

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

 bot.dialog('/', intents); 

然后你可以使用正则expression式来构build你的匹配:

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

你将要确保有一些东西来处理消息没有匹配太:

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

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

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

无论是哪一种,您都可以使用标准的JavaScript正则expression式来捕获用户数据,并提供适当的对话框。

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

理想情况下,您的机器人非常适合使用LUIS。 您可以为class级位置创build一个单一的意图,并为class级创build一个单一的实体。 然后,您可以使用各种短语来训练您的LUIS应用程序,同时用实体标记replace确切的类名称。 然后,当LUIS达到你的意图时,实体将可用,你可以使用它来访问你的字典,并获得位置。 如果你有兴趣,我最近写了一篇文章 。

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

根据这里的文档,你应该简单地在你的matches方法中使用Javascript正则expression式格式。