Judy
Judy

Reputation: 194

Online code editor for Azure Bot Framework - Receiving error: destructure property 'applicationID' of 'undefined' or 'null'

I am working on a simple Bot Framework SDK v4 chatbot with LUIS capabilities. I began with an Echo bot and am in the process of connecting to my LUIS database. I have changed the application settings to the appropriate keys for my app, however, when I try to run the bot, I get this error: Cannot destructure property 'applicationId' of 'undefined' or 'null', causing me to think that it is having trouble accessing the .env file. Here is my bot.js code:

const { ActivityHandler } = require('botbuilder');
const { BotFrameworkAdapter } = require('botbuilder');
const { LuisRecognizer } = require('botbuilder-ai');

class LuisBot {
    constructor(application, luisPredictionOptions) {
        this.luisRecognizer = new LuisRecognizer(application, luisPredictionOptions);
    }

    async onTurn(turnContext) {
    // Make API call to LUIS with turnContext (containing user message)
    const results = await this.luisRecognizer.recognize(turnContext);

    // Extract top intent from results
    const topIntent = results.luisResult.topScoringIntent;
    switch (topIntent.intent) {
        case 'Greeting':
            await turnContext.sendActivity('Hey! Ask me something to get started.');
            break;

        case 'UpdateInfo':
            await updateInfoIntent.handleIntent(turnContext);
            break;
        }
    }
}

And here is my index.js code:

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const dotenv = require('dotenv');
const path = require('path');
const restify = require('restify');

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');

// This bot's main dialog.
const { LuisBot } = require('./bot');

// Import required bot configuration.
const ENV_FILE = path.join(__dirname, '.env');

dotenv.config({ path: ENV_FILE });

// Create HTTP server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
    console.log(`\n${ server.name } listening to ${ server.url }`);
    console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
    console.log(`\nTo talk to your bot, open the emulator select "Open Bot"`);
});

const luisApplication = {
    applicationId: process.env.LuisAppId,
    endpointKey: process.env.LuisAuthoringKey,
    azureRegion: process.env.LuisAzureRegion
};

const luisPredictionOptions = {
    includeAllIntents: true,
    log: true,
    staging: false
};

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.

const adapter = new BotFrameworkAdapter({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    channelService: process.env.ChannelService,
    openIdMetadata: process.env.BotOpenIdMetadata
});


// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
    // This check writes out errors to console log .vs. app insights.
    console.error(`\n [onTurnError]: ${ error }`);

    // Send a message to the user
    await context.sendActivity(`Oops. Something went wrong!`);
};

// Create the main dialog.
const bot = new LuisBot();

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
    adapter.processActivity(req, res, async (context) => {
        // Route to main dialog.
        await bot.run(context);
    });
});

I'm obviously a beginner with the bot framework and node.js and I have already been reading through a ton of tutorials, so any help would be greatly appreciated.

Upvotes: 0

Views: 570

Answers (2)

mdrichardson
mdrichardson

Reputation: 7241

@MdFaridUddinKiron's answer is so close!

You get that error because in index.js, you're not passing luisApplication to MyBot.

Like in @MdFaridUddinKiron's answer, you should have:

index.js

const bot = new LuisBot(luisApplication, luisPredictionOptions);

instead of:

const bot = new LuisBot();

You mentioned you were kind of new (maybe to programming, in general), so I'll add some additional help.

I highly, highly, highly recommend against using the Online Editor. VS Code is free and AWESOME! If you would have used it, it would likely have shown you an error indicating exactly what was wrong. You can use it to edit your bot by:

  1. Downloading and installing VS Code
  2. In the Azure Portal, open your Web App Bot resource and go to Build > Download Bot Source Code:

enter image description here

  1. Unzip it and open in VS Code

When you're done editing and you want to deploy/publish your bot back to Azure, follow the Deployment Docs

After that, I recommend learning how to properly debug using breakpoints. I break it down a little in this answer, under Debug. I learned this WAYYY too late in programming and it has been SUPER helpful.

Upvotes: 1

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22457

You must miss the related parameters in your .env file. I have a test from my side, and it works well. Here is the .env file:

enter image description here

Here is the bot2.js

const { ActivityHandler } = require('botbuilder');
const { BotFrameworkAdapter } = require('botbuilder');
const { LuisRecognizer } = require('botbuilder-ai');

class LuisBot {
    constructor(application, luisPredictionOptions) {
        this.luisRecognizer = new LuisRecognizer(application, luisPredictionOptions, true);
    }
    async onTurn(turnContext) {
        // Make API call to LUIS with turnContext (containing user message)
        try {
            const results = await this.luisRecognizer.recognize(turnContext);

            //console.log(results);
            // Extract top intent from results
            const topIntent = results.luisResult.topScoringIntent;
            switch (topIntent.intent) {
                case 'Greeting':
                    await turnContext.sendActivity('Hey! Ask me something to get started.');
                    break;

                case 'UpdateInfo':
                    await updateInfoIntent.handleIntent(turnContext);
                    break;
                default:
                    await turnContext.sendActivity('Hey!');
            }
        } catch (error) {
        }
    }
}
module.exports.LuisBot = LuisBot;

Here is the index.js:

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const dotenv = require('dotenv');
const path = require('path');
const restify = require('restify');

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');

// This bot's main dialog.
// const { EchoBot } = require('./bot');
const { LuisBot } = require('./bot2');

// Import required bot configuration.
const ENV_FILE = path.join(__dirname, '.env');
dotenv.config({ path: ENV_FILE });

// Create HTTP server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
    console.log(`\n${ server.name } listening to ${ server.url }`);
    console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
    console.log(`\nTo talk to your bot, open the emulator select "Open Bot"`);
});

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new BotFrameworkAdapter({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    channelService: process.env.ChannelService,
    openIdMetadata: process.env.BotOpenIdMetadata
});

const luisApplication = {
    applicationId: process.env.LuisAppId,
    endpointKey: process.env.LuisAuthoringKey,
    azureRegion: process.env.LuisAzureRegion
};
const luisPredictionOptions = {
    includeAllIntents: true,
    log: true,
    staging: false
};

// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
    // This check writes out errors to console log .vs. app insights.
    console.error(`\n [onTurnError]: ${ error }`);
    // Send a message to the user
    await context.sendActivity(`Oops. Something went wrong!`);
};

// Create the main dialog.
// const bot = new EchoBot();
// Create the main dialog.
const bot = new LuisBot(luisApplication, luisPredictionOptions);

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
    // console.log(process.env.LuisAppId);
    adapter.processActivity(req, res, async (context) => {
        // Route to main dialog.
        // console.log(process.env.LuisAppId);
        await bot.onTurn(context);
    });
});

And test in bot emulator:

enter image description here enter image description here

Upvotes: 2

Related Questions