public async Task StartAsync(IDialogContext context)
{
PromptDialog.Choice(context,
this.ResumeAfter,
options: new string[] { "Yes", "No" },
prompt: "Are you ready to continue?",
retry: "Not a valid option",
attempts: 3);
}
public async Task ResumeAfter(IDialogContext context, IAwaitable<string> argument)
{
var message = await argument;
//this sets bool-proceed to false if message is QUIT, for example
await ProcessTextMessage(context, message);
if (proceed)
{
bool result;
if (bool.TryParse(message, out result) && result)
{
//do stuff for YES
}
else
{
//this is NO
await context.PostAsync("What else can I help you with?");
context.Done("DONE");
}
}
}
这将显示"is"和“否”,并且仅接受"is"或“否”。 有没有办法接受特殊 token (例如 HELP、RESET、BACK),然后我可以自己处理这些 token ?
例如表单对话框如何始终处理帮助、返回、退出等。
这是我的 ProcessTextMessage 方法:
private async Task ProcessTextMessage(IDialogContext context, string message)
{
switch (message)
{
case "QUIT":
proceed = false;
await context.PostAsync($"**QUIT** Application was triggered. What else can I help you with today?");
context.Done("QUIT");
break;
case "RESET":
proceed = false;
await context.PostAsync($"**RESET** Application was triggered.");
await StartAsync(context);
break;
case "HELP":
await context.PostAsync($"Some other actions you can use: **QUIT**, **RESET**, **BACK**.");
break;
case "BACK":
switch (CurrentState)
{
//in progress
//calls previous method
}
break;
default:
break;
}
}
最佳答案
这可能是使用 Scorables 的绝佳机会。这样,无论您的用户位于对话框中的哪个位置,您都可以拦截并执行“退出”或“帮助”等命令。有一个很棒的video here 。
另一种方法是,当使用 ImBack
或 MessageBack
执行操作时,在 Activity.Text
上使用 switch 语句或 if 语句不同的事情取决于您的用户输入的内容,代码示例如下:
var message = await result as Activity;
switch (message.Text.ToLower())
{
case "yes":
//do yes stuff
break;
case "no":
//do no stuff
break;
case "quit":
//do quit stuff
break;
case "help":
//do help stuff
break;
}
下面的代码“有效”,但我不确定您的目标是什么。我首先将提示移出 MessageReceivedAsync
。无论如何,这是代码:
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private bool proceed;
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
PromptDialog.Choice(context,
this.ResumeAfter,
options: new string[] { "Yes", "No" },
prompt: "Are you ready to continue?",
retry: "Not a valid option",
attempts: 3);
}
public async Task ResumeAfter(IDialogContext context, IAwaitable<string> argument)
{
var message = await argument;
//this sets bool-proceed to false if message is QUIT, for example
await ProcessTextMessage(context, message);
bool proceed =true;
if (proceed)
{
bool result;
if (bool.TryParse(message, out result) && result)
{
//do stuff for YES
}
else
{
//this is NO
await context.PostAsync("What else can I help you with?");
context.Done("DONE");
}
}
}
private async Task ProcessTextMessage(IDialogContext context, string message)
{
switch (message)
{
case "QUIT":
proceed = false;
await context.PostAsync($"**QUIT** Application was triggered. What else can I help you with today?");
context.Done("QUIT");
break;
case "RESET":
proceed = false;
await context.PostAsync($"**RESET** Application was triggered.");
await StartAsync(context);
break;
case "HELP":
await context.PostAsync($"Some other actions you can use: **QUIT**, **RESET**, **BACK**.");
break;
case "BACK":
break;
default:
break;
}
}
关于c# - 我可以接受 Prompt.Choice 中不在选项列表中的文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290111/