DialogFlow v2 Error: Resource name '' does not match 'projects/*/locations/*/agent/environments/*/users/*/sessions/*'

DialogFlow v2 Error: Resource name '' does not match 'projects/*/locations/*/agent/environments/*/users/*/sessions/*'

我正在尝试为 DialogFlow 聊天机器人实现自定义聊天 window。据我所知,我需要创建一个服务器来处理来自聊天 window 的请求,将它们转发到 DialogFlow 以获得响应,然后 return 对聊天 window 的响应。我遵循了 DialogFlow Node.js 客户端 SDK 中的 sample(第 6 步:"Try an example"),结果是:

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 有某种关系?我正在做的查询(在 Postman 端)是 POST 到 http://localhost:3000 with the Content-Type header set to application/json and the request body set as raw JSON (I pasted in this sample Webhook request)。我倾向于 DialogFlow 请求的问题,但我包含了 Postman 信息以防万一。

我认为问题在于您没有发送您认为发送给 detectIntent() 的内容。

假设此代码旨在 运行 在某处服务器上,从 JavaScript 客户端获取请求,然后创建对 Dialogflow 的请求 - 你实际上并没有这样做。

尽管您正在创建请求(在名为 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)

当您将其发送到 Dialogflow 时,您发送的不是 request 对象,而是 req 对象,它来自客户端:

const responses = await sessionClient.detectIntent(req);

你应该永远不要传递来自客户端的东西而不先在你的服务器中对其进行清理。

我怀疑如果你把它改成这样,它应该可以工作:

const responses = await sessionClient.detectIntent(request);