javascript - 当我尝试使用 Google Assistant 时,将上下文从一个意图传递到另一个意图时获取 "null"上下文

标签 javascript node.js dialogflow-es dialogflow-es-fulfillment

我正在尝试使用DialogFlow学习Google Assistant集成,我编写了以下代码,当我在dialogFlow中测试时,它的工作原理就像charm一样,但当我在Google Assistant上测试相同的代码时,它会失败(即,在payBill意图中访问时,从Intent getBillingInfo传递的上下文变为空)。请帮助我理解我哪里出了问题。

代码:

var https = require ('https');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

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

      // // below to get this function to be run when a Dialogflow intent is matched
  function getBillingInfoHandler(agent) {
    const parameters = request.body.queryResult.parameters;
    var phoneNumber = parameters['phone-number'];
    console.log("Phone Number: "+ phoneNumber);
    let url = "https://testapi.io/api/shwej//getBillingInfo";
    return new Promise((resolve, reject) => {       
    https.get(url, (res) => {
            let body = ''; // var to store the response chunks
            res.on('data', (chunk) => {body += chunk; });
            res.on('end', () => {
                // After all the data has been received, parse the JSON for desired data
                let response = JSON.parse(body);
                let output = response.billing_amount;
                // Resolve the promise with the output text
                console.log(body);
                agent.add("Your bill for " + phoneNumber + " is " + output + " ₹ ");
                agent.add(new Suggestion(`Click to Pay`));

                //agent.setContext('billing_context');
                //const context = {'phoneNumber': phoneNumber, 'billAmount': output};
                //agent.setContext(context);
                agent.setContext({
                'name':'billing-context',
                'lifespan': 50,
                'parameters':{
                    'phoneNumber': phoneNumber, 
                    'billAmount': output
                }
                });
                resolve();
            });
            res.on('error', (error) => {
                agent.add("Error occurred while calling API.");
                console.log(`Error calling the API: ${error}`);
                reject();
            });
    });
    });

  }

 function payBillHandler(agent) {
     let billingContext = agent.getContext('billing-context');

     if (typeof billingContext === 'undefined' || billingContext === null){
         agent.add("Some error with passing context!!!");
     }else{
        agent.add("Your payment is successful! ");
        agent.add(" Phone Number : " + billingContext.parameters.phoneNumber);
        agent.add(" Amount paid : " + billingContext.parameters.billAmount + " ₹ ");
     }
   }

  // 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('getBillingInfo', getBillingInfoHandler);
  intentMap.set('payBill', payBillHandler);
  agent.handleRequest(intentMap);
});

最佳答案

您似乎没有导入 Actions on Google 客户端库。尝试将您的执行的前几行替换为:

// Import the Dialogflow module and response creation dependencies
// from the Actions on Google client library.

const {
  dialogflow,
  BasicCard,
  Permission,
  Suggestions,
} = require('actions-on-google');

// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

您还可以尝试 Actions on Google Codelab( level 1level 2 )或查看 the Actions on Google github repos .

关于javascript - 当我尝试使用 Google Assistant 时,将上下文从一个意图传递到另一个意图时获取 "null"上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039972/

相关文章:

Node.JS HTML 到 PDF

node.js - 是否可以等到ask方法返回响应

testing - 如何更改 Action 部分的合成语音?

javascript - <pattern> & required 不适用于动态创建的表单

javascript - 从数组javascript中删除

node.js - 如何从路由器访问以前的URL参数

javascript - NodeJs/Google Firebase 函数字符串方法不起作用

javascript - 使用对象文字更新标记上的图标

javascript - 如何获取firebase文件存储的下载Url

javascript - 如何从 Node 中的 module.exports 链调用函数