将单击的按钮重定向到特定页面

时间:2018-06-05 09:56:56

标签: c# botframework

public virtual async Task ShowAnnuvalConferenceTicket(IDialogContext context, IAwaitable<IMessageActivity> activity)
        {
            var message = await activity;

            PromptDialog.Choice(
                context: context,
                resume:ChoiceReceivedAsync ,
                options: (IEnumerable<ProductGroups>)Enum.GetValues(typeof(ProductGroups)),
                prompt: " ",
                promptStyle: PromptStyle.Auto
                );
        }
        public virtual async Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<ProductGroups> activity)
        {
            ProductGroups response = await activity;
            context.Call<object>(new AnnualPlanDialog(response.ToString()), ChildDialogComplete);

        }
        public virtual async Task ChildDialogComplete(IDialogContext context, IAwaitable<object> response)
        {
            var respond = await response as Activity;
            if (respond = ProductGroups.MoreCategories)
            {
                await context.PostAsync("Click here to view more categories");
                context.Done(this);
            }

        }

public enum ProductGroups {
            Toys,
            Electronics,
            Phones,
            Health,
            Beauty,
            Computers,
            Cameras,
            Jewelery,
            Watches,
            Weddings,
            MoreCategories
        }

美好的一天,请在这些方面需要帮助,我一直在尝试获取从枚举常量生成的选项列表,每个选项都将我重定向到不同的页面,但知道如何去做。有什么帮助吗?

1 个答案:

答案 0 :(得分:6)

如果您想打开网址,则可以只使用按钮Hero Cards。您可以使用标题和链接创建字典,也可以使用枚举。这是我解决的问题:

而不是发送提示

var reply = context.MakeMessage();
List<CardAction> enumOptions = new List<CardAction>();
foreach(ProductGroups option in Enum.GetValues(typeof(ProductGroups)))
{
    enumOptions.Add(new CardAction
    {
        Title = option.ToString(),
        Type = option.ToString().Equals("MoreCategories") ? ActionTypes.ImBack : ActionTypes.OpenUrl,
        Value = option.GetType()
                .GetMember(option.ToString())
                .First()
                .GetCustomAttribute<DescribeAttribute>()
                .Description
    });


}

reply.Attachments.Add(GetHeroCard(null, null, null, null, enumOptions));
await context.PostAsync(reply);

CardAction中的Type Attribute将决定单击按钮时要执行的操作。因此,如果用户点击了除MoreCategories以外的任何按钮,则会打开一个链接,如果用户点击了MoreCategories,则机器人将获得更多类别的消息,之后您可以进行处理。

GetHeroCard只是一个基本功能,您可以调用它来创建任何英雄卡:

    public static Attachment GetHeroCard(string title, string subTitle, string text, List<CardImage> images, List<CardAction> action)
    {

        HeroCard card = new HeroCard();
        if (!String.IsNullOrEmpty(title))
            card.Title = title;
        if (!String.IsNullOrEmpty(subTitle))
            card.Subtitle = subTitle;
        if (!String.IsNullOrEmpty(text))
            card.Text = text;
        if (images != null)
            card.Images = images;
        if (action != null)
            card.Buttons =  action ;
        return card.ToAttachment();
    }

您可以像这样更新您的枚举:

public enum ProductGroups
{
    [Describe("https://stackoverflow.com/")]
    Toys,
    [Describe("https://stackoverflow.com/")]
    Electronics,
    [Describe("https://stackoverflow.com/")]
    Phones,
    [Describe("https://stackoverflow.com/")]
    Health,
    [Describe("https://stackoverflow.com/")]
    Beauty,
    [Describe("https://stackoverflow.com/")]
    Computers,
    [Describe("https://stackoverflow.com/")]
    Cameras,
    [Describe("https://stackoverflow.com/")]
    Jewelery,
    [Describe("https://stackoverflow.com/")]
    Watches,
    [Describe("https://stackoverflow.com/")]
    Weddings,
    [Describe("More categories")]
    MoreCategories
}

<强>输出

Output