我可以接受不在选项列表中的Prompt.Choice中的文本吗?

时间:2018-01-16 21:28:37

标签: c# botframework

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");
        }
    }
}

这将显示是和否,它只接受是或否。 有没有办法接受我可以自己处理的特殊令牌(如HELP,RESET,BACK)?

表格对话框如何始终处理HELP,BACK,QUIT等

这是我的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;
    }
}

3 个答案:

答案 0 :(得分:3)

这可能是使用Scorables的绝佳机会。这样,无论您的用户在对话框中的哪个位置,您都可以拦截并按照&#34; quit&#34;等命令进行操作。或&#34;帮助&#34;。有一个伟大的video here

另一种方法是,如果在使用Activity.TextImBack使用MessageBack 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; } 时使用switch语句或if语句来执行此操作,则根据您的用户类型代码示例执行以下操作:

MessageReceivedAsync

以下代码是&#34;工作&#34;但我不确定你的目标是什么。我会先将提示移出[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; } } 。无论如何这里是代码:

saveButtonClick = () =>{
let config = new MatSnackBarConfig();
config.duration = 5000;
config.panelClass = ['red-snackbar']
this.snackBar.open("This is a message!", "ACTION", config);

答案 1 :(得分:1)

您应该在对话框中处理此问题,因为您想要更改对话框的流程。因此,您可以通过调整用户的文本来控制对话框。

答案 2 :(得分:0)

根据我与JasonSowers的讨论,简短的回答是否定的。什么工作是创建一个可重用的Dialog(按钮的ThumbnailCards)和拦截用户输入MessageReceived()。如果输入是保留令牌,则执行:context.Done(userInput)并在对话框外处理它。