gpt4 book ai didi

c# - 将变量传递给 Microsoft BotBuilder C# .NET Core 中的对话框

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:10 26 4
gpt4 key购买 nike

我正在尝试使用这个 example 构建我的机器人.

现在的流程:

  1. 用户写了一些东西 foo
  2. 机器人进入对话...

我坚持在 dialog 中获取用户的第一条消息 foo .

最佳答案

您可以像这样从 turnContext 获取用户输入:

string userInput = turnContext.Context.Activity.Text

例子:

 public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
string userInput = turnContext.Activity.Text;
}

至于将变量传递给UserProfileDialog,您可以通过以下方式进行:

await innerDc.BeginDialogAsync(nameof(DialogFlowDialog), userInput );

BeginDialogAsync 接受一个可选参数(对象)以传递给正在启动的对话框。

在您的 UserProfileDialog 中,您可以从 stepContext

获取该参数

例子:

 private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
string userInput = string.Empty;

// Options contains the information the waterfall dialog was called with
if (stepContext.Options != null)
{
userInput = stepContext.Options.ToString();
}
}

如果你想获取用户发送的第一条消息,你总是可以从上下文中获取它,如果你使用的是05.multi-turn-prompt你可以通过这种方式在里面获取你的 UserProfileDialog

   private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// this contains the text message the user sent
string userInput = stepContext.Context.Activity.Text;
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
// Running a prompt here means the next WaterfallStep will be run when the users response is received.
return await stepContext.PromptAsync(nameof(ChoicePrompt),
new PromptOptions
{
Prompt = MessageFactory.Text("Please enter your mode of transport."),
Choices = ChoiceFactory.ToChoices(new List<string> { "Car", "Bus", "Bicycle" }),
}, cancellationToken);
}

或者在你的 DialogBot

中像这样
  public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{

// this contains the text message the user sent
string userInput = turnContext.Activity.Text;

await base.OnTurnAsync(turnContext, cancellationToken);

// Save any state changes that might have occured during the turn.
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
}

关于c# - 将变量传递给 Microsoft BotBuilder C# .NET Core 中的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58317045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com