无法使用TriggerAction与Microsoft Bot框架中的LUIS意图(node.js)匹配

时间:2018-04-04 07:27:15

标签: node.js azure botframework luis facebook-chatbot

我遇到一个问题,即在提示选项内部无法使用triggerAction与luis意图匹配。对于我当前的代码,如果用户键入的单词与我硬编码的确切语句(如“Card1”)匹配,则triggerAction将开始对话。

当用户到达card2对话框时,它将显示包含提示问题和选项的对话框(Card2Link)的消息。

渴望:如果用户决定他/她想要输入一个应该尝试与luis意图匹配的随机单词并触发意图对话框。如果随机单词不符合luis意图或任何luis意图,它将触发“请再次选择你的选择”。

实际:当用户输入随机字时,它不会搜索luis意图并立即提供错误信息“请选择您的选择.1。Card3”。

请就此问题提出建议。我一直坚持这个问题很长一段时间。

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata 
});

server.post('/api/messages', connector.listen());

var tableName = 'dataBT';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);


bot.beginDialogAction('Card1', '/Card1')

// Make sure you add code to validate these fields
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com';

const LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v1/application?id=' + luisAppId + '&subscription-key=' + luisAPIKey;

// Main dialog with LUIS
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
var intents = new builder.IntentDialog({ recognizers: [recognizer] })

.matches('Card1', (session) => {
     session.beginDialog('/Card1')
 })

// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);

bot.dialog('/', intents); 

bot.dialog('/cards', [
  function (session) {

    session.send('Test1');
    var msg = new builder.Message(session)
        .attachmentLayout(builder.AttachmentLayout.carousel)
        .textFormat(builder.TextFormat.xml)
        .attachments([
          new builder.HeroCard(session)
                .title('Test1')
                .images([
                  builder.CardImage.create(session, 'imgURL')
                ])
                .buttons([builder.CardAction.dialogAction(session, 'Card1', null, 'Here')
                  ])
        ]);
        msg.addAttachment (
            new builder.HeroCard(session)
                .title("Test2")
                .images([
                    builder.CardImage.create(session, "imgUrl")
                ])
                .buttons([
                    builder.CardAction.dialogAction(session, "Card2", null, "Here")
                ])
        );

    session.endDialog(msg)
  }
]).triggerAction({ matches: /^hi/i });

//Adding of new card
bot.dialog('/Card1', [
  function (session) {
    var msg1 = new builder.Message(session).sourceEvent({
    //specify the channel
    facebook: {
      text:"Card1"
    }
  });

    session.endDialog(msg1)
    session.beginDialog('/card1link');
  }
]).triggerAction({ matches: /^Card1/i });

bot.dialog('/card1link', [
    function (session, args, next) {
        builder.Prompts.choice(session, '1. Card2\n2. Card3\n', ['1', '2'], {
            retryPrompt: "Please pick your choice.\n1. Card2\n2. Card3\n",
            maxRetries: 1
        });
    },
    function (session, args, next) {
        if (args.response) {
            var choice = args.response.entity;
            switch (choice) {
                case '1':
                    session.replaceDialog('Card2');
                    break;
                case '2':
                    session.replaceDialog('Card3');
                    break;
            }
        }
        else{
            session.send("Sorry");
        }
    }
]);

bot.dialog('/Card2', [
  function (session) {
    var msg1 = new builder.Message(session).sourceEvent({
    //specify the channel
    facebook: {
      text:"Card2"
    }
  });

    session.endDialog(msg1)
    session.beginDialog('/card2link');
  }
]).triggerAction({ matches: /^Card2/i });

bot.dialog('/card2link', [
    function (session, args, next) {
        builder.Prompts.choice(session, '1. Card3\n', ['1'], {
            retryPrompt: "Please pick your choice.\n1. Card3\n",
            maxRetries: 1
        });
    },
    function (session, args, next) {
        if (args.response) {
            var choice = args.response.entity;
            switch (choice) {
                case '1':
                    session.replaceDialog('Card3');
                    break;
            }
        }
        else{
            session.send("Sorry");
        }
    }
]);

2 个答案:

答案 0 :(得分:2)

您可以使用同义词。

builder.Prompts.choice(
        session, 
        "Selection one option", 
        [
            {
                value: "1",
                synonyms: ["Card1", "Card 1"]
            },
            {
                value: "2",
                synonyms: ["Card2", "Card 2"]
            }
        ],
        {
            listStyle: builder.ListStyle.button,
            retryPrompt: 'Selection one option.'
        }
    )

答案 1 :(得分:2)

请尝试使用bot.recognizer(recognizer);替换var intents = new builder.IntentDialog({ recognizers: [recognizer] }); bot.dialog('/', intents);

哪个应该达到你的要求。请参考以下测试代码:

bot.recognizer(recognizer);
bot.dialog('card', [(session) => {
    builder.Prompts.choice(session, '1. Card2\n2. Card3\n', ['1', '2'], {
        retryPrompt: "Please pick your choice.\n1. Card2\n2. Card3\n",
        maxRetries: 1
    });
}, (session, args, next) => {
    if (args.response) {
        var choice = args.response.entity;
        switch (choice) {
            case '1':
                session.send('Card2');
                break;
            case '2':
                session.send('Card3');
                break;
        }
    } else {
        session.send("Sorry");
    }
}]).triggerAction({
    matches: /card/i,
})

其结果如下:
enter image description here