【发布时间】:2019-03-24 17:56:40
【问题描述】:
我正在尝试制作一个脚本,该脚本接受用户的输入,通过 Dialogflow 运行它,然后将其返回给用户。我从中获取输入的平台仅支持 Node.js。我通过 glitch.com 托管机器人,但我认为这不是导致问题的原因。在向GitHub repo. 提交错误报告之前,我想在这里查看一下
var bot = 'the platform i use to accept inputs and send outputs'
bot.on("message", async message => {
console.log(message.content); // Log chat to console for debugging/testing
if (message.content.indexOf(config.prefix) === 0) { // Message starts with your prefix
let msg = message.content.slice(config.prefix.length); // slice of the prefix on the message
let args = msg.split(" "); // break the message into part by spaces
let cmd = args[0].toLowerCase(); // set the first word as the command in lowercase just in case
args.shift(); // delete the first word from the args
// You can find your project ID in your Dialogflow agent settings
const projectId = process.env.PROJECT_ID; //https://dialogflow.com/docs/agents#settings
const sessionId = 'quickstart-session-id';
var query = msg;
const languageCode = 'en-US';
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
// Send request and log result
sessionClient
.detectIntent(request)
.then(responses => {
console.log('Detected intent');
const result = responses[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
})
.catch(err => {
console.error('ERROR:', err);
});
}
return;
});
这是代码的相关部分。对于那些想知道的人,process.env.PROJECT_ID 是 glitch.com 用于任何私人的东西。因为我不希望随机的人获得我的项目 ID,所以我将其隐藏在其中,并在我未明确邀请的任何人面前隐藏它。
每次我执行此操作并尝试查询机器人时,它都会返回错误Uncaught Promise Error: TypeError: dialogflow.SessionsClient is not a constructor。
如果有人可以指导我了解我所缺少的内容或问题所在,那就太好了!
【问题讨论】:
标签: javascript node.js google-cloud-platform dialogflow-es