我正在开发一个具有LUIS功能的简单Bot Framework SDK v4聊天机器人。我从Echo机器人开始,目前正在连接到LUIS数据库。我已将应用程序设置更改为适合我的应用程序的键,但是,当我尝试运行机器人时,出现以下错误:无法解构属性“ applicationId”的“ undefined”或“ null”,导致我认为它无法访问.env文件。 这是我的bot.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);
}
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;
}
}
}
这是我的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 { 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);
});
});
我显然是bot框架和node.js的初学者,并且我已经阅读了大量的教程,因此对您的帮助将不胜感激。
答案 0 :(得分:2)
您必须错过.env文件中的相关参数。我从我的角度进行了测试,并且效果很好。 这是.env文件:
这是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;
这是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);
});
});
并在漫游器模拟器中进行测试:
答案 1 :(得分:1)
@MdFaridUddinKiron的答案是如此接近!
您会收到此错误,因为在index.js
中,您没有将luisApplication
传递给MyBot
。
就像@MdFaridUddinKiron的答案一样,您应该具有:
index.js
const bot = new LuisBot(luisApplication, luisPredictionOptions);
代替:
const bot = new LuisBot();
您提到您是一种新手(一般来说可能是编程的),所以我将添加一些其他帮助。
我强烈建议不要使用在线编辑器。 VS Code是免费的,真棒!如果您使用过它,则可能会向您显示一个错误,指出确切的错误所在。您可以通过以下方式使用它来编辑您的机器人:
完成编辑后,要将机器人部署/发布回Azure,请遵循the Deployment Docs
之后,我建议学习如何使用断点正确调试。我在 Debug 下的this answer中对其进行了细分。我在编程时就学到了WAYYY,为时已晚,对您有很大帮助。