Dialogflow CX webhook 用于实现使用 nodejs 回复用户 [英] Dialogflow CX webhook for fulfilment to reply user using nodejs

查看:33
本文介绍了Dialogflow CX webhook 用于实现使用 nodejs 回复用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried using dialogflow-fulfillment library but I guess it is for Dialogflow ES so now I am using @google-cloud/dialogflow-cx library but I don't know how to use this library for webhook connection to reply to users using fulfilments, there is very little material available for 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-履行 库仅支持 Dialogflow ES@google-cloud/dialogflow-cx 库仅用于 node.js 应用程序访问 Dialogflow CX API.

Note that the dialogflow-fulfillment library only supports Dialogflow ES and the @google-cloud/dialogflow-cx library is only used for node.js applications to access Dialogflow CX API.

由于 Dialogflow CX 尚无可用的执行库,您可以参考 Dialogflow CX webhook 请求网络钩子响应,用于为您的 Dialogflow CX 代理构建网络钩子服务.

As there are no fulfillment libraries available yet for Dialogflow CX, you can refer to the Dialogflow CX webhook request and webhook response for building webhook services for your Dialogflow CX agent.

您还可以参考下方使用 Node.js 和 express 的 Dialogflow CX 示例 Webhook 服务代码:

You can also refer to the sample webhook service code for Dialogflow CX using Node.js and express below:

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);
});

这篇关于Dialogflow CX webhook 用于实现使用 nodejs 回复用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆