Herocard 在 Skype Microsoft 机器人框架中不显示超过 3 个按钮
Herocard not displaying more than 3 buttons in skype Microsoft bot framework
我在英雄卡片中添加了 6 个按钮并尝试显示它们。它在 Teams 和模拟器中运行良好,但在 Skype 上不起作用。它只显示 3 个按钮。
private List<CardAction> GetCardButton(List<string> opts)
{
List<CardAction> cardButtons = new List<CardAction>();
int i = 1;
foreach(string opt in opts)
{
CardAction plButton = new CardAction()
{
Title = opt,
Type = "postBack",
Text = i.ToString(),
Value = i.ToString()
};
cardButtons.Add(plButton);
i++;
}
return cardButtons;
}
//passing list of strings.
List<CardAction> cardButtons = GetCardButton(cardOpts);
HeroCard plCard = new HeroCard()
{
Title = "Try here",
Text = "with:",
Buttons = cardButtons
};
plAttachment = plCard.ToAttachment();
但在 Skype 中我只看到前 3 个按钮。有什么方法可以使卡片可滚动或减小按钮大小吗?
在 Skype 的卡片中可以显示的按钮数量有限制。该限制取决于包含多少其他文本和媒体元素。
根据文档(参见 https://dev.skype.com/bots/ 中的第 6 部分)
如上一个答案所示,每个频道在具体可以显示的内容、按钮数量、文本长度等方面都有限制。看来您 运行 受此限制。您可以做的一件事是,如果有三个以上的按钮,则显示另一张卡片并将它们呈现在列表或轮播中。
这里是一个黑客组合代码示例:
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Bot_Application13.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
List<Attachment> cards = new List<Attachment>();
List<CardAction> buttons = new List<CardAction>();
for (int i = 0; i < 10; i++)
{
CardAction ca = new CardAction()
{
Title = i.ToString(),
Type = "postBack",
Text = i.ToString(),
Value = i.ToString()
};
buttons.Add(ca);
}
var reply = context.MakeMessage();
GetCardsAttachments(buttons, cards);
//reply.AttachmentLayout = AttachmentLayoutTypes.List;
//or
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = cards;
await context.PostAsync(reply);
context.Wait(this.MessageReceivedAsync);
}
private Attachment GetHeroCard(List<CardAction> buttons)
{
var heroCard = new HeroCard();
//heroCard.Title = "Title";
heroCard.Buttons = buttons;
return heroCard.ToAttachment();
}
private void GetCardsAttachments(List<CardAction> buttons, List<Attachment> cards)
{
if (buttons.Count <= 3)
{
cards.Add(GetHeroCard(buttons));
}
else
{
var temp = new List<CardAction>();
for (int i = 0; i < buttons.Count; i++)
{
if (i % 3 != 0)
{
temp.Add(buttons.ElementAt(i));
}
else
{
if (i != 0)
{
cards.Add(GetHeroCard(temp));
}
temp = new List<CardAction>();
temp.Add(buttons.ElementAt(i));
}
}
cards.Add(GetHeroCard(temp));
}
}
}
}
我在英雄卡片中添加了 6 个按钮并尝试显示它们。它在 Teams 和模拟器中运行良好,但在 Skype 上不起作用。它只显示 3 个按钮。
private List<CardAction> GetCardButton(List<string> opts)
{
List<CardAction> cardButtons = new List<CardAction>();
int i = 1;
foreach(string opt in opts)
{
CardAction plButton = new CardAction()
{
Title = opt,
Type = "postBack",
Text = i.ToString(),
Value = i.ToString()
};
cardButtons.Add(plButton);
i++;
}
return cardButtons;
}
//passing list of strings.
List<CardAction> cardButtons = GetCardButton(cardOpts);
HeroCard plCard = new HeroCard()
{
Title = "Try here",
Text = "with:",
Buttons = cardButtons
};
plAttachment = plCard.ToAttachment();
但在 Skype 中我只看到前 3 个按钮。有什么方法可以使卡片可滚动或减小按钮大小吗?
在 Skype 的卡片中可以显示的按钮数量有限制。该限制取决于包含多少其他文本和媒体元素。
根据文档(参见 https://dev.skype.com/bots/ 中的第 6 部分)
如上一个答案所示,每个频道在具体可以显示的内容、按钮数量、文本长度等方面都有限制。看来您 运行 受此限制。您可以做的一件事是,如果有三个以上的按钮,则显示另一张卡片并将它们呈现在列表或轮播中。
这里是一个黑客组合代码示例:
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Bot_Application13.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
List<Attachment> cards = new List<Attachment>();
List<CardAction> buttons = new List<CardAction>();
for (int i = 0; i < 10; i++)
{
CardAction ca = new CardAction()
{
Title = i.ToString(),
Type = "postBack",
Text = i.ToString(),
Value = i.ToString()
};
buttons.Add(ca);
}
var reply = context.MakeMessage();
GetCardsAttachments(buttons, cards);
//reply.AttachmentLayout = AttachmentLayoutTypes.List;
//or
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = cards;
await context.PostAsync(reply);
context.Wait(this.MessageReceivedAsync);
}
private Attachment GetHeroCard(List<CardAction> buttons)
{
var heroCard = new HeroCard();
//heroCard.Title = "Title";
heroCard.Buttons = buttons;
return heroCard.ToAttachment();
}
private void GetCardsAttachments(List<CardAction> buttons, List<Attachment> cards)
{
if (buttons.Count <= 3)
{
cards.Add(GetHeroCard(buttons));
}
else
{
var temp = new List<CardAction>();
for (int i = 0; i < buttons.Count; i++)
{
if (i % 3 != 0)
{
temp.Add(buttons.ElementAt(i));
}
else
{
if (i != 0)
{
cards.Add(GetHeroCard(temp));
}
temp = new List<CardAction>();
temp.Add(buttons.ElementAt(i));
}
}
cards.Add(GetHeroCard(temp));
}
}
}
}