You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bot Framework中为Datetime Prompt添加额外建议选项的解决方案咨询

How to Add Custom Suggested Actions to a Datetime Prompt in Bot Framework

Got it, let's break down why your current code isn't working and fix it. The key issue is two-fold:

  1. The built-in DATETIME_PROMPT doesn't have a choices property you can set in its prompt options.
  2. Sending suggested actions as a separate message before the prompt means those actions aren't linked to the Datetime Prompt's input flow—when a user clicks "Today" or "Tomorrow", that response is treated as a standalone message instead of input to the waiting prompt.

Solution 1: Embed Suggested Actions Directly in the Datetime Prompt's Activity

The prompt property in PromptOptions accepts not just a string, but a full Activity object. This lets you attach suggested actions directly to the prompt message, so clicks on the actions are routed to the Datetime Prompt for parsing.

Here's how to modify your code:

async flightBookingDate(step) {
  if (destinationProvidedForFlightBooking) {
    step.values.destination = "Delhi";
  } else {
    step.values.destination = step.result;
  }

  if (!step.values.flightBookingDate) {
    // Create a prompt activity with embedded suggested actions
    const promptActivity = MessageFactory.text(
      "For when you want to book your flight?",
      undefined,
      {
        suggestedActions: {
          actions: [
            // Use imBack to send a machine-readable date string
            {
              type: "imBack",
              title: "Today",
              value: new Date().toISOString().split('T')[0] // YYYY-MM-DD format
            },
            {
              type: "imBack",
              title: "Tomorrow",
              value: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString().split('T')[0]
            }
          ]
        }
      }
    );

    const prompData = {
      prompt: promptActivity,
      retryPrompt: "No flights available for that date. Please enter a different date or select an option."
    };

    return await step.prompt(DATETIME_PROMPT, prompData);
  } else {
    return await step.continueDialog();
  }
}

Why this works:

  • The suggested actions are part of the same message as the Datetime Prompt, so user clicks are sent as input to the prompt.
  • We send YYYY-MM-DD formatted strings (which the Datetime Prompt automatically recognizes) as the value of each action, so the prompt can parse them correctly without extra work.

Solution 2: Use a Text Prompt with Custom Date Parsing (For More Flexibility)

If you need more control over how date inputs are handled (e.g., supporting natural language like "next week"), you can use a TEXT_PROMPT instead, add suggested actions to it, then parse and validate the input manually in the next step.

Example:

// First, prompt with suggested actions using TEXT_PROMPT
async flightBookingDate(step) {
  if (destinationProvidedForFlightBooking) {
    step.values.destination = "Delhi";
  } else {
    step.values.destination = step.result;
  }

  if (!step.values.flightBookingDate) {
    const promptActivity = MessageFactory.text(
      "For when you want to book your flight?",
      undefined,
      {
        suggestedActions: {
          actions: [
            { type: "imBack", title: "Today", value: "Today" },
            { type: "imBack", title: "Tomorrow", value: "Tomorrow" }
          ]
        }
      }
    );

    return await step.prompt(TEXT_PROMPT, {
      prompt: promptActivity,
      retryPrompt: "Please enter a valid date (e.g., 2024-10-05) or select an option."
    });
  } else {
    return await step.continueDialog();
  }
}

// Next step: parse and validate the input
async validateFlightDate(step) {
  let selectedDate;
  const userInput = step.result.toLowerCase();

  // Handle suggested action values
  if (userInput === "today") {
    selectedDate = new Date();
  } else if (userInput === "tomorrow") {
    selectedDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
  } else {
    // Parse natural language or formatted dates (use a library like date-fns if needed)
    selectedDate = new Date(userInput);
  }

  // Validate the date
  if (isNaN(selectedDate.getTime())) {
    return await step.repromptDialog();
  }

  // Store the valid date and proceed
  step.values.flightBookingDate = selectedDate.toISOString().split('T')[0];
  return await step.next();
}

When to use this:

  • You need to support non-standard date inputs that the Datetime Prompt doesn't recognize.
  • You want to add custom validation logic (e.g., blocking past dates).

内容的提问来源于stack exchange,提问作者Raman Jha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:48:10