botbuilder v 4,带有下拉菜单的动态自适应卡,并在提示时捕获值 [英] botbuilder v 4, dynamic adaptive card with dropdown and capturing values on prompt

查看:54
本文介绍了botbuilder v 4,带有下拉菜单的动态自适应卡,并在提示时捕获值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ms botbuilder v 4 我正在使用webcontrol,webchat.js,最新,反应 案例非常琐碎: 我想在下拉列表中显示可能的值列表,值将是动态的(来自API,我在那里需要标题和值(Ids).然后,当用户选择某些项目并单击确定"时,我想获取值(Id)并进一步工作接着就,随即. 就目前而言,显示下拉列表的唯一方法是使用自适应卡,在v3中,有一个在提示中使用自适应卡的选项,并且还计划用于下一个版本: https://github.com/Microsoft/botbuilder-dotnet/issues/614,仅使用字符串列表,一切正常,但如果我要存储键值对(用于ID),则无法执行该操作.PromptOptions中的cos Choices仅接受字符串列表(将在下面显示).因此,我现在使用的唯一解决方法是存储整个值集合,并在获得结果后查找其ID.有更方便的解决方案吗? 这是代码:

I'm using ms botbuilder v 4 I'm using webcontrol, webchat.js, latest, react Case is pretty trivial: I want to show list of possible values in dropdown, values will be dynamic (comes from API, i need Titles and Values (Ids) there. Then when user selects some item and clicks OK i want to get value (Id) and work further with that. As i got it for now only way to show dropdown is using adaptive cards, in v3 there was an option to use adaptive cards in prompts and it also planned for next version: https://github.com/Microsoft/botbuilder-dotnet/issues/1170 But for now only woraround for that is exaplained here: https://github.com/Microsoft/botbuilder-dotnet/issues/614 , with just list of string everything's working fine, but if i want to store keyvalue pairs (for IDs) i'm not able to do that cos Choices in PromptOptions only accepts list of string (will show below). So only workaround i'm using now is to store whole collection of values and after getting the result go and find it's id. Is there more convinient solution for that? Here's the code:

var choicesInputs = _teams.Select(s => new AdaptiveChoice { Title = s.Value, Value = s.Value}).ToList();

var card = new AdaptiveCard
{
    Version = new AdaptiveSchemaVersion(1, 0),
    Body =
    {
        new AdaptiveTextBlock("Select a team to assign your ticket"),
        new AdaptiveChoiceSetInput
        {
            Choices = choicesInputs,
            Id = "setId",
            Style = AdaptiveChoiceInputStyle.Compact,
            IsMultiSelect = false
        }
    },
    Actions = new List<AdaptiveAction>
    {
        new AdaptiveSubmitAction
        {
            Title = "Ok",
            Type = "Action.Submit"
        }
    }
};

signInPhoneState.Teams = _teams;

return await stepcontext.PromptAsync(
    "SelectGroupCardDialog",
    new PromptOptions
    {
            Choices = ChoiceFactory.ToChoices(_teams.Select(pair => pair.Value).ToList()),
        Prompt = (Activity) MessageFactory.Attachment(new Attachment
        {
            ContentType = AdaptiveCard.ContentType,
            Content = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(card))
        })
    },
    cancellationtoken);

// . . .

var selectedTeamId = signInPhoneState.Teams.FirstOrDefault(pair => pair.Value == sel).Key;

快速附带问题(但与我正在使用的解决方法有关): 通过对话框持久保存某些变量的最简单方法是什么?如果我正确地记得在v3中,这就像将一个值标记为public并将dialog标记为可序列化那样简单,就是这样,现在,我需要为每个对话框创建特殊的访问器,在其中复制属性并管理其状态,这是正确的吗? 谢谢

Quick side question (but related in terms i'm using it for workaround): What is the easiest way to persist some variable though dialog? If i remember correectly In v3 it was as simple as marking a value as public and marking dialog as serializable and that's it, now as i get it you need to create special accessor for each dialog, dublicate property there and manage the state of it, is it correct? Thanks

推荐答案

您有一个字典,其中以团队ID为键,而以团队名称为值.您将团队名称用作提示中使用的自适应选择集的值,然后在提示之后依次使用团队名称从字典中提取团队ID.您想要一个更方便的选择.

You have a dictionary with team ID's as keys and team names as values. You are using the team names as the values for an adaptive choice set that's being used in a prompt, and in the turn after the prompt you're extracting the team ID from the dictionary using the team name. You want a more convenient option.

访问字典中的数据时,使用键访问值比使用其他方法更有效.毕竟,这就是字典的目的.因此,您可以使用团队ID,而不是使用团队名称作为选择集中的值.

When accessing the data in a dictionary, it is more efficient to access a value using a key than the other way around. That is what dictionaries are for, after all. So instead of using the team names as values in your choice set, you could use team ID's.

var choicesInputs = _teams.Select(s => new AdaptiveChoice { Title = s.Value, Value = s.Key }).ToList();

// . . .

signInPhoneState.Teams.TryGetValue(sel, out string selectedTeamName);

这意味着,如果词典是从某些可能发生更改的外部来源中提取的,则团队名称将尽可能保持最新.

This would mean that if the dictionary is being drawn from some external source that's subject to change, the team name would be as up-to-date as possible.

您可以在选择的值中存储团队ID和团队名称.

You could store both the team ID and the team name in the choice's value.

var choicesInputs = _teams.Select(s => new AdaptiveChoice { Title = s.Value, Value = JsonConvert.SerializeObject(s) }).ToList();

// . . .

var pair = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(sel);
var selectedTeamId = pair.Key;
var selectedTeamName = pair.Value;

这意味着,如果基础数据在提示的第一回合和第二回合之间发生变化,则该选择仍然有效.

This would mean if the underlying data changes between the first turn of the prompt and the second, the choice would still be valid.

这篇关于botbuilder v 4,带有下拉菜单的动态自适应卡,并在提示时捕获值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆