Crie um fluxo de conversação avançado usando ramificações e loops
Artigo
APLICA-SE A: SDK v4
Você pode criar fluxos de conversa complexos usando a biblioteca de diálogos.
Este artigo aborda como gerenciar conversas complexas que ramificam e fazem loop e como passar argumentos entre diferentes partes da caixa de diálogo.
Nota
Os SDKs JavaScript, C# e Python do Bot Framework continuarão a ser suportados, no entanto, o Java SDK está sendo desativado com suporte final de longo prazo terminando em novembro de 2023.
Os bots existentes construídos com o Java SDK continuarão a funcionar.
Este exemplo representa um bot que pode inscrever usuários para revisar até duas empresas de uma lista.
O bot usa três caixas de diálogo de componentes para gerenciar o fluxo de conversa.
Cada caixa de diálogo de componente inclui uma caixa de diálogo em cascata e todos os prompts necessários para coletar a entrada do usuário.
Essas caixas de diálogo são descritas com mais detalhes nas seções a seguir.
Ele usa o estado da conversa para gerenciar suas caixas de diálogo e usa o estado do usuário para salvar informações sobre o usuário e quais empresas eles desejam revisar.
O bot deriva do manipulador de atividades. Como muitos dos bots de exemplo, ele dá as boas-vindas ao usuário, usa caixas de diálogo para lidar com mensagens do usuário e salva o estado do usuário e da conversa antes que a vez termine.
/// <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
Criar as caixas de diálogo
Este bot contém três caixas de diálogo:
A caixa de diálogo principal inicia o processo geral e, em seguida, resume as informações coletadas.
A caixa de diálogo de nível superior coleta as informações do usuário e inclui lógica de ramificação, com base na idade do usuário.
A caixa de diálogo revisão-seleção permite que o usuário selecione iterativamente as empresas a serem revisadas. Ele usa a lógica de looping para fazer isso.
A caixa de diálogo principal
A caixa de diálogo principal tem duas etapas:
Inicie a caixa de diálogo de nível superior.
Recupere e resuma o perfil de usuário coletado pela caixa de diálogo de nível superior, salve essas informações no estado do usuário e sinalize o fim da caixa de 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);
}
diálogos/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());
}
diálogos\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()
A caixa de diálogo de nível superior
A caixa de diálogo de nível superior tem quatro etapas:
Peça o nome do usuário.
Pergunte a idade do usuário.
Inicie a caixa de diálogo de seleção de revisão ou avance para a próxima etapa, com base na idade do usuário.
Por fim, agradeça ao usuário pela participação e devolva as informações coletadas.
A primeira etapa cria um perfil de usuário vazio como parte do estado da caixa de diálogo. A caixa de diálogo começa com um perfil vazio e adiciona informações ao perfil à medida que ele progride. Quando termina, a última etapa retorna as informações coletadas.
Na terceira etapa (seleção inicial), o fluxo de conversa se ramifica, com base na idade do usuário.
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);
}
}
}
diálogos/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)));
}
diálogos\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)
A caixa de diálogo revisão-seleção
A caixa de diálogo revisão-seleção tem duas etapas:
Peça ao usuário para escolher uma empresa para revisar ou done concluir.
Se a caixa de diálogo foi iniciada com qualquer informação inicial, as informações estão disponíveis através da propriedade options do contexto da etapa em cascata. A caixa de diálogo de seleção de revisão pode ser reiniciada e usa isso para permitir que o usuário escolha mais de uma empresa para revisar.
Se o usuário já tiver selecionado uma empresa para revisão, essa empresa será removida das opções disponíveis.
Uma done opção é adicionada para permitir que o usuário saia do loop mais cedo.
Repita esta caixa de diálogo ou saia, conforme apropriado.
Se o usuário escolheu uma empresa para analisar, adicione-a à lista.
Se o usuário escolheu duas empresas ou optou por sair, encerre a caixa de diálogo e retorne a lista coletada.
Caso contrário, reinicie a caixa de diálogo, inicializando-a com o conteúdo de sua 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);
}
}
diálogos/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);
}
diálogos/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
)
Executar as caixas de diálogo
A classe de bot de diálogo estende o manipulador de atividades e contém a lógica para executar as caixas de diálogo.
A classe de bot de diálogo e boas-vindas estende o bot de diálogo para também dar as boas-vindas a um usuário quando ele ingressa na conversa.
O manipulador de turnos do bot repete o fluxo de conversa definido pelas três caixas de diálogo.
Quando recebe uma mensagem do utilizador:
Ele executa a caixa de diálogo principal.
Se a pilha de diálogo estiver vazia, isso iniciará a caixa de diálogo principal.
Caso contrário, as caixas de diálogo ainda estão no meio do processo, e isso continuará a caixa de diálogo ativa.
Ele salva o estado, para que todas as atualizações do usuário, da conversa e do estado da caixa de diálogo sejam persistentes.
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"),
)
Registrar serviços para o bot
Crie e registre serviços conforme necessário:
Serviços básicos para o bot: um adaptador e a implementação do bot.
Serviços para gerenciar o estado: armazenamento, estado do usuário e estado da conversa.
// 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
O armazenamento de memória é usado apenas para fins de teste e não se destina ao uso em produção.
Certifique-se de usar um tipo persistente de armazenamento para um bot de produção.
Inicie o emulador, conecte-se ao seu bot e envie mensagens como mostrado abaixo.
Recursos adicionais
Para obter uma introdução sobre como implementar uma caixa de diálogo, consulte implementar fluxo de conversa sequencial, que usa uma única caixa de diálogo em cascata e alguns prompts para fazer uma série de perguntas ao usuário.
A biblioteca Diálogos inclui validação básica para prompts. Você também pode adicionar validação personalizada. Para obter mais informações, consulte coletar a entrada do usuário usando um prompt de diálogo.
Para simplificar seu código de diálogo e reutilizá-lo vários bots, você pode definir partes de um conjunto de diálogo como uma classe separada.
Para obter mais informações, consulte caixas de diálogo de reutilização.