[英]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();
}
};
}
所以這是我從MS的示例中獲取的當前類,但是我想更改它,我一直在嘗試做,但是我無法執行...我想做的是,而不是使用枚舉來構建我要使用的對話框字符串列表 。 那可能嗎 ? 如果可以提供所有幫助,我都會在牆上碰到這個...查找有關此主題的信息也非常困難。
我要走出去,假設您還想動態指定那些字符串? 好吧,讓我們一起走一段小路。
首先讓我們開始定義一個表單
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靜態函數中添加“名稱”字段並動態填充它。
現在讓我們來看看我們的對話框
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" );
}
}
}
現在,在對話框中,當我們在窗體對話框類的構造函數中實例化窗體時,我們只需傳遞名稱列表即可。 如果您不想從對話框中傳遞列表,則可以在表單的構造函數中設置_names字段。
我敢肯定,自從最初提出問題以來,您已經過了自己的生活,但是如果其他人遇到了這篇文章,這可能會對他們有所幫助。
請注意,這是我第二次使用botframework,因此,如果這是一種可怕的做法或造成嚴重后果,請在生產之前給我一些提示。
我想我遲到了,但是我有答案了! FormFlow對話框不允許您將“列表作為”字段傳遞給與用戶:(
允許的類型:
- 整數– sbyte,byte,short,ushort,int,uint,long,ulong
- 浮點-浮點,雙
- 串
- 約會時間
- 枚舉
- 枚舉列表
PS最近,我遇到了同樣的問題:我的問題存儲在外部數據庫中,我想讓機器人問這些問題,但是沒有明確的方法來解決:(無論如何,需要專家幫助解決這個問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.