首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何接收从DialogFlow发送的数据?

如何接收从DialogFlow发送的数据?
EN

Stack Overflow用户
提问于 2020-01-22 06:55:20
回答 1查看 624关注 0票数 2

我正在为我的网站创建一个聊天机器人。因此,我使用的是Kommunicate.ioDialogFlow。我在我的html文件中添加了一个由kCommunic式给出的脚本。为了从html页面向bot发送自定义数据,我们在"onInit"函数中使用了onInit变量,但我无法在对话框bot中获得如何使用这些数据。这是我的通讯剧本:

代码语言:javascript
复制
(function(d, m){
        var kommunicateSettings = {"appId":"7519ee060abee2b532e8565aa0527aed","popupWidget":true,"automaticChatOpenOnNavigation":true, 
                "title": "test",
                 "appSettings": {
                        "chatWidget": {
                          "popup": true,           // To enable or disable the pre-chat popup (boolean)
                          //"emojilibrary" : true
                        },
                        "chatPopupMessage": [{
                          "message": "Wanna ask something related to "+document.title+ "?", // Message to be displayed in the pre-chat popup (string)
                          "delay": 3000                    // Delay interval of pre-chat popup (number in milliseconds)
                        }],
                 },
                 "onInit" : function(){
                     var chatContext = {
                      "lat":"Latitude° N",
                      "lon":"Longitude° E"
                    }
                    Kommunicate.updateChatContext(chatContext);
                 }
        };

        var s = document.createElement("script"); s.type = "text/javascript"; s.async = true;
        s.src = "https://widget.kommunicate.io/v2/kommunicate.app";
        var h = document.getElementsByTagName("head")[0]; h.appendChild(s);
        window.kommunicate = m; m._globals = kommunicateSettings;
      })(document, window.kommunicate || {});

这是我的DialogFlow代码:

代码语言:javascript
复制
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Payload} =require('dialogflow-fulfillment');
const i18n= require('i18n');
const express=require('express');
var router=express.Router();

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  function About(agent){
    agent.add(`We are a company!`);
  }

  // // Uncomment and edit to make your own intent handler
  // // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function yourFunctionHandler(agent) {
  //   agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
  //   agent.add(new Card({
  //       title: `Title: this is a card title`,
  //       imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
  //       text: `This is the body text of a card.  You can even use line\n  breaks and emoji! `,
  //       buttonText: 'This is a button',
  //       buttonUrl: 'https://assistant.google.com/'
  //     })
  //   );
  //   agent.add(new Suggestion(`Quick Reply`));
  //   agent.add(new Suggestion(`Suggestion`));
  //   agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
  // }

  // // Uncomment and edit to make your own Google Assistant intent handler
  // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function googleAssistantHandler(agent) {
  //   let conv = agent.conv(); // Get Actions on Google library conv instance
  //   conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
  //   agent.add(conv); // Add Actions on Google library responses to your agent's response
  // }
  // // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
  // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
   intentMap.set('About the company', About);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

我想在我的欢迎辞中展示"lat“和"lon”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-24 12:32:10

您在chatContext对象中传递的数据可以是从您的实现代码中访问。此数据可作为originalDetectIntentRequest参数的一部分在request对象中使用。

这是一个例子:

代码语言:javascript
复制
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

  const agent = new WebhookClient({ request, response });

  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));

  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  console.log('Data passed as chatContext from Kommunicate is: ' +  JSON.stringify(request.body.originalDetectIntentRequest)  );

   .
   .
   .
   .
   .


});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59854170

复制
相关文章

相似问题

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