我想在机器人框架下禁用网络聊天中的卡片按钮。例如,在一张卡片我有多个按钮,我想根据一些数据值禁用一些按钮。我怎么能这样做?
答案 0 :(得分:5)
将来我会建议缩小您的要求,使其不那么广泛,并包含与您要实现的目标相关的详细信息。尽管如此,我已经填补了一些空白并做了一些我希望会有所帮助的事情。 :)
我有working example bot执行以下操作:
我在UserData,SendGreeting中保存了一个布尔值,通知机器人是否显示第二个按钮。在控制器中,我有以下代码:
然后将该布尔值保存到UserData.SentGreeting。
// create connector service
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// get state client object
StateClient stateClient = activity.GetStateClient();
// retrieve user data
BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
bool SentGreeting;
if (activity.Text == "hello" || activity.Text == "hi")
{
SentGreeting = true;
} else
{
SentGreeting = false;
}
userData.SetProperty<bool>("SentGreeting", SentGreeting);
// save state
await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
// initiate CardDialog
await Conversation.SendAsync(activity, () => new CardDialog());
在对话框中,有以下代码:
创建秘密按钮,仅在SentGreeting为真时将其添加到按钮列表
// create sentGreeting
bool sentGreeting;
// assign value from UserData.SentGreeting
context.UserData.TryGetValue<bool>("SentGreeting", out sentGreeting);
// create standard button list (for those who do not greet the bot)
var GenericButtonList = new List<CardAction>
{
new CardAction(ActionTypes.OpenUrl, "Bot Framework", value: "https://docs.botframework.com/en-us/"),
};
// create the hero card
var Card = new HeroCard()
{
Title = "Select an Action",
Subtitle = "Choose from available actions",
Text = "If you were polite and said \'hi\' or \'hello\' to this bot, you will see two buttons below.",
Images = new List<CardImage> { new CardImage("https://cloud.githubusercontent.com/assets/14900841/23733811/c618e402-042f-11e7-9b8e-6262d9f2d898.JPG") },
Buttons = GenericButtonList
};
// create secret button that only users who said hi can see
CardAction secretButton = new CardAction();
secretButton.Title = "Very Polite";
secretButton.Type = "openUrl";
secretButton.Value = "https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/demo-ContosoFlowers/ContosoFlowers.Services";
if (sentGreeting)
{
Card.Buttons.Add(secretButton);
}
链接到此机器人的GitHub repo
答案 1 :(得分:3)
您无法使用开箱即用的WebChat启用/禁用。但是,您可以构建自定义WebChat并添加该功能。
所有魔法都发生在Attachment.tsx文件上。例如,在呈现buttons时会调用HeroCard函数。该函数创建一个按钮并应用CSS类。您可以根据按钮的某个值动态更改类。
顺便说一下,如果你设法做到这一点,IMO,它可能对WebChat组件有很好的贡献。