是否存在将Dialogflow代理连接到node.js
代码的简单方法?当我使用此代码与Dialogflow代理的设置页面中的正确projectID时,我有以下错误:
错误:获取应用程序默认凭据时出现意外错误:无法加载默认凭据。浏览https://developers.google.com/accounts/docs/application-default-credentials以获取更多信息。
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
我访问了该页面,但是出于我的想法,我觉得很混乱(他们引用了其他API和很多设置),我该如何解决这个问题?
我想从文件中获取信息并加载所有内容而不安装API或其他一些内容......
答案 0 :(得分:8)
它没有很好的文档记录,但最简单的身份验证方法是使用Google云平台控制台上提供的JSON文件。
const sessionClient = new dialogflow.SessionsClient({
keyFilename: '/path/to/google.json'
});
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
这也适用于所有其他客户。 ContextsClients
,EntityTypesClient
等等。
答案 1 :(得分:3)
我正在编写对我有用的代码。请按照Reference link 2中提供的所有步骤进行操作,出于编码目的,您可以使用提供的代码段。
我还添加了Google Cloud Oauth的示例JSON
参考文献:
//Downloaded JSON format
{
"type": "service_account",
"project_id": "mybot",
"private_key_id": "123456asd",
"private_key": "YOURKEY",
"client_email": "yourID@mybot.iam.gserviceaccount.com",
"client_id": "098091234",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/yourID%40mybot.iam.gserviceaccount.com"
}
//------*********************---------------------------
//
const projectId = 'mybot';
//https://dialogflow.com/docs/agents#settings
// generate session id (currently hard coded)
const sessionId = '981dbc33-7c54-5419-2cce-edf90efd2170';
const query = 'hello';
const languageCode = 'en-US';
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
let privateKey = 'YourKey';
// as per goolgle json
let clientEmail = "yourID@mybot.iam.gserviceaccount.com";
let config = {
credentials: {
private_key: privateKey,
client_email: clientEmail
}
}
const sessionClient = new dialogflow.SessionsClient(config);
// 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);
});
答案 2 :(得分:1)
几个月前我也遇到了同样的问题,请检查一下,这就是我的解决方法。 Google Cloud从您的JSON中提取出这些行。
const dialogflow = require('dialogflow');
const LANGUAGE_CODE = 'en-US'
const projectId = 'projectid';
const sessionId = 'sessionId';
const query = 'text to check';
let privateKey = "private key JSON";
let clientEmail = "email acount from JSON";
let config = {
credentials: {
private_key: privateKey,
client_email: clientEmail
}
};
sessionClient = new dialogflow.SessionsClient(config);
async function sendTextMessageToDialogFlow(textMessage, sessionId) {
// Define session path
const sessionPath = this.sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: textMessage,
languageCode: LANGUAGE_CODE
}
}
}
try {
let responses = await this.sessionClient.detectIntent(request)
console.log('DialogFlow.sendTextMessageToDialogFlow: Detected intent', responses);
return responses
} catch (err) {
console.error('DialogFlow.sendTextMessageToDialogFlow ERROR:', err);
throw err
}
};
sendTextMessageToDialogFlow(query, sessionId)
答案 3 :(得分:1)
由于原始问题,对Dialogflow身份验证的文档进行了改进。您应该在这里找到所有答案:
答案 4 :(得分:1)
我几乎没有改变地遵循上述解决方案:
// A unique identifier for the given session
const sessionId = uuid.v4();
// Create a new session
const sessionClient = new dialogflow.SessionsClient({
keyFilename: require("path").join('config/google-credential.json')
});
const sessionPath = sessionClient.sessionPath(process.env.DIALOGFLOW_PROJECTID, sessionId);