带列表的 Bot Framework Formflow 对话框?

Bot Framework Formflow Dialog with list?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using BotVetAlpha3.Core;
using Microsoft.Bot.Builder.FormFlow;

namespace BotVetAlpha3.Dialog
{
public enum SandwichOptions
{
    BLT, BlackForestHam, BuffaloChicken, ChickenAndBaconRanchMelt, ColdCutCombo, MeatballMarinara,
    OvenRoastedChicken, RoastBeef, RotisserieStyleChicken, SpicyItalian, SteakAndCheese, SweetOnionTeriyaki, Tuna,
    TurkeyBreast, Veggie
};
public enum LengthOptions { SixInch, FootLong };
public enum BreadOptions { NineGrainWheat, NineGrainHoneyOat, Italian, ItalianHerbsAndCheese, Flatbread };
public enum CheeseOptions { American, MontereyCheddar, Pepperjack };
public enum ToppingOptions
{
    Avocado, BananaPeppers, Cucumbers, GreenBellPeppers, Jalapenos,
    Lettuce, Olives, Pickles, RedOnion, Spinach, Tomatoes
};
public enum SauceOptions
{
    ChipotleSouthwest, HoneyMustard, LightMayonnaise, RegularMayonnaise,
    Mustard, Oil, Pepper, Ranch, SweetOnion, Vinegar
};




[Serializable]
public class RootDialog
{

    public SandwichOptions? Sandwich;
    public LengthOptions? Length;
    public BreadOptions? Bread;
    public CheeseOptions? Cheese;
    public List<ToppingOptions> Toppings;
    public List<SauceOptions> Sauce;

    public static IForm<RootDialog> BuildForm()
    {
        return new FormBuilder<RootDialog>()
                .Message("Welcome to the simple sandwich order bot!")
                .Build();
    }
};

}

所以这是我当前的 class 来自 MS 的示例,但我想更改它,我一直在尝试这样做,但我无法... 我想做的不是 using enum 来构建我的对话框我想使用 List of strings 。那可能吗 ?如果可以的话,欢迎提供帮助,我一直在用这个撞墙...... 查找有关此主题的信息也很困难。

我想我迟到了,但我有答案了! FormFlow Dialog 不允许您将列表作为字段传递以在与用户的对话中收集:(

允许的类型:

  • Integral – sbyte, byte, short, ushort, int, uint, long, ulong
  • Floating point - float, double
  • String
  • DateTime
  • Enum
  • List of enum

Source

P.S。最近我遇到了同样的问题:我的问题存储在外部数据库中,我想让机器人问他们,但是没有明确的方法:(无论如何,这个问题请求专家帮助!

我要冒昧地假设您也想动态指定那些字符串?好吧,让我们 运行 一起踏上一段旅程。

首先让我们从定义一个表单开始

using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.FormFlow.Advanced;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace pc.Bot.Form
{
    [Serializable]
    public class MyForm
    {
        public MyForm(List<string> Names) { _names = Names; }

        List<string> _names;

       [Template(TemplateUsage.NotUnderstood, "**{0}** isn't a valid selection",  ChoiceStyle = ChoiceStyleOptions.PerLine)]
       [Prompt("**Choose from the following names**:  {||}")]
       public List<string> Names { get; set; }

       public static IForm<MyForm> BuildForm() {
            return new FormBuilder<MyForm>()
             .Field(new FieldReflector<MyForm>(nameof(Names))
                .SetType(null)
                .SetActive(form => form._names != null && form._names.Count > 0)
                .SetDefine(async (form, field) =>
                {
                    form?._names.ForEach(name=> field.AddDescription(name, name).AddTerms(name, name));

                    return await Task.FromResult(true);
                }))
            .Build();
        }
    }
}

现在请注意,我们的表单是可序列化的,并且有一个接受字符串列表的构造函数,然后在我们的 BuildForm 静态函数中,我们添加我们的 Names 字段并动态填充它。

现在让我们看看我们的对话框

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using pc.Bot.Form;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace pc.Bot.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            await Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var names = new List<string>() { "Pawel", "Tomek", "Marin", "Jakub", "Marco" };
            var form = new FormDialog<MyForm>(new MyForm(names), MyForm.BuildForm, FormOptions.PromptInStart);

            context.Call(form, Form_Callback);

            await Task.CompletedTask;
        }

       private async Task Form_Callback(IDialogContext context, IAwaitable<MyForm> result)
       {
           var formData = await result;

           //output our selected names form our form
           await context.PostAsync("You selected the following names:");
           await context.PostAsync(formData.Names?.Aggregate((x, y) => $"{x}, {y}") ?? "No names to select" );
       }
   }
}

现在在我们的对话框中,当我们在表单对话框的构造函数中实例化我们的表单时 class 我们只需传入我们的名称列表。如果您不想从对话框中传递列表,那么您可以在表单的构造函数中设置 _names 字段。

我确定您在最初的问题后继续您的生活,但如果其他人遇到此问题 post,这可能会对他们有所帮助。

请注意,这是我使用 botframework 的第二周,所以如果这是一些可怕的做法或有一些严重的后果,请在我投入生产之前提醒我。