当我尝试使用Google Assistant时,将上下文从一种意图传递到另一种意图时,获取“空”上下文

时间:2019-05-08 11:39:45

标签: javascript node.js dialogflow dialogflow-fulfillment

我正在尝试使用DialogFlow学习Google Assistant集成,我编写了以下代码,当我在dialogFlow中进行测试时,它的工作原理类似于魅力,但是当我在Google Assistant上对其进行测试时,它却失败了(即,从Intent getBillingInfo传递的上下文在访问时变为null在payBill意图中)。请帮助我了解我要去哪里。

代码:

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

1 个答案:

答案 0 :(得分:0)

您似乎没有在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});

您还可以在Google代码实验室上试用“操作”(level 1level 2)或查看the Actions on Google github repos