Creación de un flujo de conversación avanzado con bifurcaciones y bucles
Artículo
SE APLICA A: SDK v4
La biblioteca Dialogs se puede usar para crear flujos de conversación complejos.
En este artículo se describe cómo administrar conversaciones complejas que se bifurcan y crean bucles, y cómo pasar argumentos entre las diferentes partes del diálogo.
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.
En este ejemplo representa un bot que puede registrar usuarios para revisar hasta dos compañías desde una lista.
El bot usa tres diálogos de componente para administrar el flujo de conversación.
Cada diálogo de componente incluye un diálogo en cascada y las solicitudes necesarias para recopilar los datos de entrada del usuario.
En las secciones siguientes se describen estos diálogos de forma más detallada.
Usa el estado de la conversación para administrar sus diálogos y utiliza el estado del usuario para guardar información sobre el usuario y qué compañías quiere revisar.
El bot se deriva del controlador de actividad. Al igual que muchos de los bots de ejemplo, da la bienvenida al usuario, usa diálogos para administrar los mensajes del usuario y guarda el estado del usuario y la conversación antes de finalizar el turno.
Para usar diálogos, instale el paquete de NuGet Microsoft.Bot.Builder.Dialogs.
Para usar diálogos, el proyecto debe instalar el paquete de npm botbuilder-dialogs.
Para usar diálogos, el proyecto debe instalar el paquete de PyPI botbuilder-dialogs mediante la ejecución de pip install botbuilder-dialogs.
Definición del perfil de usuario
El perfil de usuario contendrá la información recopilada por los diálogos, el nombre del usuario, la edad y las compañías seleccionadas para su revisión.
/// <summary>Contains information about a user.</summary>
public class UserProfile
{
public string Name { get; set; }
public int Age { get; set; }
// The list of companies the user wants to review.
public List<string> CompaniesToReview { get; set; } = new List<string>();
userProfile.js
class UserProfile {
constructor(name, age) {
this.name = name;
this.age = age;
// The list of companies the user wants to review.
this.companiesToReview = [];
}
}
UserProfile.java
/**
* Contains information about a user.
*/
public class UserProfile {
private String name;
private Integer age;
// The list of companies the user wants to review.
private List<String> companiesToReview = new ArrayList<>();
/**
* Gets the name of the user
* @return Name of the user
*/
public String getName() {
return name;
}
/**
* Sets the name of the user
* @param name Name of the user
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the age of the user
* @return Age of the user
*/
public Integer getAge() {
return age;
}
/**
* Sets the age of the user
* @param age Age of the user
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* Gets the list of companies
* @return A list of companies
*/
public List<String> getCompaniesToReview() {
return companiesToReview;
}
/**
* Sets a list of companies
* @param companiesToReview A list of companies
*/
public void setCompaniesToReview(List<String> companiesToReview) {
this.companiesToReview = companiesToReview;
}
}
data_models/user_profile.py
class UserProfile:
def __init__(
self, name: str = None, age: int = 0, companies_to_review: List[str] = None
):
self.name: str = name
self.age: int = age
self.companies_to_review: List[str] = companies_to_review
Creación de los diálogos
Este bot contiene tres diálogos:
El diálogo principal inicia el proceso general y, después, resume la información recopilada.
El diálogo de nivel superior recopila la información del usuario e incluye lógica de bifurcación, en función de la edad del usuario.
El diálogo de revisión de la selección permite al usuario seleccionar de forma iterativa las compañías que se van a revisar. Para ello, usa la lógica de bucle.
El diálogo principal
El diálogo principal tiene dos pasos:
Iniciar el diálogo de nivel superior.
Recuperar y resumir el perfil de usuario que recopiló el diálogo de nivel superior, guardar esa información en el estado del usuario y señalizar el final del diálogo principal.
private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.BeginDialogAsync(nameof(TopLevelDialog), null, cancellationToken);
}
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var userInfo = (UserProfile)stepContext.Result;
string status = "You are signed up to review "
+ (userInfo.CompaniesToReview.Count is 0 ? "no companies" : string.Join(" and ", userInfo.CompaniesToReview))
+ ".";
await stepContext.Context.SendActivityAsync(status);
var accessor = _userState.CreateProperty<UserProfile>(nameof(UserProfile));
await accessor.SetAsync(stepContext.Context, userInfo, cancellationToken);
return await stepContext.EndDialogAsync(null, cancellationToken);
}
dialogs/mainDialog.js
async initialStep(stepContext) {
return await stepContext.beginDialog(TOP_LEVEL_DIALOG);
}
async finalStep(stepContext) {
const userInfo = stepContext.result;
const status = 'You are signed up to review ' +
(userInfo.companiesToReview.length === 0 ? 'no companies' : userInfo.companiesToReview.join(' and ')) + '.';
await stepContext.context.sendActivity(status);
await this.userProfileAccessor.set(stepContext.context, userInfo);
return await stepContext.endDialog();
}
MainDialog.java
private CompletableFuture<DialogTurnResult> initialStep(WaterfallStepContext stepContext) {
return stepContext.beginDialog("TopLevelDialog");
}
private CompletableFuture<DialogTurnResult> finalStep(WaterfallStepContext stepContext) {
UserProfile userInfo = (UserProfile) stepContext.getResult();
String status = String.format("You are signed up to review %s.",
userInfo.getCompaniesToReview().size() == 0
? "no companies"
: String.join(" and ", userInfo.getCompaniesToReview()));
return stepContext.getContext().sendActivity(status)
.thenCompose(resourceResponse -> {
StatePropertyAccessor<UserProfile> userProfileAccessor = userState.createProperty("UserProfile");
return userProfileAccessor.set(stepContext.getContext(), userInfo);
})
.thenCompose(setResult -> stepContext.endDialog());
}
dialogs\main_dialog.py
async def initial_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
return await step_context.begin_dialog(TopLevelDialog.__name__)
async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
user_info: UserProfile = step_context.result
companies = (
"no companies"
if len(user_info.companies_to_review) == 0
else " and ".join(user_info.companies_to_review)
)
status = f"You are signed up to review {companies}."
await step_context.context.send_activity(MessageFactory.text(status))
# store the UserProfile
accessor = self.user_state.create_property("UserProfile")
await accessor.set(step_context.context, user_info)
return await step_context.end_dialog()
El diálogo de nivel superior
El diálogo de nivel superior tiene cuatro pasos:
Preguntar por el nombre del usuario.
Preguntar por la edad del usuario.
Iniciar el cuadro de diálogo de revisión de la selección o continuar con el paso siguiente, en función de la edad del usuario.
Por último, agradecer al usuario su participación y devolver la información recopilada.
En el primer paso se crea un perfil de usuario vacío como parte del estado del diálogo. El diálogo se inicia con un perfil vacío y agrega información al perfil a medida que progresa. Cuando termina, el último paso devuelve la información recopilada.
En el tercer paso (selección inicial), el flujo de conversación se ramifica, en función de la edad del usuario.
stepContext.Values[UserInfo] = new UserProfile();
var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your name.") };
// Ask the user to enter their name.
return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
}
private async Task<DialogTurnResult> AgeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Set the user's name to what they entered in response to the name prompt.
var userProfile = (UserProfile)stepContext.Values[UserInfo];
userProfile.Name = (string)stepContext.Result;
var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your age.") };
// Ask the user to enter their age.
return await stepContext.PromptAsync(nameof(NumberPrompt<int>), promptOptions, cancellationToken);
}
private async Task<DialogTurnResult> StartSelectionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Set the user's age to what they entered in response to the age prompt.
var userProfile = (UserProfile)stepContext.Values[UserInfo];
userProfile.Age = (int)stepContext.Result;
if (userProfile.Age < 25)
{
// If they are too young, skip the review selection dialog, and pass an empty list to the next step.
await stepContext.Context.SendActivityAsync(
MessageFactory.Text("You must be 25 or older to participate."),
cancellationToken);
return await stepContext.NextAsync(new List<string>(), cancellationToken);
}
else
{
// Otherwise, start the review selection dialog.
return await stepContext.BeginDialogAsync(nameof(ReviewSelectionDialog), null, cancellationToken);
}
}
private async Task<DialogTurnResult> AcknowledgementStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Set the user's company selection to what they entered in the review-selection dialog.
var userProfile = (UserProfile)stepContext.Values[UserInfo];
userProfile.CompaniesToReview = stepContext.Result as List<string> ?? new List<string>();
// Thank them for participating.
await stepContext.Context.SendActivityAsync(
MessageFactory.Text($"Thanks for participating, {((UserProfile)stepContext.Values[UserInfo]).Name}."),
cancellationToken);
// Exit the dialog, returning the collected user information.
return await stepContext.EndDialogAsync(stepContext.Values[UserInfo], cancellationToken);
}
}
}
dialogs/topLevelDialog.js
async nameStep(stepContext) {
// Create an object in which to collect the user's information within the dialog.
stepContext.values.userInfo = new UserProfile();
const promptOptions = { prompt: 'Please enter your name.' };
// Ask the user to enter their name.
return await stepContext.prompt(TEXT_PROMPT, promptOptions);
}
async ageStep(stepContext) {
// Set the user's name to what they entered in response to the name prompt.
stepContext.values.userInfo.name = stepContext.result;
const promptOptions = { prompt: 'Please enter your age.' };
// Ask the user to enter their age.
return await stepContext.prompt(NUMBER_PROMPT, promptOptions);
}
async startSelectionStep(stepContext) {
// Set the user's age to what they entered in response to the age prompt.
stepContext.values.userInfo.age = stepContext.result;
if (stepContext.result < 25) {
// If they are too young, skip the review selection dialog, and pass an empty list to the next step.
await stepContext.context.sendActivity('You must be 25 or older to participate.');
return await stepContext.next();
} else {
// Otherwise, start the review selection dialog.
return await stepContext.beginDialog(REVIEW_SELECTION_DIALOG);
}
}
async acknowledgementStep(stepContext) {
// Set the user's company selection to what they entered in the review-selection dialog.
const userProfile = stepContext.values.userInfo;
userProfile.companiesToReview = stepContext.result || [];
await stepContext.context.sendActivity(`Thanks for participating ${ userProfile.name }`);
// Exit the dialog, returning the collected user information.
return await stepContext.endDialog(userProfile);
}
TopLevelDialog.java
private CompletableFuture<DialogTurnResult> nameStep(WaterfallStepContext stepContext) {
// Create an object in which to collect the user's information within the dialog.
stepContext.getValues().put(USER_INFO, new UserProfile());
// Ask the user to enter their name.
PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(MessageFactory.text("Please enter your name."));
return stepContext.prompt("TextPrompt", promptOptions);
}
private CompletableFuture<DialogTurnResult> ageStep(WaterfallStepContext stepContext) {
// Set the user's name to what they entered in response to the name prompt.
UserProfile userProfile = (UserProfile) stepContext.getValues().get(USER_INFO);
userProfile.setName((String) stepContext.getResult());
// Ask the user to enter their age.
PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(MessageFactory.text("Please enter your age."));
return stepContext.prompt("NumberPrompt", promptOptions);
}
private CompletableFuture<DialogTurnResult> startSelectionStep(WaterfallStepContext stepContext) {
// Set the user's age to what they entered in response to the age prompt.
UserProfile userProfile = (UserProfile) stepContext.getValues().get(USER_INFO);
userProfile.setAge((Integer) stepContext.getResult());
// If they are too young, skip the review selection dialog, and pass an empty list to the next step.
if (userProfile.getAge() < 25) {
return stepContext.getContext().sendActivity(MessageFactory.text("You must be 25 or older to participate."))
.thenCompose(resourceResponse -> stepContext.next(new ArrayList<String>()));
}
// Otherwise, start the review selection dialog.
return stepContext.beginDialog("ReviewSelectionDialog");
}
private CompletableFuture<DialogTurnResult> acknowledgementStep(WaterfallStepContext stepContext) {
// Set the user's company selection to what they entered in the review-selection dialog.
UserProfile userProfile = (UserProfile) stepContext.getValues().get(USER_INFO);
userProfile.setCompaniesToReview(stepContext.getResult() instanceof List
? (List<String>) stepContext.getResult()
: new ArrayList<>());
// Thank them for participating.
return stepContext.getContext()
.sendActivity(MessageFactory.text(String.format("Thanks for participating, %s.", userProfile.getName())))
.thenCompose(resourceResponse -> stepContext.endDialog(stepContext.getValues().get(USER_INFO)));
}
dialogs\top_level_dialog.py
async def name_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
# Create an object in which to collect the user's information within the dialog.
step_context.values[self.USER_INFO] = UserProfile()
# Ask the user to enter their name.
prompt_options = PromptOptions(
prompt=MessageFactory.text("Please enter your name.")
)
return await step_context.prompt(TextPrompt.__name__, prompt_options)
async def age_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
# Set the user's name to what they entered in response to the name prompt.
user_profile = step_context.values[self.USER_INFO]
user_profile.name = step_context.result
# Ask the user to enter their age.
prompt_options = PromptOptions(
prompt=MessageFactory.text("Please enter your age.")
)
return await step_context.prompt(NumberPrompt.__name__, prompt_options)
async def start_selection_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
# Set the user's age to what they entered in response to the age prompt.
user_profile: UserProfile = step_context.values[self.USER_INFO]
user_profile.age = step_context.result
if user_profile.age < 25:
# If they are too young, skip the review selection dialog, and pass an empty list to the next step.
await step_context.context.send_activity(
MessageFactory.text("You must be 25 or older to participate.")
)
return await step_context.next([])
# Otherwise, start the review selection dialog.
return await step_context.begin_dialog(ReviewSelectionDialog.__name__)
async def acknowledgement_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
# Set the user's company selection to what they entered in the review-selection dialog.
user_profile: UserProfile = step_context.values[self.USER_INFO]
user_profile.companies_to_review = step_context.result
# Thank them for participating.
await step_context.context.send_activity(
MessageFactory.text(f"Thanks for participating, {user_profile.name}.")
)
# Exit the dialog, returning the collected user information.
return await step_context.end_dialog(user_profile)
El diálogo de selección de revisión
El diálogo de revisión-selección tiene dos pasos:
Pedir al usuario que elija una empresa para revisar o elegir done para terminar.
Si el diálogo se inició con información inicial, la información está disponible en la propiedad options del contexto del paso en cascada. El diálogo de revisión de la selección puede reiniciarse a sí mismo, para que el usuario pueda elegir más de una empresa para revisar.
Si el usuario ya ha seleccionado una empresa para revisar, esta se quita de las opciones disponibles.
Se agrega una opción done para que el usuario pueda salir del bucle antes del final.
Repita este diálogo o salga, según corresponda.
Si el usuario elige una empresa para revisar, se agrega a su lista.
Si el usuario ha elegido dos empresas o elige salir, el diálogo termina y vuelve a la lista recopilada.
En caso contrario, reinicia el diálogo y se inicializa con el contenido de la lista.
private async Task<DialogTurnResult> SelectionStepAsync(
WaterfallStepContext stepContext,
CancellationToken cancellationToken)
{
// Continue using the same selection list, if any, from the previous iteration of this dialog.
var list = stepContext.Options as List<string> ?? new List<string>();
stepContext.Values[CompaniesSelected] = list;
// Create a prompt message.
string message;
if (list.Count is 0)
{
message = $"Please choose a company to review, or `{DoneOption}` to finish.";
}
else
{
message = $"You have selected **{list[0]}**. You can review an additional company, " +
$"or choose `{DoneOption}` to finish.";
}
// Create the list of options to choose from.
var options = _companyOptions.ToList();
options.Add(DoneOption);
if (list.Count > 0)
{
options.Remove(list[0]);
}
var promptOptions = new PromptOptions
{
Prompt = MessageFactory.Text(message),
RetryPrompt = MessageFactory.Text("Please choose an option from the list."),
Choices = ChoiceFactory.ToChoices(options),
};
// Prompt the user for a choice.
return await stepContext.PromptAsync(nameof(ChoicePrompt), promptOptions, cancellationToken);
}
private async Task<DialogTurnResult> LoopStepAsync(
WaterfallStepContext stepContext,
CancellationToken cancellationToken)
{
// Retrieve their selection list, the choice they made, and whether they chose to finish.
var list = stepContext.Values[CompaniesSelected] as List<string>;
var choice = (FoundChoice)stepContext.Result;
var done = choice.Value == DoneOption;
if (!done)
{
// If they chose a company, add it to the list.
list.Add(choice.Value);
}
if (done || list.Count >= 2)
{
// If they're done, exit and return their list.
return await stepContext.EndDialogAsync(list, cancellationToken);
}
else
{
// Otherwise, repeat this dialog, passing in the list from this iteration.
return await stepContext.ReplaceDialogAsync(InitialDialogId, list, cancellationToken);
}
}
dialogs/reviewSelectionDialog.js
async selectionStep(stepContext) {
// Continue using the same selection list, if any, from the previous iteration of this dialog.
const list = Array.isArray(stepContext.options) ? stepContext.options : [];
stepContext.values[this.companiesSelected] = list;
// Create a prompt message.
let message = '';
if (list.length === 0) {
message = `Please choose a company to review, or \`${ this.doneOption }\` to finish.`;
} else {
message = `You have selected **${ list[0] }**. You can review an additional company, or choose \`${ this.doneOption }\` to finish.`;
}
// Create the list of options to choose from.
const options = list.length > 0
? this.companyOptions.filter(function(item) { return item !== list[0]; })
: this.companyOptions.slice();
options.push(this.doneOption);
// Prompt the user for a choice.
return await stepContext.prompt(CHOICE_PROMPT, {
prompt: message,
retryPrompt: 'Please choose an option from the list.',
choices: options
});
}
async loopStep(stepContext) {
// Retrieve their selection list, the choice they made, and whether they chose to finish.
const list = stepContext.values[this.companiesSelected];
const choice = stepContext.result;
const done = choice.value === this.doneOption;
if (!done) {
// If they chose a company, add it to the list.
list.push(choice.value);
}
if (done || list.length > 1) {
// If they're done, exit and return their list.
return await stepContext.endDialog(list);
} else {
// Otherwise, repeat this dialog, passing in the list from this iteration.
return await stepContext.replaceDialog(this.initialDialogId, list);
}
}
ReviewSelectionDialog.java
private CompletableFuture<DialogTurnResult> selectionStep(WaterfallStepContext stepContext) {
// Continue using the same selection list, if any, from the previous iteration of this dialog.
List<String> list = stepContext.getOptions() instanceof List
? (List<String>) stepContext.getOptions()
: new ArrayList<>();
stepContext.getValues().put(COMPANIES_SELECTED, list);
// Create a prompt message.
String message;
if (list.size() == 0) {
message = String.format("Please choose a company to review, or `%s` to finish.", DONE_OPTION);
} else {
message = String.format("You have selected **%s**. You can review an additional company, or choose `%s` to finish.",
list.get(0),
DONE_OPTION);
}
// Create the list of options to choose from.
List<String> options = new ArrayList<>(companiesOptions);
options.add(DONE_OPTION);
if (list.size() > 0) {
options.remove(list.get(0));
}
PromptOptions promptOptions = new PromptOptions();
promptOptions.setPrompt(MessageFactory.text(message));
promptOptions.setRetryPrompt(MessageFactory.text("Please choose an option from the list."));
promptOptions.setChoices(ChoiceFactory.toChoices(options));
// Prompt the user for a choice.
return stepContext.prompt("ChoicePrompt", promptOptions);
}
private CompletableFuture<DialogTurnResult> loopStep(WaterfallStepContext stepContext) {
// Retrieve their selection list, the choice they made, and whether they chose to finish.
List<String> list = (List<String>) stepContext.getValues().get(COMPANIES_SELECTED);
FoundChoice choice = (FoundChoice) stepContext.getResult();
boolean done = StringUtils.equals(choice.getValue(), DONE_OPTION);
// If they chose a company, add it to the list.
if (!done) {
list.add(choice.getValue());
}
// If they're done, exit and return their list.
if (done || list.size() >= 2) {
return stepContext.endDialog(list);
}
// Otherwise, repeat this dialog, passing in the list from this iteration.
return stepContext.replaceDialog(getId(), list);
}
dialogs/review_selection_dialog.py
async def selection_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
# step_context.options will contains the value passed in begin_dialog or replace_dialog.
# if this value wasn't provided then start with an emtpy selection list. This list will
# eventually be returned to the parent via end_dialog.
selected: [
str
] = step_context.options if step_context.options is not None else []
step_context.values[self.COMPANIES_SELECTED] = selected
if len(selected) == 0:
message = (
f"Please choose a company to review, or `{self.DONE_OPTION}` to finish."
)
else:
message = (
f"You have selected **{selected[0]}**. You can review an additional company, "
f"or choose `{self.DONE_OPTION}` to finish. "
)
# create a list of options to choose, with already selected items removed.
options = self.company_options.copy()
options.append(self.DONE_OPTION)
if len(selected) > 0:
options.remove(selected[0])
# prompt with the list of choices
prompt_options = PromptOptions(
prompt=MessageFactory.text(message),
retry_prompt=MessageFactory.text("Please choose an option from the list."),
choices=self._to_choices(options),
)
return await step_context.prompt(ChoicePrompt.__name__, prompt_options)
def _to_choices(self, choices: [str]) -> List[Choice]:
choice_list: List[Choice] = []
for choice in choices:
choice_list.append(Choice(value=choice))
return choice_list
async def loop_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
selected: List[str] = step_context.values[self.COMPANIES_SELECTED]
choice: FoundChoice = step_context.result
done = choice.value == self.DONE_OPTION
# If they chose a company, add it to the list.
if not done:
selected.append(choice.value)
# If they're done, exit and return their list.
if done or len(selected) >= 2:
return await step_context.end_dialog(selected)
# Otherwise, repeat this dialog, passing in the selections from this iteration.
return await step_context.replace_dialog(
self.initial_dialog_id, selected
)
Ejecución de los diálogos
La clase bot de diálogo amplía el controlador de actividades y contiene la lógica para ejecutar los diálogos.
El diálogo bot de bienvenida y diálogo amplía el bot de diálogo para que dé la bienvenida a un usuario cuando se una a la conversación.
El controlador de turnos del bot repite el flujo de conversación definido por estos tres diálogos.
Cuando recibe un mensaje del usuario:
Ejecuta el diálogo principal.
Si la pila del diálogo está vacía, se iniciará el diálogo principal.
De lo contrario, los diálogos siguen en mitad del proceso y se continuará con el diálogo activo.
Guarda el estado para conservar todas las actualizaciones del usuario, la conversación y el diálogo.
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occurred during the turn.
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
Logger.LogInformation("Running dialog with Message Activity.");
// Run the Dialog with the new message Activity.
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
}
bots/dialogBot.js
this.onMessage(async (context, next) => {
console.log('Running dialog with Message Activity.');
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
// By calling next() you ensure that the next BotHandler is run.
await next();
});
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
DialogBot.java
@Override
public CompletableFuture<Void> onTurn(
TurnContext turnContext
) {
return super.onTurn(turnContext)
// Save any state changes that might have occurred during the turn.
.thenCompose(result -> conversationState.saveChanges(turnContext))
.thenCompose(result -> userState.saveChanges(turnContext));
}
@Override
protected CompletableFuture<Void> onMessageActivity(
TurnContext turnContext
) {
LoggerFactory.getLogger(DialogBot.class).info("Running dialog with Message Activity.");
// Run the Dialog with the new message Activity.
return Dialog.run(dialog, turnContext, conversationState.createProperty("DialogState"));
}
bots/dialog_bot.py
async def on_turn(self, turn_context: TurnContext):
await super().on_turn(turn_context)
# Save any state changes that might have occurred during the turn.
await self.conversation_state.save_changes(turn_context, False)
await self.user_state.save_changes(turn_context, False)
async def on_message_activity(self, turn_context: TurnContext):
await DialogHelper.run_dialog(
self.dialog,
turn_context,
self.conversation_state.create_property("DialogState"),
)
Registro de los servicios del bot
Cree y registre los servicios que sean necesarios:
Servicios básicos del bot: un adaptador y la implementación del bot.
Servicios para administrar el estado: almacenamiento, estado del usuario y estado de la conversación.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient().AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.MaxDepth = HttpHelper.BotMessageSerializerSettings.MaxDepth;
});
// Create the Bot Framework Authentication to be used with the Bot Adapter.
services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();
// Create the Bot Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
services.AddSingleton<IStorage, MemoryStorage>();
// Create the User state. (Used in this bot's Dialog implementation.)
services.AddSingleton<UserState>();
index.js
// Create user and conversation state with in-memory storage provider.
const userState = new UserState(memoryStorage);
const conversationState = new ConversationState(memoryStorage);
// Create the main dialog.
const dialog = new MainDialog(userState);
const bot = new DialogAndWelcomeBot(conversationState, userState, dialog);
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
Application.java
@Bean
public Bot getBot(
ConversationState conversationState,
UserState userState,
Dialog dialog
) {
return new DialogAndWelcome<>(conversationState, userState, dialog);
}
app.py
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG))
# Catch-all for errors.
CONVERSATION_STATE = ConversationState(MEMORY)
# Create Dialog and Bot
DIALOG = MainDialog(USER_STATE)
BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG)
# Listen for incoming requests on /api/messages.
Nota:
El almacenamiento en memoria se usa solo con fines de prueba y no está pensado para su uso en producción.
Asegúrese de usar un tipo de almacenamiento persistente para un bot de producción.
Inicie el emulador, conéctese al bot y envíe mensajes como se muestra a continuación.
Recursos adicionales
Para ver una introducción acerca de cómo implementar un diálogo, consulte el artículo sobre cómo implementar el flujo de conversación secuencial, que utiliza un único diálogo en cascada y unas pocas indicaciones para formular al usuario una serie de preguntas.
Para simplificar el código de diálogo y reutilizarlo en varios bots, puede definir partes de un conjunto de diálogo como una clase independiente.
Para más información, consulte Reutilización de diálogos.