microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.51k stars 2.45k forks source link

Manually trigger IntentDialog (LUIS) for testing #4327

Closed alucard001 closed 6 years ago

alucard001 commented 6 years ago

Bot Info

Issue Description

Hi all. I have 2 questions:

  1. It seems that I cannot use LUIS with my bot emulator. All I have to do is to deploy to azure for testing. Is there a way to use LUIS together with the emulator? I am pretty sure that my MS app ID/pwd and LUIS appID/key are correct.
  2. Please see my code below. Since I am currently using an emulator, for some unknown reason when I type myintent in chat box, the intents returned is null, therefore all messages goes to onDefault part of the code. Are there any way to manually trigger, or by modifying the code so that the matches goes to my_LUIS_intent?

Thank you very much in advance for all helping.

Code Example

var recognizer = new builder.LuisRecognizer(LuisModelUrl);

var intents = new builder.IntentDialog({ recognizers: [recognizer] })
    .matches('my_LUIS_intent', [
        (session, args, next) => {
            session.send('Some response');
        }
    ])
    .onDefault((session) => {
        let text = session.message.text;
        session.send([
            `Sorry, I did not understand: ${text}`,
            `抱歉,我不太明白什麼是: ${text}`
        ]);
    }).triggerAction({
        matches: /^(myintent|intents)$/i
    });
...
bot.dialog('/', intents);
stevengum commented 6 years ago
  1. How are you constructing your LuisModelUrl when you're testing locally/using the emulator?
  2. It sounds like the recognizer for LUIS isn't being called properly, which probably relates back to the first point. When you console log the args parameter from the first waterfall step in your `'my_LUIS_intent', what does it print?
stevengum commented 6 years ago

I don't recall offhand, but I seem to recall that in the Azure Bot Service LUIS template, the LuisModelUrl is constructed in something like this:

var LuisModelUrl = 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/' + process.env.appId
  + '?subscription-key=' + process.env.subscriptionKey + '&timezoneOffset=0&verbose=true&q=';

Are your environmental variables/process.env setup properly?

alucard001 commented 6 years ago

Thanks. I finally make it to work. My storage argument is using Azure storage, which I should use MemoryBotStorage instead.

And also thanks Steven providing LUIS 2.0 API, I just found myself copying LUIS 1.0 code.

Here is the full sample code:

/*-----------------------------------------------------------------------------
A simple echo bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
require('dotenv').config();

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

/*----------------------------------------------------------------------------------------
 * Bot Storage: This is a great spot to register the private state storage for your bot.
 * We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
 * For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
 * ---------------------------------------------------------------------------------------- */

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

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

// bot.use({
//     receive: function(event, next){
//         next();
//     },
//     botbuilder: function(session, next){
//         next();
//     },
//     send: function(event, next){
//         next();
//     }
// });

// 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/v2.0/apps/' + luisAppId + '?subscription-key=' + luisAPIKey + '&verbose=true';

// Main dialog with LUIS
var recognizer = new builder.LuisRecognizer(LuisModelUrl);

var intents = new builder.IntentDialog({ recognizers: [recognizer] })

intents
    .matches('my_luis_intent', function (session, args, next) {
        console.log(session);
        console.log(args);
        console.log(next);
    })
.onDefault((session) => {
    let text = session.message.text;
    session.send([
        `Sorry, I did not understand: ${text}`,
        `抱歉,我不太明白什麼是: ${text}`
    ]);
});

bot.dialog('/', intents);