Bot框架 - 没有按钮的imBack或postBack消息?可能吗?

时间:2017-12-07 05:20:22

标签: c# bots botframework

我试图通过一些回复操作向我的Skype,Slack或Messenger用户显示一张卡片(或链接列表)。

Herocard工作正常,它们显示带有文本的按钮,如下例所示:

    IMessageActivity replyMessage = context.MakeMessage();
    replyMessage.Attachments.Add(
    new HeroCard
    {
        Subtitle = "Settings",
        Buttons = new List<CardAction> {
            new CardAction(ActionTypes.ImBack, $"Spell-check {spellcheckst}", value: $"Set Spell-check setting {spellcheckst}", text: $"Spell-check {spellcheckst}", displayText: $"Spell-check {spellcheckst}"),
            new CardAction(ActionTypes.ImBack, $"Small-talk {smalltalkst}", value: $"Set Small-talk setting {smalltalkst}", text: $"Small-talk {smalltalkst}", displayText: $"Small-talk {smalltalkst}")

        }
    }.ToAttachment()
    );
    await context.PostAsync(replyMessage);

但是按钮会占用很多屏幕空间,很多时候它们的标题会被截断。

有没有办法在不显示按钮的情况下触发postBack或imBack卡动作。下面是一个示例场景,当用户请求帮助时,Bot显示命令列表:

  1. 添加网页Try Me
  2. 删除网页Try Me
  3. 显示帮助Try Me
  4. “试用我”链接会像上面的按钮一样将后回到机器人。

    这可能吗?

    由于

2 个答案:

答案 0 :(得分:0)

事实上,大按钮名称会产生问题,而且它看起来像卡片一样陌生。取而代之的是你可以使用Carousel Cards来做到这一点。将以下代码放在对话框中并检查。我正在做它和它的工作。

 public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }
        public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var reply = context.MakeMessage();

            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            reply.Attachments = GetCardsAttachments();

            await context.PostAsync(reply);

            context.Wait(this.MessageReceivedAsync);
        }

        private static IList<Attachment> GetCardsAttachments()
        {
            return new List<Attachment>()
            {
                GetHeroCard(
                    "Add a webpage",
                    "",
                    "",
                    new CardImage(url: "https://cdn.dribbble.com/users/22691/screenshots/1958250/attachments/340010/Button_800x600.gif?sz=328"),
                    new CardAction(ActionTypes.ImBack, "Add a webpage", value: "Add a webpage")),
                GetHeroCard(
                    "delete a webpage",
                    "",
                    "",
                    new CardImage(url: "https://cdn.dribbble.com/users/22691/screenshots/1958250/attachments/340010/Button_800x600.gif?sz=328"),
                    new CardAction(ActionTypes.ImBack, "delete a webpage", value: "delete a webpage")),
                GetHeroCard(
                    "Display help",
                    "",
                    "",
                    new CardImage(url: "https://cdn.dribbble.com/users/22691/screenshots/1958250/attachments/340010/Button_800x600.gif?sz=328"),
                    new CardAction(ActionTypes.ImBack, "Display help", value: "Display help")),
                GetHeroCard(
                    "etc",
                    "",
                    "",
                    new CardImage(url: "https://cdn.dribbble.com/users/22691/screenshots/1958250/attachments/340010/Button_800x600.gif?sz=328"),
                    new CardAction(ActionTypes.ImBack, "etc", value: "etc")),

            };
        }

        private static Attachment GetHeroCard(string title, string subtitle, string text, CardImage cardImage, CardAction cardAction)
        {
            var heroCard = new HeroCard
            {
                Title = title,
                Subtitle = subtitle,
                Text = text,
                Images = new List<CardImage>() { cardImage },
                Buttons = new List<CardAction>() { cardAction },
            };

            return heroCard.ToAttachment();
        }

我希望这能帮助你解决问题。

以下是scren shot供您参考。 enter image description here

答案 1 :(得分:0)

使用这些按钮,您可以将标题设置为显示文本,并将值设置为您需要回发的内容。然后你可以在按钮上方放一小段文字,沿着这些线条说明,&#34;点击任何一个按钮试试&#34;。

我在信使中使用这种方法,使我的按钮文字简短,但是&#34;有效载荷&#34; (或值)按钮设置为更丰富的描述。接下来的是,该值不会显示给用户,但是消息活动的通道数据部分中的框架会接收该值。