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

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

这将显示"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

另一种方法是,当使用 ImBackMessageBack 执行操作时,在 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/

相关文章:

c# - DataGrid.ColumnHeaderStyle 和命令绑定(bind)

c# - 使用 foreach 迭代时如何知道行索引?

asp.net-core - 使用 Bot 框架在 Microsoft Teams 上发送主动消息

c# - ASP.NET Web 应用程序消息框

c# - 使用 Sitecore Webforms For Marketers 生成摘要报告

C# 文件为空,创建实例时崩溃。

azure - Microsoft Bot Framework - 响应时间非常长

botframework - 在聊天机器人中输入指示符

node.js - 如何在 Microsoft bot 框架中为每个用户进行自定义对话更新 - 新用户消息传递?

node.js - Azure Bot Framework 的在线代码编辑器 - 接收错误 : destructure property 'applicationID' of 'undefined' or 'null'