用于实现的Dialogflow CX webhook使用nodejs回复用户



我尝试使用dialogflow实现库,但我想它是用于dialogflow ES的,所以现在我使用@谷歌云/dialogflow cx库,但我不知道如何使用这个库进行webhook连接,以回复使用fulfilments的用户,dialogflow cx可用的材料很少。

// use credentials or keyFilename i'm using keyFile
credentials: {
private_key: "-----BEGIN PRIVATE KEY-----==n-----END PRIVATE KEY-----n",

client_email:"pro1a3711.iam.gserviceaccount.com",
},                                                                                                                                                                                                                                                                                                                                                                              
keyFilename: './pr.json'
}
const {SessionsClient} = require('@google-cloud/dialogflow-cx');
const projectId = 'pro1-293711';
const location = 'global';
const agentId = 'da2271f5-0221-4dce-98d3-efa----9dd';
const languageCode = 'en';
const query = ['hello'];
// Imports the Google Cloud Some API library
//console.log(WebhooksClient)
const client = new SessionsClient(config);
//console.log("client",client)
async function detectIntentText() {
const sessionId = Math.random().toString(36).substring(7);
const sessionPath = client.projectLocationAgentSessionPath(
projectId,
location,
agentId,
sessionId
);
console.info(sessionPath);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
},
languageCode,
},
};
const [response] = await client.detectIntent(request);
console.log(`User Query: ${query}`);
for (const message of response.queryResult.responseMessages) {
if (message.text) {
console.log(`Agent Response: ${message.text.text}`);
}
}
if (response.queryResult.match.intent) {
console.log(
`Matched Intent: ${response.queryResult.match.intent.displayName}`
);
}
console.log(
`Current Page: ${response.queryResult.currentPage.displayName}`
);
}
detectIntentText()```

请注意,dialogflow-folfillment库仅支持dialogflow ES,@google-cloud/dialogflow-cx库仅用于node.js应用程序访问dialogflow cx API。

由于Dialogflow CX还没有可用的履行库,您可以参考Dialogflow CX的webhook请求和webhook响应,为您的Dialogflow CX代理构建webhook服务。

您也可以使用Node.js参考Dialogflow CX的示例webhook服务代码,并表达如下:

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/webhook", (request, response) => {
let tag = request.body.fulfillmentInfo.tag;
let jsonResponse = {};
if (tag == "welcome tag") {
//fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
jsonResponse = {
fulfillment_response: {
messages: [
{
text: {
//fulfillment text response to be sent to the agent
text: ["Hi! This is a webhook response"]
}
}
]
}
};
} else {
jsonResponse = {
//fulfillment text response to be sent to the agent if there are no defined responses for the specified tag
fulfillment_response: {
messages: [
{
text: {
////fulfillment text response to be sent to the agent
text: [
`There are no fulfillment responses defined for "${tag}"" tag`
]
}
}
]
}
};
}
response.json(jsonResponse);
});
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});

最新更新