这是一个完整的示例,演示了从 LUIS 意图触发的库文件中的对话框:
带有启动文件,例如:
require('dotenv').config();
const restify = require('restify');
const builder = require('botbuilder');
var listenPort = process.env.port || process.env.PORT || 3978;
var server = restify.createServer();
server.listen(listenPort, '::', () => {
console.log(`smooth like a baby's bottom`)
});
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector, function (session) {
session.endDialog(`I'm sorry, I did not understand '${session.message.text}'.\nType 'help' to know more about me :)`)
}).set('storage', inMemoryStorage);
var luisRL = new builder.LuisRecognizer(process.env.LUIS_MODEL_URL).onEnabled(function (context, callback) {
var enabled = context.dialogStack().length === 0
callback(null, enabled)
});
var intents = new builder.IntentDialog({ recognizers: [luisRL] });
bot.recognizer(intents);
server.post('/api/messages', connector.listen());
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
greetings = `Ask to set or change your name...`;
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.send(new builder.Message()
.address(message.address)
.text(greetings)
)
}
})
}
});
bot.library(require('./dialogs/profile').createLibrary());
具有以下/dialogs/profile/index.js
var builder = require('botbuilder');
var lib = new builder.Library('profile');
exports.createLibrary = function () {
return lib.clone();
}
lib.dialog('changeName', [
function (session) {
debugger;
session.dialogData.updating = session.userData.hasOwnProperty('name');
var prompt = session.dialogData.updating ? "What would you like to change your name to?" : "Hi... What's your name?";
builder.Prompts.text(session, prompt);
},
function (session, results) {
session.userData.name = results.response;
session.endDialog("Got it %s...", session.userData.name);
}
]).triggerAction({ matches: 'ChangeNameIntent' });
使用以下 LUIS 模型:
{
"luis_schema_version": "3.1.0",
"versionId": "0.1",
"name": "ChangeNameLuisModel",
"desc": "",
"culture": "en-us",
"intents": [
{
"name": "ChangeNameIntent"
},
{
"name": "getage"
},
{
"name": "None"
}
],
"entities": [
{
"name": "speed",
"roles": []
}
],
"composites": [],
"closedLists": [],
"patternAnyEntities": [],
"regex_entities": [],
"prebuiltEntities": [
{
"name": "number",
"roles": []
}
],
"model_features": [],
"regex_features": [],
"patterns": [],
"utterances": [
{
"text": "allow me to change my name",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "change my name",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "change name",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "i am 10 years old",
"intent": "getage",
"entities": []
},
{
"text": "i am 12 years old",
"intent": "getage",
"entities": []
},
{
"text": "i want to change my name",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "let me alter my name",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "name change",
"intent": "ChangeNameIntent",
"entities": []
},
{
"text": "please change my name",
"intent": "ChangeNameIntent",
"entities": []
}
],
"settings": []
}
我是一名优秀的程序员,十分优秀!