适用于:SDK v4
按钮改进了聊天体验,因为可以让用户回答问题或选择所需按钮,而不必使用键盘键入响应。 与资讯卡中显示的按钮(即使在选择后仍然可见且可供用户访问)不同,建议的操作窗格中显示的按钮将在用户进行选择后消失。 这可以防止用户在聊天中选择过时按钮并简化机器人开发,因为不需对该场景进行说明。
重要
Bot Framework SDK 和 Bot Framework Emulator 已在 GitHub 上存档。 项目不再更新或维护。 自 2025 年 12 月 31 日起,Bot Framework SDK 的支持票证将不再提供服务。
若要使用所选的 AI 服务、业务流程和知识生成代理,请考虑使用 Microsoft 365 代理 SDK。 代理 SDK 对 C#、JavaScript 或 Python 具有语言支持。 可以在 aka.ms/agents 了解有关代理 SDK 的详细信息。
如果现有的机器人是使用 Bot Framework SDK 生成的,则可以将机器人更新到代理 SDK。 查看 Bot Framework SDK 到代理 SDK 迁移指南的核心更改和更新。
如果要查找基于 SaaS 的代理平台,请考虑 Microsoft Copilot Studio。
建议的操作让机器人能够显示按钮。 可以创建一个建议的操作列表(也称为“快速回复”),该列表将作为单轮聊天显示给用户。
下面是建议的操作示例中的一个示例。
// 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);
}
下面是建议的操作示例中的一个示例。
}
}
}
/**
* Send suggested actions to the user.
* @param {TurnContext} turnContext A TurnContext instance containing all the data needed for processing this conversation turn.
*/
async sendSuggestedActions(turnContext) {
const cardActions = [
{
type: ActionTypes.PostBack,
title: 'Red',
value: 'Red',
image: 'https://placehold.co/20/red/red?text=R',
imageAltText: 'R'
},
{
type: ActionTypes.PostBack,
title: 'Yellow',
value: 'Yellow',
image: 'https://placehold.co/20/yellow/yellow?text=Y',
imageAltText: 'Y'
},
{
type: ActionTypes.PostBack,
title: 'Blue',
value: 'Blue',
image: 'https://placehold.co/20/blue/blue?text=B',
imageAltText: 'B'
}
];
var reply = MessageFactory.suggestedActions(cardActions, 'What is the best color?');
await turnContext.sendActivity(reply);
}
下面是建议的操作示例中的一个示例。
/**
* 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 CompletableFuture<Void> sendSuggestedActions(TurnContext turnContext) {
Activity reply = MessageFactory.text("What is your favorite color?");
CardAction redAction = new CardAction();
redAction.setTitle("Red");
redAction.setType(ActionTypes.IM_BACK);
redAction.setValue("Red");
redAction.setImage("https://via.placeholder.com/20/FF0000?text=R");
redAction.setImageAltText("R");
CardAction yellowAction = new CardAction();
yellowAction.setTitle("Yellow");
yellowAction.setType(ActionTypes.IM_BACK);
yellowAction.setValue("Yellow");
yellowAction.setImage("https://via.placeholder.com/20/FFFF00?text=Y");
yellowAction.setImageAltText("Y");
CardAction blueAction = new CardAction();
blueAction.setTitle("Blue");
blueAction.setType(ActionTypes.IM_BACK);
blueAction.setValue("Blue");
blueAction.setImage("https://via.placeholder.com/20/0000FF?text=B");
blueAction.setImageAltText("B");
SuggestedActions actions = new SuggestedActions();
actions.setActions(Arrays.asList(redAction, yellowAction, blueAction));
reply.setSuggestedActions(actions);
return turnContext.sendActivity(reply).thenApply(sendResult -> null);
}
下面是建议的操作示例中的一个示例。
async def _send_suggested_actions(self, turn_context: TurnContext):
"""
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.
"""
reply = MessageFactory.text("What is your favorite color?")
reply.suggested_actions = SuggestedActions(
actions=[
CardAction(
title="Red",
type=ActionTypes.im_back,
value="Red",
image="https://via.placeholder.com/20/FF0000?text=R",
image_alt_text="R",
),
CardAction(
title="Yellow",
type=ActionTypes.im_back,
value="Yellow",
image="https://via.placeholder.com/20/FFFF00?text=Y",
image_alt_text="Y",
),
CardAction(
title="Blue",
type=ActionTypes.im_back,
value="Blue",
image="https://via.placeholder.com/20/0000FF?text=B",
image_alt_text="B",
),
]
)
return await turn_context.send_activity(reply)
其他资源
可以使用 C#、JavaScript、Java 和 Python 访问“建议的操作”示例的完整源代码。
后续步骤