[英]Prompt not waiting for user input
我目前正在嘗試用Microsofts Bot Framework編寫一個Chatbot。 我想使用選擇提示,但它不等待我/用戶選擇一個選項。 甚至這里的示例代碼也不適用於我。 看這個工作示例:
var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
openIdMetadata: process.env.BotOpenIdMetadata
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage());
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com';
var LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v2.0/apps/' + `enter code here`luisAppId + '?subscription-key=' + luisAPIKey;
/**
* RECOGNIZERS
*/
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
bot.recognizer(recognizer);
/**
* DIALOG SECTION
*/
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);
intents.onDefault([
function(session){
session.send('I am sorry but I don`t know that.');
}
]);
intents.matches('City' ,[
function(session, args) {
session.beginDialog('getLocationDialog');
session.endDialog();
}
]);
var locationLabels = {
City1: 'City 1',
City2: 'City 2',
City3: 'City 3'
};
bot.dialog('getLocationDialog', [
function (session) {
builder.Prompts.choice(session, "Choose a city", locationLabels
, { listStyle: builder.ListStyle.button },{
maxRetries: 3,
retryPrompt: 'This is not a valid option. Choose one of the options listed above.' });
},
function (session, results) {
console.log(results.response.entity);
if (results.response){
var location = locationLabels[results.response.entity];
session.send('You are interested in %s', location);
session.endDialog();
}
else {
session.send('Okay.');
session.endDialog();
}
}
]);
帶有提示的對話框是此部分(使用LUIS匹配intent):
intents.matches('City' ,[
function(session, args) {
session.beginDialog('getLocationDialog');
session.endDialog();
}
]);
var locationLabels = {
City1: 'City 1',
City2: 'City 2',
City3: 'City 3'
};
bot.dialog('getLocationDialog', [
function (session) {
builder.Prompts.choice(session, "Choose a city", locationLabels
, { listStyle: builder.ListStyle.button },{
maxRetries: 3,
retryPrompt: 'This is not a valid option. Choose one of the options listed above.' });
},
function (session, results) {
console.log(results.response.entity);
if (results.response){
var location = locationLabels[results.response.entity];
session.send('You are interested in %s', location);
session.endDialog();
}
else {
session.send('Okay.');
session.endDialog();
}
}
]);
請參閱此代碼的實際操作
機器人不等我選擇一個選項。 有人可以告訴問題在哪里嗎?
請嘗試下面列出的代碼。 我做了一些改變,讓它在我的最后工作。 首先,刪除'City'意圖匹配中的session.EndDialog()調用。 該調用導致對話框在原始對話框完成之前重新開始。
其次,您將locationLabels變量設置為鍵/值對。 這需要是一個簡單的數組。 如果需要使用鍵/值對,請考慮使用iChoice。
第三,只需要為您的位置變量分配results.response.entity。 由於locationLabels是一個簡單的數組,因此您不再需要嘗試匹配它。
希望這可以幫助。
史蒂夫。
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);
intents.onDefault([
function(session){
session.send('I am sorry but I don`t know that.');
}
]);
intents.matches('City' ,[
function(session, args) {
session.beginDialog('getLocationDialog');
// session.endDialog();
}
]);
var locationLabels = ['City 1', 'City 2', 'City 3'];
bot.dialog('getLocationDialog', [
function (session) {
builder.Prompts.choice(session, "Choose a city", locationLabels, { listStyle: builder.ListStyle.button });
},
function (session, results) {
console.log(results.response.entity);
var location = results.response.entity;
if (results.response){
session.send('You are interested in %s', location);
session.endDialog();
}
else {
session.send('Okay.');
session.endDialog();
}
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.