Los botones mejoran la experiencia conversacional al permitir al usuario responder a una pregunta o seleccionar el botón deseado, en lugar de tener que escribir una respuesta con el teclado. A diferencia de los botones que aparecen en las tarjetas enriquecidas (que permanecen visibles y accesibles para el usuario incluso después de que se seleccionen), los botones que aparecen en el panel de acciones sugeridas desaparecerán una vez que el usuario haya hecho una selección. Esto evita que el usuario seleccione botones obsoletos dentro de una conversación y simplifica el desarrollo del bot, ya que no tendrá que tener en cuenta ese escenario.
Nota:
Los SDK de JavaScript, C# y Python de Bot Framework seguirán siendo compatibles, pero el SDK de Java se va a retirar con la compatibilidad final a largo plazo que finaliza en noviembre de 2023.
Los bots existentes creados con el SDK de Java seguirán funcionando.
Las acciones sugeridas permiten al bot presentar botones. Puede crear una lista de acciones sugeridas (también conocidas como respuestas rápidas) que se mostrarán al usuario para un único turno de la conversación:
// 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);
}
/**
* 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)
Recursos adicionales
Puede acceder al código fuente completo para el ejemplo de acciones sugeridas en C#, JavaScript, Java y Python.