如何在 Bot Framework C# 中在运行时生成瀑布步骤?
How to generate waterfall steps at runtime in Bot Framework C#?
我有一个由 Bot Framework v4 制作的聊天机器人,我正在阅读机器人响应和他想问用户的问题。
这是一个单独的文件:
BotQuestions.cs
public class BotQuestions{
public string Intro = "Welcome to Chat Session! I am Mr. A, your assistant.";
public string AskFood = "How was your experience with our food?";
public string Acknowledge = "I am glad that you liked our food!";
public string Sad = "We apologize that you didn't enjoy our food. We will take care of it next time";
}
然后我在定义 WaterFall 步骤的 Bot class 中调用此 class。
ChatBotDialog.cs
public class ChatBotDialog : CancelAndHelpDialog
{
public static BotQuestions question = new BotQuestions();
public ChatBotDialog(UserState userState, ConversationState conversationState) :
base(nameof(ChatBotDialog))
{
memoryStorage = new MemoryStorage();
_conversationState = conversationState;
// the waterfall method to maintain the order of the chat
var waterfallSteps = new WaterfallStep[]
{
IntroStepAsync,
AskFoodStepAsync,
AckStepAsync,
SadAsync
};
// adding named dialogs to the Dialog Set. These names are saved in dialog set
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
AddDialog(new TextPrompt(nameof(TextPrompt)));
// run the initial child dialog
InitialDialogId = nameof(WaterfallDialog);
}
private static async Task<DialogTurnResult> IntroStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
Activity reply = stepContext.Context.Activity.CreateReply();
reply.Type = ActivityTypes.Typing;
ConnectorClient connector = new ConnectorClient(new Uri(stepContext.Context.Activity.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
//BotReplyTime();
var promptOptions = new PromptOptions
{
Prompt = MessageFactory.Text(questions.Intro) // here I am accessing the Bot Question class string property and its value.
};
return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
}
private static async Task<DialogTurnResult> AskFoodStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
private static async Task<DialogTurnResult> AcknowledgeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
private static async Task<DialogTurnResult> SadStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
}
如您所见,ChatBotDialog 有瀑布式步骤,每个步骤都调用 BotQuestions class 来访问字符串值。
现在的情况是如果我想在BotQuestions.cs中添加一个新问题,我必须生成字符串值,生成相应的瀑布步骤,然后 运行 看起来非常笨拙的对话框...那么有没有办法在 运行 时间内动态生成瀑布步骤? (仅在 C# 中)如果我在 BotQuestions.cs 之间的任何位置添加一个新问题,机器人是否可以检测到相应地改变和调整它?这可能吗?
无法动态添加瀑布步骤。
我花了一秒钟才明白你在做什么。我不建议您根据向用户提出的一系列问题来构建您的瀑布,而是根据预期的用户输入(即 "I need my user's shoes size, address and date of birth")来构建它。
我建议您查看新的 Bot Framework Composer。它是一个基于 UI 的机器人创建工具,可围绕对话构建机器人(与您正在做的没什么不同)。
我有一个由 Bot Framework v4 制作的聊天机器人,我正在阅读机器人响应和他想问用户的问题。
这是一个单独的文件:
BotQuestions.cs
public class BotQuestions{
public string Intro = "Welcome to Chat Session! I am Mr. A, your assistant.";
public string AskFood = "How was your experience with our food?";
public string Acknowledge = "I am glad that you liked our food!";
public string Sad = "We apologize that you didn't enjoy our food. We will take care of it next time";
}
然后我在定义 WaterFall 步骤的 Bot class 中调用此 class。
ChatBotDialog.cs
public class ChatBotDialog : CancelAndHelpDialog
{
public static BotQuestions question = new BotQuestions();
public ChatBotDialog(UserState userState, ConversationState conversationState) :
base(nameof(ChatBotDialog))
{
memoryStorage = new MemoryStorage();
_conversationState = conversationState;
// the waterfall method to maintain the order of the chat
var waterfallSteps = new WaterfallStep[]
{
IntroStepAsync,
AskFoodStepAsync,
AckStepAsync,
SadAsync
};
// adding named dialogs to the Dialog Set. These names are saved in dialog set
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
AddDialog(new TextPrompt(nameof(TextPrompt)));
// run the initial child dialog
InitialDialogId = nameof(WaterfallDialog);
}
private static async Task<DialogTurnResult> IntroStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
Activity reply = stepContext.Context.Activity.CreateReply();
reply.Type = ActivityTypes.Typing;
ConnectorClient connector = new ConnectorClient(new Uri(stepContext.Context.Activity.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
//BotReplyTime();
var promptOptions = new PromptOptions
{
Prompt = MessageFactory.Text(questions.Intro) // here I am accessing the Bot Question class string property and its value.
};
return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
}
private static async Task<DialogTurnResult> AskFoodStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
private static async Task<DialogTurnResult> AcknowledgeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
private static async Task<DialogTurnResult> SadStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken){
// similar logic
}
}
如您所见,ChatBotDialog 有瀑布式步骤,每个步骤都调用 BotQuestions class 来访问字符串值。
现在的情况是如果我想在BotQuestions.cs中添加一个新问题,我必须生成字符串值,生成相应的瀑布步骤,然后 运行 看起来非常笨拙的对话框...那么有没有办法在 运行 时间内动态生成瀑布步骤? (仅在 C# 中)如果我在 BotQuestions.cs 之间的任何位置添加一个新问题,机器人是否可以检测到相应地改变和调整它?这可能吗?
无法动态添加瀑布步骤。
我花了一秒钟才明白你在做什么。我不建议您根据向用户提出的一系列问题来构建您的瀑布,而是根据预期的用户输入(即 "I need my user's shoes size, address and date of birth")来构建它。
我建议您查看新的 Bot Framework Composer。它是一个基于 UI 的机器人创建工具,可围绕对话构建机器人(与您正在做的没什么不同)。