如何使用 dialogflow fulfillment 从列表响应的上下文中提取参数

how to extract parameters from the context for list response using dialogflow fulfillment

是否可以将 selected 列表项的值传递给任何其他意图。在我的例子中,我传递了一个列表和用户 select 列表中的一个项目,现在我想在另一个意图中显示这个 selected 项目名称,但我不能那样做。 我的代码有什么问题

代码运行正常唯一的问题是 "selected item name" 它 returns "undefined".

这是我的代码

'use strict';

const functions = require('firebase-functions');
const {dialogflow, SimpleResponse} = require ('actions-on-google');
const {Suggestions, List, Image, BasicCard} = require ('actions-on-google');

const SHOW_PHONE_INTENT = 'Default Welcome Intent';
const FALLBACK_INTENT = 'Default Fallback Intent';
const SELECTED_PHONE_INTENT = 'SelectedPhoneIntent';
const ADD_TO_CART_INTENT = 'AddToCartIntent';

const AppContexts = {AWAITING_PHONE: 'awaiting-phone'};
const AppContexts1 = {AWAITING_REPLY: 'awaiting-reply'};

const app = dialogflow();

const PhoneDetail = {
  'Phone1': {
    text: `screen size = 5 inches  \n
    price = 0`,
    subtitle: 'This is phone1',
    title: 'Phone1 Details',
    image: new Image({
      url: 'https://img.icons8.com/plasticine/2x/name.png',
      alt: 'pic1',
    }),
    display: 'WHITE',
  };

  'Phone2': {
    text: `screen size = 5.5 inches  \n
    price = 0`,
    subtitle: 'This is phone2',
    title: 'Phone2 Details',
    image: new Image({
      url: 'https://img.icons8.com/plasticine/2x/name.png',
      alt: 'pic2',
    }),
    display: 'WHITE',
  };

  'Phone3': {
    text: `screen size = 6 inches  \n
    price = 0`,
    subtitle: 'This is phone3',
    title: 'Phone3 Details',
    image: new Image({
      url: 'https://img.icons8.com/plasticine/2x/name.png',
      alt: 'pic3',
    }),
    display: 'WHITE',
  };  
};

app.intent(FALLBACK_INTENT, (conv) => {
    conv.ask("Sorry! Could you please repeat that?");
});

app.intent(SHOW_PHONE_INTENT, (conv) => {
  conv.contexts.set(AppContexts.AWAITING_PHONE, 1);
  conv.ask("Here's the list of phone's.");
  conv.ask(new List({
    title: "Select a phone to see details.",
    items: {
      "Phone1": {
        title: "phone1",
        description: "Click here to check phone1 details.",
        image: new Image({
          url: 'https://img.icons8.com/plasticine/2x/name.png',
          alt: 'p1',
        }),
      },
      "Phone2": {
        title: "phone2",
        description: "Click here to check phone2 details.",
        image: new Image({
          url: 'https://img.icons8.com/plasticine/2x/name.png',
          alt: 'plc',
        }),
      },
      "Phone3": {
        title: "phone3",
        description: "Click here to check phone3 details.",
        image: new Image({
          url: 'https://img.icons8.com/plasticine/2x/name.png',
          alt: 'obj',
        }),
      },
    },
  }));
});

app.intent(SELECTED_PHONE_INTENT, (conv, input, option) => {
    const context = conv.contexts.get(AppContexts.AWAITING_PHONE);
    if (option) {
      conv.ask(`${option} Details`);
      conv.ask(new BasicCard(PhoneDetail[option]));
      conv.ask(new Suggestions(['Show List', 'Add to Cart']));
    } else {
      conv.close('Sorry! there might be some issue, please contact support.');
    }
    conv.contexts.set(AppContexts1.AWAITING_REPLY, 1);
});

app.intent(ADD_TO_CART_INTENT, (conv, parameters) => {
  const context1 = conv.contexts.get(AppContexts1.AWAITING_REPLY);
  const selectedPhone = context1.parameters;
  const qty = context1.parameters.qty;
  if ('Add to Cart'){
    let missingSlots = [];
    if (!qty) { missingSlots.push('qty'); }
    if (missingSlots.length === 1){
      conv.ask(`How many phone's do you need?`);
    } else {
      conv.ask(`You have ordered ${qty} ${selectedPhone}. `);
      conv.close("Thanks for shopping with us.");
    }
  } else {
    conv.close('Sorry! there might be some issue, please contact support.');
  }
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

假设用户 select 的电话 1 和订单 2 数量

然后我的回复是 "You have ordered 2 undefined. Thanks for shopping with us."

需要帮助获取 selected 项目名称而不是未定义的。

这是处理 select 列表中项目的 Intent:

问题是您实际上并没有在上下文中设置参数,因此在对您的 webhook 的调用之间没有保留任何值。

在 SELECTED_PHONE_INTENT 处理程序中,该行应该更像

conv.contexts.set(AppContexts1.AWAITING_REPLY, 5, {
  phone: option
});

在 ADD_TO_CART_INTENT 处理程序中,您将通过

等行获取此信息
const selectedPhone = context1.parameters.phone;