我的 TaskOnTurn 方法有问题。当我启动机器人时,它会发送欢迎消息并启动一个新的对话框,因为
if(!turnContext.Responded)
现在,当我在对话框中时,它会再次跳到最后一个 if 语句,并且当我已经在一个对话框中时,会启动一个新对话框。 !turnContext.Responded 如何工作?
然后我尝试启动对话框
if (turnContext.Activity.MembersAdded != null)
在等待SendWecomeMessage 下。那行不通。然后,它发送 1 条欢迎消息,然后启动 2 个对话。这也让我很困惑。
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
var activity = turnContext.Activity;
var dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
if (turnContext.Activity.Type == ActivityTypes.Message)
{
await dc.ContinueDialogAsync(cancellationToken);
}
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
{
if (turnContext.Activity.MembersAdded != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
}
else
{
await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken: cancellationToken);
}
if (!turnContext.Responded)
{
await dc.BeginDialogAsync(ReservationDialog, cancellationToken);
}
}
TurnContext.Responded https://learn.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.turncontext.responded?view=botbuilder-dotnet-stable#Microsoft_Bot_Builder_TurnContext_Responded指示当前回合是否至少发送了一个响应。OnTurnAsync
在每个瀑布步骤之间触发,所以如果ReservationDialog
有提示,一旦用户回答提示,OnTurnAsync
被解雇,并且由于机器人尚未在该 StepContext 内做出响应,TurnContext.Responded
is false
。这就是为什么你会在另一个对话框中间看到一个对话框。
您可以选择很多不同的路线OnTurnAsync
。我绝对推荐
检查一些样品 https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore看看他们是如何做事的——他们中的大多数都得到了很好的评论。
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
var activity = turnContext.Activity;
var dc = await Dialogs.CreateContextAsync(turnContext);
// Execute on incoming messages
if (activity.Type == ActivityTypes.Message)
{
if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
activity.Text = JsonConvert.SerializeObject(activity.Value);
}
}
var dialogResult = await dc.ContinueDialogAsync();
// Execute based on different situations within a Dialog. See BasicBot for examples:
// https://github.com/Microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/13.basic-bot/BasicBot.cs#L112
if (!dc.Context.Responded)
{
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
case DialogTurnStatus.Waiting:
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
// Here's where we show welcome messages
if (activity.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded != null)
{
foreach (var member in activity.MembersAdded)
{
// This makes sure the new user isn't the bot. It's a little different from some of the samples
// because code has changed in the SDK and emulator
if (member.Name != "Bot" && member.Name != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
await dc.BeginDialogAsync(ReservationDialog, cancellationToken);
}
}
}
}
// Be sure you're saving ConversationState at the end since DialogContext derives from it
await _conversationState.SaveChangesAsync(turnContext);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)