Creare un flusso di conversazione avanzato con rami e cicli
Articolo
SI APPLICA A: SDK v4
È possibile creare flussi di conversazione semplici e complessi usando la libreria di dialoghi.
Questo articolo illustra come gestire conversazioni complesse con rami e cicli e come passare argomenti tra parti diverse del dialogo.
Nota
Gli SDK JavaScript, C# e Python di Bot Framework continueranno a essere supportati, ma Java SDK verrà ritirato con il supporto finale a lungo termine che termina a novembre 2023.
I bot esistenti creati con Java SDK continueranno a funzionare.
Questo esempio rappresenta un bot che può consentire agli utenti di accedere per recensire fino a due aziende da un elenco.
Il bot usa tre dialoghi componenti per gestire il flusso di conversazione.
Ogni dialogo componente include un dialogo a cascata ed eventuali richieste necessarie per raccogliere l'input dell'utente.
Questi dialoghi vengono descritti in maggior dettaglio nelle sezioni seguenti.
Il bot usa lo stato della conversazione per gestire i dialoghi e lo stato dell'utente per salvare informazioni sull'utente e sulle aziende che vuole recensire.
Il bot deriva dal gestore attività. Come molti bot di esempio, saluta l'utente, usa i dialoghi per gestire i messaggi che invia e salva il suo stato e quello della conversazione prima che termini in turno.
/// <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
Creare i dialoghi
Questo bot contiene tre dialoghi:
Il dialogo principale avvia il processo generale e quindi riepiloga le informazioni raccolte.
Il dialogo di primo livello raccoglie le informazioni dell'utente e include la logica di diramazione, in base all'età dell'utente.
Il dialogo di selezione della recensione consente all'utente di scegliere le aziende in modo iterativo. A tale scopo, usa la logica di ciclo.
Il dialogo principale
Il dialogo principale prevede due passaggi:
Avviare il dialogo di primo livello.
Recuperare e riepilogare il profilo utente raccolto dal dialogo di primo livello, salvare le informazioni nello stato dell'utente e quindi segnalare la fine del dialogo principale.
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()
Dialogo di primo livello
Il dialogo di primo livello prevede quattro passaggi:
Chiedere il nome dell'utente.
Chiedere l'età dell'utente.
Avviare il dialogo di selezione della recensione o procedere con il passaggio successivo, in base all'età dell'utente.
Ringraziare infine l'utente per aver partecipato e restituire le informazioni raccolte.
Il primo passaggio consiste nella creazione di un profilo utente vuoto come parte dello stato del dialogo. Il dialogo inizia con un profilo vuoto e vi aggiunge informazioni man mano che prosegue. Al termine, l'ultimo passaggio restituisce le informazioni raccolte.
Nel terzo passaggio (avvio della selezione), il flusso della conversazione si dirama, in base all'età dell'utente.
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)
Dialogo di selezione della recensione
Il dialogo di selezione della recensione prevede due passaggi:
Chiedere all'utente di scegliere un'azienda da recensire o scegliere done per terminare.
Se il dialogo è stato avviato con informazioni iniziali, tali informazioni sono disponibili tramite la proprietà options del contesto del passaggio a cascata. Il dialogo di selezione della recensione può riavviarsi automaticamente e in questo modo consente all'utente di scegliere più di un'azienda da recensire.
Se l'utente ha già selezionato un'azienda da recensire, quest'ultima viene rimossa dalle opzioni disponibili.
Viene aggiunta un'opzione done per consentire all'utente di uscire prima dal ciclo.
Ripetere questo dialogo o uscire, a seconda dei casi.
Se l'utente sceglie un'azienda da recensire, aggiungerla al suo elenco.
Se l'utente ha scelto due società o ha scelto di uscire, terminare la finestra di dialogo e restituire l'elenco raccolto.
In caso contrario, riavviare il dialogo, inizializzandolo con il contenuto dell'elenco.
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
)
Eseguire i dialoghi
La classe dialog bot estende il gestore attività e contiene la logica per l'esecuzione dei dialoghi.
La classe dialog and welcome bot classe estende la classe dialog bot per aggiungere anche un messaggio di benvenuto all'utente quando si aggiunge alla conversazione.
Il gestore dei turni del bot ripete il flusso di conversazione definito dai tre dialoghi.
Quando riceve un messaggio dall'utente:
Esegue il dialogo principale.
Se lo stack del dialogo è vuoto, viene avviato il dialogo principale.
In caso contrario, i dialoghi sono ancora in corso e verrà continuato il dialogo attivo.
Lo stato viene salvato, per cui eventuali aggiornamenti apportati allo stato dell'utente, della conversazione e del dialogo vengono resi persistenti.
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"),
)
Registrare i servizi per il bot
Creare e registrare i servizi in base alle esigenze:
Servizi di base per un bot: un adattatore e l'implementazione del bot.
Servizi per la gestione dello stato: archiviazione, stato utente e stato conversazione.
// 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
L'archiviazione di memoria viene usata solo a scopo di test e non è destinata all'uso in produzione.
Assicurarsi di usare un tipo di archiviazione permanente per un bot di produzione.
Avviare l'emulatore, connettersi al bot e inviare messaggi come mostrato di seguito.
Risorse aggiuntive
Per un'introduzione su come implementare un dialogo, vedere Implementare un flusso di conversazione sequenziale, che usa un singolo dialogo a cascata e alcune richieste per porre all'utente una serie di domande.
Per semplificare il codice del dialogo e riusarlo per più bot, è possibile definire parti di un set di dialoghi come classe separata.
Per altre informazioni, vedere Riusare i dialoghi.