如何使用对话框流程实现从上下文中提取参数以进行列表响应

问题描述 投票:0回答:1

是否可以将所选列表项的值传递给其他任何意图。就我而言,我正在传递一个列表,并且用户从列表中选择了一个项目,现在我想以另一种意图显示此选定的项目名称,但我不能这样做。我的代码有什么问题

代码正常运行,唯一的问题是它返回了“ undefined”,而不是“ selected item name”。

这是我的代码

'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 = $100`,
    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 = $150`,
    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 = $200`,
    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);

假设用户选择电话1并订购2个数量

然后我的回答是“您订购了2个未定义的商品。​​感谢您与我们一起购物。”

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

enter image description here

node.js parameters actions-on-google chatbot dialogflow-fulfillment
1个回答
0
投票

问题是您实际上没有在Context中设置参数,因此在调用Webhook之间没有保留任何值。

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

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

在ADD_TO_CART_INTENT处理程序中,您可以通过如下一行获得此信息:

const selectedPhone = context1.parameters.phone;
© www.soinside.com 2019 - 2024. All rights reserved.