首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dialogflow CX webhook,用于实现使用nodejs回复用户

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

Stack Overflow用户
提问于 2020-11-02 21:53:22
回答 1查看 2.8K关注 0票数 2

我试过使用dialogflow-fulfillment库,但我猜它是用于Dialogflow ES的,所以现在我使用@google-cloud/ Dialogflow -cx库,但我不知道如何使用这个库进行webhook连接来回复使用实现的用户,Dialogflow CX的可用材料很少。

代码语言:javascript
复制
// 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()```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 06:57:51

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

由于Dialogflow CX尚无实施库可用,因此您可以参考Dialogflow CX webhook requestwebhook response为您的Dialogflow CX代理构建webhook服务。

您还可以使用下面的Node.js和express引用Dialogflow CX的webhook服务示例代码:

代码语言:javascript
复制
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);
});
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64646879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档