如何使用Javascript Ajax调用在DialogFlow v2上进行http调用 [英] How to make http call on DialogFlow v2 using Javascript ajax call
问题描述
我使用Node.js在DialogFlow的官方网站上找到了这个示例,它运行正常,但是我不知道如何将其集成到我的Web应用程序中。
I found this example on the official site of DialogFlow using Node.js and it is working fine, but I dont know how do I integrate this into my web application.
是否可以将其集成到其他javascript jquery代码中?在这里我需要运行节点index.js,但是如果我与代码集成,是否还需要这样做?
Is it possible that I can integrate this into my other javascript jquery code? and here I need to run node index.js but do I still need to do this if I integrate with my code?
const projectId = 'xxx'; //https://dialogflow.com/docs/agents#settings
const sessionId = 'xxxxx';
const query = 'Hello';
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);
console.log(sessionPath);
// 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);
});
有没有其他方法可以使用普通javascript jquery,ajax使用DialogFlow v2,而无需我做每次我想使用dialogflow时都使用node index.js。
Are there any alternative that we can use DialogFlow v2 using normal javascript jquery, ajax without me having to do node index.js everytime I want to use dialogflow.
DialogFlow v1的使用非常简单。我有这样的东西:
DialogFlow v1 was quite simple to use. I had it something like this:
fetch(url, {
body: JSON.stringify(data),
// cache: 'no-cache',
// credentials: 'same-origin',
headers: {
'content-type': 'application/json',
"Authorization": "Bearer " + configs.accessToken,
},
method: 'POST',
mode: 'cors',
redirect: 'follow',
referrer: 'no-referrer',
})
.then(response => response.json()) // parses response to JSON
推荐答案
您可以轻松调用Dialogflow的V2 API detectIntent $ c
You can easily call Dialogflow's V2 API detectIntent
endpoint from jQuery.
API文档显示URL和请求格式:
The API docs show the URL and request formats:
POST https://dialogflow.googleapis.com/v2/{session=projects/*/agent/sessions/*}:detectIntent
{
"queryParams": {
object(QueryParameters)
},
"queryInput": {
object(QueryInput)
},
"inputAudio": string
}
身份验证的工作方式略有不同;您无需使用访问令牌,而可以使用Cloud信息中心创建服务帐户和密钥。 此文档页面说明了操作方法。
Authentication works slightly differently; instead of using an access token, you'll create a service account and key using the Cloud dashboard. This documentation page explains how.
这篇关于如何使用Javascript Ajax调用在DialogFlow v2上进行http调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!