如何为ChoicePrompt Bot Framework V4 C#添加10个以上的按钮

时间:2019-04-24 13:03:45

标签: c# botframework chatbot

我正在 WaterfallStep 中使用 ChoicePrompt 来显示选择。问题是,如果选择列表大于10,我没有得到按钮,则显示为文本。请帮助我解决此错误。

 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 Framework V3中这不是问题。

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

请帮助我如何解决此错误。

2 个答案:

答案 0 :(得分:1)

这些显示选择是由于所用通道的限制/准则而做出的。

如果您查看有关Quick replies here的Facebook Messenger开发人员页面,则该页面显示:

  

快速答复提供了一种显示一组最多11个按钮的方法   对话,其中包含标题和可选图片,并显示   在作曲家之上您也可以使用快速回复   请求一个人的位置,电子邮件地址和电话号码。

因此,在Github上可用的BotBuilder的代码中,您将拥有一种Determine if a number of Suggested Actions are supported by a Channel here的方法:

/// <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中,选择显示(请参见代码here):

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 :(得分:0)

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

HeroCard

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

AddDialog(new ChoicePrompt("ShowChoicePrompt", ValidateShowChoice) { Style = ListStyle.HeroCard });

虽然在消息中包含太多按钮是不正确的做法,并且大多数渠道通过不允许卡片上有很多按钮来强制执行此约定,但如果您尝试将Facebook和Skype的连接器自动生成多张卡片,在一张卡上放置太多按钮。

在Facebook Messenger中,它将如下所示:

Facebook Messenger

在Skype中,它看起来像这样:

Skype