如何在FormFlow中的消息到达识别器之前对其进行拦截? (枚举用法) [英] How do I intercept a message in FormFlow before it reaches recognizers? (enum usage)

查看:86
本文介绍了如何在FormFlow中的消息到达识别器之前对其进行拦截? (枚举用法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户不喜欢列表中的任何选项,我想截取用户写的内容.我的代码如下,但是仅当用户选择一个选项时,validate函数才起作用.

I would like to intercept what the user writes if he doesn't like any option in the list. My code is the following, but the validate function works only if the user chooses an option.

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace BotApplication.App_Code
{
    public enum MainOptions { AccessoAreaRiservata = 1, AcquistoNuovaPolizza, RinnovoPolizza, Documenti, StatoPratica, AltroArgomento }

    [Serializable]
    public class MainReq
    {
        [Prompt("Indicare la tipologia della richiesta? {||}")]
        public MainOptions? MainOption;

        public static IForm<MainReq> BuildForm()
        {
            var form = (new FormBuilder<MainReq>()

                .Field(nameof(MainOption),validate: async (state, response) =>
                        {
                            var result = new ValidateResult { IsValid = true };
                          {
                            string risposta = (response.ToString());
                            if (risposta  == "AltroArgomento")
                            {
                                result.Feedback = "it works only if user choose an option";
                                result.IsValid = true;
                            }
                            return result;
                          }
                        })
                .Build()); 
            return form;
        }
    }
}

推荐答案

有几种可能的解决方法供您考虑.通常,如果您要考虑用户要提问或说与表格无关的情况,可以让他们使用

There are a few possible workarounds for you to consider. Normally if you want to account for situations where a user wants to ask a question or say something unrelated to the form, you'd have them cancel the form using the Quit command. If you want your bot to be smart enough to interpret when users change the subject in the middle of a form, that's a bit more advanced.

如果要继续使用validate方法,可以将MainOption字段更改为string而不是MainOptions?,以便将所有用户输入发送到validate方法,但随后需要生成自己选择的列表.

If you want to keep using a validate method, you can change your MainOption field to a string instead of a MainOptions? so that all user input gets sent to the validate method, but then you'd need to generate the list of choices yourself.

我的建议是使用自定义提示器而不是验证方法.我写了一个博客文章,其中详细介绍了如何制作这样的提示.首先,您将提供一个

My recommendation is to use a custom prompter instead of a validate method. I've written a blog post that details how to make such a prompter. First you would provide a NotUnderstood template to indicate to your prompter when a message isn't a valid option in FormFlow. Then in the prompter you would call your QnAMaker dialog or do whatever you want with the message.

// Define your NotUnderstood template
[Serializable, Template(TemplateUsage.NotUnderstood, NOT_UNDERSTOOD)]
public class MainReq
{
    public const string NOT_UNDERSTOOD = "Not-understood message";

    [Prompt("Indicare la tipologia della richiesta? {||}")]
    public MainOptions? MainOption;

    public static IForm<MainReq> BuildForm()
    {
        var form = (new FormBuilder<MainReq>()
            .Prompter(PromptAsync)  // Build your form with a custom prompter
            .Build());

        return form;
    }

    private static async Task<FormPrompt> PromptAsync(IDialogContext context, FormPrompt prompt, MainReq state, IField<MainReq> field)
    {
        var preamble = context.MakeMessage();
        var promptMessage = context.MakeMessage();

        if (prompt.GenerateMessages(preamble, promptMessage))
        {
            await context.PostAsync(preamble);
        }

        // Here is where we've made a change to the default prompter.
        if (promptMessage.Text == NOT_UNDERSTOOD)
        {
            // Access the message the user typed with context.Activity
            await context.PostAsync($"Do what you want with the message: {context.Activity.AsMessageActivity()?.Text}");
        }
        else
        {
            await context.PostAsync(promptMessage);
        }

        return prompt;
    }
}

这篇关于如何在FormFlow中的消息到达识别器之前对其进行拦截? (枚举用法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆