Vytvoření pokročilého toku konverzace pomocí větví a smyček
Článek
PLATÍ PRO: SDK v4
Pomocí knihovny dialogových oken můžete vytvářet složité toky konverzací.
Tento článek popisuje, jak spravovat složité konverzace, které větve a smyčky a jak předávat argumenty mezi různými částmi dialogového okna.
Poznámka:
Sady SDK služby Bot Framework JavaScript, C# a Python budou nadále podporovány, ale sada Java SDK se vyřazuje s konečnou dlouhodobou podporou končící v listopadu 2023.
Stávající roboti sestavení pomocí sady Java SDK budou i nadále fungovat.
Tato ukázka představuje robota, který může zaregistrovat uživatele a zkontrolovat až dvě společnosti ze seznamu.
Robot ke správě toku konverzace používá tři dialogy komponent.
Každé dialogové okno komponenty obsahuje vodopádové dialogové okno a všechny výzvy potřebné ke shromáždění uživatelského vstupu.
Tato dialogová okna jsou podrobněji popsána v následujících částech.
Ke správě dialogových oken používá stav konverzace a používá stav uživatele k ukládání informací o uživateli a o tom, které společnosti chtějí zkontrolovat.
Robot je odvozený z obslužné rutiny aktivity. Stejně jako mnoho ukázkových robotů vítá uživatele, používá dialogy ke zpracování zpráv od uživatele a ukládá stav uživatele a konverzace před koncem turnu.
/// <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
Vytvoření dialogových oken
Tento robot obsahuje tři dialogy:
Hlavní dialog spustí celkový proces a pak shrne shromážděné informace.
Dialogové okno nejvyšší úrovně shromažďuje informace o uživatelích a zahrnuje logiku větvení na základě věku uživatele.
Dialogové okno pro výběr kontroly umožňuje uživateli iterativní výběr společností, které se mají zkontrolovat. K tomu používá logiku smyčky.
Hlavní dialog
Hlavní dialog má dva kroky:
Spusťte dialogové okno nejvyšší úrovně.
Načtěte a shrňte profil uživatele, který shromáždil dialog nejvyšší úrovně, uložil tyto informace do stavu uživatele a pak signalizoval konec hlavního dialogového okna.
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);
}
dialogy/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());
}
dialogy\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()
Dialogové okno nejvyšší úrovně
Dialogové okno nejvyšší úrovně má čtyři kroky:
Požádejte o jméno uživatele.
Požádejte o věk uživatele.
Buď spusťte dialogové okno pro výběr kontroly nebo přejděte k dalšímu kroku na základě věku uživatele.
Nakonec uživateli děkuji za účast a vrácení shromážděných informací.
První krok vytvoří prázdný profil uživatele jako součást stavu dialogového okna. Dialogové okno začíná prázdným profilem a při jeho průběhu přidá do profilu informace. Po skončení vrátí poslední krok shromážděné informace.
Ve třetím kroku (počáteční výběr) se tok konverzace větví podle věku uživatele.
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);
}
}
}
dialogy/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)));
}
dialogy\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)
Dialogové okno pro výběr revizí
Dialogové okno pro výběr revizí má dva kroky:
Požádejte uživatele, aby zvolil společnost, která si ji má zkontrolovat nebo done dokončit.
Pokud se dialogové okno spustilo s počátečními informacemi, jsou informace k dispozici prostřednictvím vlastnosti možností kontextu vodopádového kroku. Dialogové okno pro výběr revizí se může restartovat a používá ho k tomu, aby si uživatel mohl vybrat více než jednu společnost ke kontrole.
Pokud už uživatel vybral společnost k revizi, odebere se z dostupných voleb.
Přidá done se volba, která uživateli umožní včas ukončit smyčku.
Podle potřeby tento dialog opakujte nebo ukončete.
Pokud si uživatel vybral společnost ke kontrole, přidejte ji do svého seznamu.
Pokud uživatel vybral dvě společnosti nebo se rozhodl ukončit, ukončete dialogové okno a vraťte shromážděný seznam.
V opačném případě dialogové okno restartujte a inicializujete ho obsahem jejich seznamu.
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);
}
}
dialogy/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);
}
dialogy/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
)
Spuštění dialogů
Třída robota dialogového okna rozšiřuje obslužnou rutinu aktivity a obsahuje logiku pro spouštění dialogů.
Třída dialogového okna a uvítacího robota rozšiřuje robota dialogového okna, aby také přivítal uživatele, když se připojí ke konverzaci.
Obslužná rutina otáčení robota opakuje tok konverzace definovaný třemi dialogy.
Když uživatel obdrží zprávu:
Spustí hlavní dialog.
Pokud je zásobník dialogového okna prázdný, spustí se hlavní dialog.
V opačném případě jsou dialogy stále v polovině procesu a budou pokračovat v aktivním dialogovém okně.
Uloží stav, takže všechny aktualizace uživatele, konverzace a stavu dialogového okna se zachovají.
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);
}
roboti/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"));
}
roboti/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"),
)
Registrace služeb pro robota
Podle potřeby vytvořte a zaregistrujte služby:
Základní služby pro robota: adaptér a implementace robota
Služby pro správu stavu: úložiště, stav uživatele a stav konverzace.
// 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.
Poznámka:
Úložiště paměti se používá jenom pro účely testování a není určené pro produkční použití.
Ujistěte se, že pro produkčního robota používáte trvalý typ úložiště.
Spusťte emulátor, připojte se k robotovi a odešlete zprávy, jak je znázorněno níže.
Další materiály
Úvod k implementaci dialogového okna najdete v tématu Implementace sekvenčního toku konverzace, který používá jeden vodopádový dialog a několik výzev k zadání řady otázek uživatele.
Pokud chcete zjednodušit kód dialogového okna a znovu ho použít více robotů, můžete definovat části dialogového okna nastavené jako samostatnou třídu.
Další informace najdete v dialogových oknech pro opakované použití.