Online code editor for Azure Bot Framework - Receiving error: destructure property 'applicationID' of 'undefined' or 'null'
Online code editor for Azure Bot Framework - Receiving error: destructure property 'applicationID' of 'undefined' or 'null'
我正在开发具有 LUIS 功能的简单 Bot Framework SDK v4 聊天机器人。我从 Echo 机器人开始,正在连接到我的 LUIS 数据库。我已将应用程序设置更改为适用于我的应用程序的密钥,但是,当我尝试 运行 机器人时,出现此错误:无法解构 属性 'applicationId' of '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 的初学者,并且我已经阅读了大量教程,因此非常感谢任何帮助。
您的.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);
});
});
并在机器人模拟器中测试:
@MdFaridUddinKiron 的回答太接近了!
您收到该错误是因为在 index.js
中,您没有将 luisApplication
传递给 MyBot
。
就像@MdFaridUddinKiron 的回答一样,你应该:
index.js
const bot = new LuisBot(luisApplication, luisPredictionOptions);
而不是:
const bot = new LuisBot();
你提到你是新手(一般来说可能是编程方面的),所以我会添加一些额外的帮助。
我强烈、强烈、强烈建议不要使用在线编辑器。 VS Code 免费而且很棒!如果您使用它,它可能会向您显示一个错误,指出到底出了什么问题。您可以使用它来编辑您的机器人:
- 正在下载并安装 VS Code
- 在 Azure 门户中,打开您的 Web App Bot 资源并转到“构建”>“下载 Bot 源代码”:
- 解压并在 VS Code 中打开
当您完成编辑并且想要 deploy/publish 将您的机器人返回 Azure 时,请按照 the Deployment Docs
之后,我建议学习如何使用断点进行正确调试。我在 this answer 的 Debug 下对其进行了一些分解。我在编程中学到这个 WAYYY 太晚了,它非常有帮助。
我正在开发具有 LUIS 功能的简单 Bot Framework SDK v4 聊天机器人。我从 Echo 机器人开始,正在连接到我的 LUIS 数据库。我已将应用程序设置更改为适用于我的应用程序的密钥,但是,当我尝试 运行 机器人时,出现此错误:无法解构 属性 'applicationId' of '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 的初学者,并且我已经阅读了大量教程,因此非常感谢任何帮助。
您的.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);
});
});
并在机器人模拟器中测试:
@MdFaridUddinKiron 的回答太接近了!
您收到该错误是因为在 index.js
中,您没有将 luisApplication
传递给 MyBot
。
就像@MdFaridUddinKiron 的回答一样,你应该:
index.js
const bot = new LuisBot(luisApplication, luisPredictionOptions);
而不是:
const bot = new LuisBot();
你提到你是新手(一般来说可能是编程方面的),所以我会添加一些额外的帮助。
我强烈、强烈、强烈建议不要使用在线编辑器。 VS Code 免费而且很棒!如果您使用它,它可能会向您显示一个错误,指出到底出了什么问题。您可以使用它来编辑您的机器人:
- 正在下载并安装 VS Code
- 在 Azure 门户中,打开您的 Web App Bot 资源并转到“构建”>“下载 Bot 源代码”:
- 解压并在 VS Code 中打开
当您完成编辑并且想要 deploy/publish 将您的机器人返回 Azure 时,请按照 the Deployment Docs
之后,我建议学习如何使用断点进行正确调试。我在 this answer 的 Debug 下对其进行了一些分解。我在编程中学到这个 WAYYY 太晚了,它非常有帮助。