更改Microsoft Bot Framework中的消息流

更改Microsoft Bot Framework中的消息流

问题描述:

你好,我是Microsoft Bot Framework的新手,我有一个无法找到答案的问题. 我有一个FormFlow询问用户一个问题,在一个特定问题之后,我希望机器人执行一些逻辑并相应地显示消息(例如,如果用户选择了选项1然后显示了消息X,并且用户选择了选项2则显示了消息是).

Hello I'm new to Microsoft Bot Framework and I have a question that I couldn't find an answer to. I have a FormFlow that ask the user for some question, after a specific question I want the bot to do some logic and show messages accordingly (for example if the user selected option 1 then show message X and if the user selected option 2 show message Y).

这是我的代码:

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

namespace Bot_CRM.FormFlow
{
    public enum RequestOptions { Unknown, CheckStatus, CreateCase };

    [Serializable]
    public class CaseFormFlow
    {
        public RequestOptions RequestType;
        [Prompt("What is your first name?")]
        public string FirstName;
        public string LastName;
        public string ContactNumber;
        [Prompt("Please enter your id")]
        public string Id;

        public static IForm<CaseFormFlow> BuildForm()
        {
            OnCompletionAsyncDelegate<CaseFormFlow> processRequest = async (context, state) =>
            {
                await context.PostAsync($@"Thanks for your request");
            };

            return new FormBuilder<CaseFormFlow>()
                   .Message("Hello and welcom to my service desk bot")
                   .Field(nameof(FirstName))
                   .Message("hello {FirstName}")
                   .Field(nameof(Id))
                   .Field(nameof(RequestType)) => 
//here if user select 1 start flow of check status and if user select 2 start flow of create case
                   .AddRemainingFields()
                   .Message("Thank you request. Our help desk team will get back to you shortly.")
                   .OnCompletion(processRequest)
                   .Build();
        }
    }
}

根据Ezequiel的建议,更新了代码:

Updated code after Ezequiel's suggestion:

 return new FormBuilder<CaseFormFlow>()
               .Message("Hello and welcom to my service desk bot")
               .Field(nameof(FirstName))
               .Message("hello {FirstName}")
               .Field(new FieldReflector<CaseFormFlow>(nameof(RequestType))
                .SetActive(state => state.AskUserForRequestType)
                .SetNext((value, state) =>
                {
                    var selection = (RequestOptions)value;

                    if (selection == RequestOptions.CheckStatus)
                    {

                        return new NextStep(new[] { nameof(Id) });
                    }
                    else
                    {
                        return new NextStep();
                    }
                }))

预先感谢您的帮助

这是一个很好的问题.关键是使用 FieldReflector 类;尽管您可以实现自己的IField.

This is a great question.The key thing is to use the SetActive and SetNext methods of the Field<T> class. You should consider using the FieldReflector class; though you can implement your own IField.

SetActive在动态字段"部分.基本上,它提供了一个根据条件启用该字段的委托.

SetActive is described in the Dynamic Fields section of the FormFlow documentation. Basically it provides a delegate that enables the field based on a condition.

SetNext将使您可以根据自定义逻辑来决定下一步应该进入表单的哪一步.

SetNext will allow you to decide what step of the form should come next based on your custom logic.

您可以查看 ContosoFlowers 一个>样品.在订单形式;正在做类似的事情.

You can take a look to the ContosoFlowers sample. In the Order form; something similar is being done.

 public static IForm<Order> BuildOrderForm()
        {
            return new FormBuilder<Order>()
                .Field(nameof(RecipientFirstName))
                .Field(nameof(RecipientLastName))
                .Field(nameof(RecipientPhoneNumber))
                .Field(nameof(Note))
                .Field(new FieldReflector<Order>(nameof(UseSavedSenderInfo))
                    .SetActive(state => state.AskToUseSavedSenderInfo)
                    .SetNext((value, state) =>
                    {
                        var selection = (UseSaveInfoResponse)value;

                        if (selection == UseSaveInfoResponse.Edit)
                        {
                            state.SenderEmail = null;
                            state.SenderPhoneNumber = null;
                            return new NextStep(new[] { nameof(SenderEmail) });
                        }
                        else
                        {
                            return new NextStep();
                        }
                    }))
                .Field(new FieldReflector<Order>(nameof(SenderEmail))
                    .SetActive(state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                    .SetNext(
                        (value, state) => (state.UseSavedSenderInfo == UseSaveInfoResponse.Edit)
                        ? new NextStep(new[] { nameof(SenderPhoneNumber) })
                        : new NextStep()))
                .Field(nameof(SenderPhoneNumber), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                .Field(nameof(SaveSenderInfo), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                .Build();
        }
    }
}