我正在尝试为DialogFlow聊天机器人实现一个自定义聊天窗口。AFAIK,我需要创建一个服务器来处理来自聊天窗口的请求,将它们转发到DialogFlow以获得响应,然后将响应返回到聊天窗口。我遵循了示例来自DialogFlow Node.js客户端SDK (步骤6:“尝试一个示例”),最后得到如下结果:
require('dotenv').config()
const dialogflow = require('dialogflow');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
const sessionClient = new dialogflow.SessionsClient({
credentials: {
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.PRIVATE_KEY
}
});
async function getResponse(req, res) {
// A unique identifier for the given session
console.log("body", req.body);
const sessionId = req.body.session.split('/').pop();
// Create a new session
console.log("session", sessionId)
const sessionPath = sessionClient.sessionPath(process.env.PROJECT_ID, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: req.body.queryResult.queryText,
// The language used by the client (en-US)
languageCode: 'en-US',
}
}
};
console.log("send request", request)
// Send request and log result
const responses = await sessionClient.detectIntent(req);
const result = responses[0].queryResult;
res.json(result);
}
app.post('/', getResponse);
app.get('/', (req, res) => res.send('Use POST'));
app.listen(PORT, () => {
console.log('Server is running on PORT:',PORT);
});尽管最初的示例工作正常,但使用Postman向我的服务器发送POST请求会导致以下错误:
(node:724) UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: Resource name '' does not match 'projects/*/locations/*/agent/environments/*/users/*/sessions/*'.
at Object.callErrorFromStatus (C:\Users\rudyt\Documents\Github\metbot-fulfillment\node_modules\@grpc\grpc-js\build\src\call.js:30:26)
at Http2CallStream.call.on (C:\Users\rudyt\Documents\Github\metbot-fulfillment\node_modules\@grpc\grpc-js\build\src\client.js:96:33)
at Http2CallStream.emit (events.js:194:15)
at process.nextTick (C:\Users\rudyt\Documents\Github\metbot-fulfillment\node_modules\@grpc\grpc-js\build\src\call-stream.js:75:22)
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:724) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:724) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.我不知道这个错误是从哪里来的,我在getResponse函数中是如何格式化请求的,还是与邮递员有关?我正在进行的查询(在Postman端)是对http://localhost:3000的一个帖子,其中Content-Type头设置为application/json,请求体设置为原始JSON (我粘贴在这个样例Web钩子请求中)。我倾向于问题是请求DialogFlow,但我包括邮递员的信息,以防万一。
发布于 2019-11-04 21:11:05
我认为问题是,您没有发送您认为您要发送到detectIntent()的东西。
假设这段代码打算在某个服务器上运行,从JavaScript客户端获取请求,然后创建对对话框流的请求--实际上并不是这样做的。
尽管您正在创建请求(在一个名为request的常量中)并记录它
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: req.body.queryResult.queryText,
// The language used by the client (en-US)
languageCode: 'en-US',
}
}
};
console.log("send request", request)当您将其发送到对话框流时,您不是在发送request对象,而是发送req对象,这是从客户端获得的:
const responses = await sessionClient.detectIntent(req);如果不首先在服务器中对客户端进行消毒,您就不应该从客户端传递东西。
我怀疑如果你把它改成这样的话,它应该能用:
const responses = await sessionClient.detectIntent(request);https://stackoverflow.com/questions/58598225
复制相似问题