你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用按钮进行输入

适用于:SDK v4

按钮改进了聊天体验,因为可以让用户回答问题或选择所需按钮,而不必使用键盘键入响应。 与资讯卡中显示的按钮(即使在选择后仍然可见且可供用户访问)不同,建议的操作窗格中显示的按钮将在用户进行选择后消失。 这可以防止用户在聊天中选择过时按钮并简化机器人开发,因为不需对该场景进行说明。

注意

Bot Framework JavaScript、C# 和 Python SDK 将继续受支持,但 Java SDK 即将停用,长期支持最终将结束于 2023 年 11 月。 仅执行此存储库中的关键安全性和缺陷修复。

使用 Java SDK 构建的现有机器人将继续正常运行。

对于新的机器人生成,请考虑使用 Power Virtual Agents 并参阅选择合适的聊天机器人解决方案

有关详细信息,请参阅机器人构建的未来

使用按钮提供操作建议

建议的操作让机器人能够显示按钮。 可以创建一个建议的操作列表(也称为“快速回复”),该列表将作为单轮聊天显示给用户。

下面是建议的操作示例中的一个示例。

// Creates and sends an activity with suggested actions to the user. When the user
// clicks one of the buttons the text value from the "CardAction" will be
// displayed in the channel just as if the user entered the text. There are multiple
// "ActionTypes" that may be used for different situations.
private static async Task SendSuggestedActionsAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
    var reply = MessageFactory.Text("What is your favorite color?");

    reply.SuggestedActions = new SuggestedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction() { Title = "Red", Type = ActionTypes.ImBack, Value = "Red", Image = "https://via.placeholder.com/20/FF0000?text=R", ImageAltText = "R" },
            new CardAction() { Title = "Yellow", Type = ActionTypes.ImBack, Value = "Yellow", Image = "https://via.placeholder.com/20/FFFF00?text=Y", ImageAltText = "Y" },
            new CardAction() { Title = "Blue", Type = ActionTypes.ImBack, Value = "Blue", Image = "https://via.placeholder.com/20/0000FF?text=B", ImageAltText = "B" },
        },
    };
    await turnContext.SendActivityAsync(reply, cancellationToken);
}

其他资源

可以使用 C#JavaScriptJavaPython 访问“建议的操作”示例的完整源代码。

后续步骤