首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为ChoicePrompt Bot框架V4 c#添加多个按钮

如何为ChoicePrompt Bot框架V4 c#添加多个按钮
EN

Stack Overflow用户
提问于 2019-04-24 13:03:45
回答 2查看 2.4K关注 0票数 1

我正在使用ChoicePrompt in WaterfallStep来显示选项。问题是,如果选择列表大于10,我不会得到按钮,即显示为文本。请帮助我解决这个错误。

代码语言:javascript
复制
 var waterfallSteps = new WaterfallStep[]
        {
            InitializeStateStepAsync,
            PromptShowChoiceStepAsync,
            DisplaySuccessStepAsync,
        };
        AddDialog(new WaterfallDialog("waterfallDialog", waterfallSteps));
        AddDialog(new ChoicePrompt("ShowChoicePrompt", ValidateShowChoice));

 private async Task<DialogTurnResult> ValidateShowChoice(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
   return await stepContext.PromptAsync("ShowChoicePrompt", new PromptOptions
            {
                Prompt = MessageFactory.Text("Please select from choices"),
                RetryPrompt = MessageFactory.Text("Sorry, Please the valid choice"),
                Choices = ChoiceFactory.ToChoices(choicesList),
            }, cancellationToken);
        }
    }

如果choicesList计数大于10,我将得到这样的按钮。这在Bot框架V3中不是一个问题。

代码语言:javascript
复制
Please select from choices
1. Choice-1
2. Choice-2
3. Choice-3
4. Choice-4
5. Choice-5
6. Choice-6
7. Choice-7
8. Choice-8
9. Choice-9
10. Choice-10
11. Choice-11
12. Choice-12
13. Choice-13

请帮助我解决这个错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-14 22:33:29

默认情况下,v3中的选择提示使用英雄卡。您可以强制提示在HeroCard枚举中使用带有新的ListStyle选项的英雄卡。当将列表样式添加到对话框集时,可以直接应用于提示符:

代码语言:javascript
复制
AddDialog(new ChoicePrompt("ShowChoicePrompt", ValidateShowChoice) { Style = ListStyle.HeroCard });

现在还可以在提示选项中指定列表样式:

代码语言:javascript
复制
return await stepContext.PromptAsync("ShowChoicePrompt", new PromptOptions
{
    Prompt = MessageFactory.Text("Please select from choices"),
    RetryPrompt = MessageFactory.Text("Sorry, Please the valid choice"),
    Choices = ChoiceFactory.ToChoices(choicesList),
    Style = ListStyle.HeroCard,
}, cancellationToken);

的确,在一条消息中包含太多的按钮是错误的做法,而且大多数频道都通过不允许在一张卡上设置很多按钮来强制执行这一约定,但是如果你试图在一张卡上放置太多的按钮,Facebook和Skype的连接器将自动生成多张卡。

在中,它将如下所示:

在Skype中,它看起来会是这样的:

票数 2
EN

Stack Overflow用户

发布于 2019-04-25 09:13:11

这些显示选择是由于对所使用的频道的限制/指南而作出的。

如果您查看一下关于Quick replies 这里的Facebook开发人员页面,它会说:

快速回复提供了一种方式来呈现一组最多11个按钮,在会话中包含一个标题和可选的图像,并显着地出现在作曲家的上方。您还可以使用快速回复来请求一个人的位置、电子邮件地址和电话号码。

因此,在Github上可用的BotBuilder代码中,您将有一个Determine if a number of Suggested Actions are supported by a Channel 这里的方法。

代码语言:javascript
复制
/// <summary>
/// Determine if a number of Suggested Actions are supported by a Channel.
/// </summary>
/// <param name="channelId">The Channel to check the if Suggested Actions are supported in.</param>
/// <param name="buttonCnt">(Optional) The number of Suggested Actions to check for the Channel.</param>
/// <returns>True if the Channel supports the buttonCnt total Suggested Actions, False if the Channel does not support that number of Suggested Actions.</returns>
public static bool SupportsSuggestedActions(string channelId, int buttonCnt = 100)
{
    switch (channelId)
    {
        // https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
        case Connector.Channels.Facebook:
        case Connector.Channels.Skype:
            return buttonCnt <= 10;

        // ...
    }
}

然后,在您使用的ChoiceFactory中,选择显示(请参阅代码这里):

代码语言:javascript
复制
public static IMessageActivity ForChannel(string channelId, IList<Choice> list, string text = null, string speak = null, ChoiceFactoryOptions options = null)
{
    channelId = channelId ?? string.Empty;

    list = list ?? new List<Choice>();

    // Find maximum title length
    var maxTitleLength = 0;
    foreach (var choice in list)
    {
        var l = choice.Action != null && !string.IsNullOrEmpty(choice.Action.Title) ? choice.Action.Title.Length : choice.Value.Length;
        if (l > maxTitleLength)
        {
            maxTitleLength = l;
        }
    }

    // Determine list style
    var supportsSuggestedActions = Channel.SupportsSuggestedActions(channelId, list.Count);
    var supportsCardActions = Channel.SupportsCardActions(channelId, list.Count);
    var maxActionTitleLength = Channel.MaxActionTitleLength(channelId);
    var hasMessageFeed = Channel.HasMessageFeed(channelId);
    var longTitles = maxTitleLength > maxActionTitleLength;

    if (!longTitles && !supportsSuggestedActions && supportsCardActions)
    {
        // SuggestedActions is the preferred approach, but for channels that don't
        // support them (e.g. Teams, Cortana) we should use a HeroCard with CardActions
        return HeroCard(list, text, speak);
    }
    else if (!longTitles && supportsSuggestedActions)
    {
        // We always prefer showing choices using suggested actions. If the titles are too long, however,
        // we'll have to show them as a text list.
        return SuggestedAction(list, text, speak);
    }
    else if (!longTitles && list.Count <= 3)
    {
        // If the titles are short and there are 3 or less choices we'll use an inline list.
        return Inline(list, text, speak, options);
    }
    else
    {
        // Show a numbered list.
        return List(list, text, speak, options);
    }
}

这就是为什么如果你提供了10多个项目,你就会得到这个列表。

一般来说,限制按钮的数量是最好的做法,超过10个按钮是很大的。您可以调整您的行为(例如,按组分组/添加额外的选择级别)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55830723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档