Create advanced conversation flow using branches and loops
Artikkeli
APPLIES TO: SDK v4
You can create complex conversation flows using the dialogs library.
This article covers how to manage complex conversations that branch and loop and how to pass arguments between different parts of the dialog.
Note
The Bot Framework JavaScript, C#, and Python SDKs will continue to be supported, however, the Java SDK is being retired with final long-term support ending in November 2023.
Existing bots built with the Java SDK will continue to function.
This sample represents a bot that can sign users up to review up to two companies from a list.
The bot uses three component dialogs to manage the conversation flow.
Each component dialog includes a waterfall dialog and any prompts needed to gather user input.
These dialogs are described in more detail in the following sections.
It uses conversation state to manage its dialogs and uses user state to save information about the user and which companies they want to review.
The bot derives from the activity handler. Like many of the sample bots, it welcomes the user, uses dialogs to handle messages from the user, and saves user and conversation state before the turn ends.
/// <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
Create the dialogs
This bot contains three dialogs:
The main dialog starts the overall process and then summarizes the collected information.
The top-level dialog collects the user information and includes branching logic, based on the user's age.
The review-selection dialog allows the user to iteratively select companies to review. It uses looping logic to do so.
The main dialog
The main dialog has two steps:
Start the top-level dialog.
Retrieve and summarize the user profile that the top-level dialog collected, save that information to user state, and then signal the end of the main dialog.
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()
The top-level dialog
The top-level dialog has four steps:
Ask for the user's name.
Ask for the user's age.
Either start the review-selection dialog or progress to the next step, based on the user's age.
Finally, thank the user for participating and return the collected information.
The first step creates an empty user profile as part of the dialog state. The dialog starts with an empty profile and adds information to the profile as it progresses. When it ends, the last step returns the collected information.
In the third (start selection) step, the conversation flow branches, based on the user's age.
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)
The review-selection dialog
The review-selection dialog has two steps:
Ask the user to choose a company to review or done to finish.
If the dialog was started with any initial information, the information is available through the options property of the waterfall step context. The review-selection dialog can restart itself, and it uses this to allow the user to choose more than one company to review.
If the user has already selected a company to review, that company is removed from the available choices.
A done choice is added to allow the user to exit the loop early.
Repeat this dialog or exit, as appropriate.
If the user chose a company to review, add it to their list.
If the user has chosen two companies or they chose to exit, end the dialog and return the collected list.
Otherwise, restart the dialog, initializing it with the contents of their list.
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
)
Run the dialogs
The dialog bot class extends the activity handler, and it contains the logic for running the dialogs.
The dialog and welcome bot class extends the dialog bot to also welcome a user when they join the conversation.
The bot's turn handler repeats the conversation flow defined by the three dialogs.
When it receives a message from the user:
It runs the main dialog.
If the dialog stack is empty, this will start the main dialog.
Otherwise, the dialogs are still in mid-process, and this will continue the active dialog.
It saves state, so that any updates to the user, conversation, and dialog state are persisted.
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"),
)
Register services for the bot
Create and register services as needed:
Basic services for the bot: an adapter and the bot implementation.
Services for managing state: storage, user state, and conversation state.
// 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.
Note
Memory storage is used for testing purposes only and isn't intended for production use.
Be sure to use a persistent type of storage for a production bot.
Start the Emulator, connect to your bot, and send messages as shown below.
Additional resources
For an introduction on how to implement a dialog, see implement sequential conversation flow, which uses a single waterfall dialog and a few prompts to ask the user a series of questions.
The Dialogs library includes basic validation for prompts. You can also add custom validation. For more information, see gather user input using a dialog prompt.
To simplify your dialog code and reuse it multiple bots, you can define portions of a dialog set as a separate class.
For more information, see reuse dialogs.