当前位置 : 主页 > 编程语言 > c语言 >

c# – 更改Microsoft Bot Framework中的消息流

来源:互联网 收集:自由互联 发布时间:2021-06-25
你好我是微软Bot框架的新手,我有一个问题,我找不到答案. 我有一个FormFlow,询问用户一些问题,在一个特定问题后,我希望机器人做一些逻辑并相应地显示消息(例如,如果用户选择选项1然后
你好我是微软Bot框架的新手,我有一个问题,我找不到答案.
我有一个FormFlow,询问用户一些问题,在一个特定问题后,我希望机器人做一些逻辑并相应地显示消息(例如,如果用户选择选项1然后显示消息X,如果用户选择选项2显示消息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的建议之后更新了代码:

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

在此先感谢您的帮助

这是一个很好的问题.关键是使用 Field<T>类的SetActive和SetNext方法.你应该考虑使用 FieldReflector课程;虽然你可以实现自己的IField.

SetFctive在FormFlow文档的Dynamic Fields section中描述.基本上它提供了一个基于条件启用字段的委托.

SetNext将允许您根据自定义逻辑决定下一步表单的哪个步骤.

您可以查看ContosoFlowers样本.在Order表格中;正在做类似的事情.

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();
        }
    }
}
网友评论