microsoft bot框架类型指示器表单流程(表单生成器)

时间:2018-03-16 21:59:52

标签: frameworks botframework bots formflow

我需要在表单流中添加键入指示符活动,我使用了以下代码,但它只在表单流的一侧工作,一旦用户进入表单构建器,键入指示符就不会出现。

 Activity replytyping1 = activity.CreateReply();
        replytyping1.Type = ActivityTypes.Typing;
        replytyping1.Text = null;
        ConnectorClient connector2 = new ConnectorClient(new Uri(activity.ServiceUrl));
        await connector2.Conversations.ReplyToActivityAsync(replytyping1);

我在对话框中使用以下代码来调用表单构建器:

 var myform = new FormDialog<TrainingForm>(new TrainingForm(), TrainingForm.MYBuildForm, FormOptions.PromptInStart, null);
            context.Call<TrainingForm>(myform, AfterChildDialog);

我的表单构建器代码:

  public enum MoreHelp { Yes, No };
public enum Helpfull { Yes, No };

[Serializable]
public class TrainingForm
{

    public string More = string.Empty;
    public string usefull = string.Empty;
    [Prompt("Is there anything else I can help you with today? {||}")]
    [Template(TemplateUsage.NotUnderstood, "What does \"{0}\" mean?", ChoiceStyle = ChoiceStyleOptions.Auto)]
    public MoreHelp? needMoreHelp { get; set; }

    [Prompt("Was this helpful? {||}")]
    [Template(TemplateUsage.NotUnderstood, "What does \"{0}\" mean?", ChoiceStyle = ChoiceStyleOptions.Auto)]
    public Helpfull? WasHelpful { get; set; }

    public static IForm<TrainingForm> MYBuildForm()
    {

        return new FormBuilder<TrainingForm>()
            .Field(new FieldReflector<TrainingForm>(nameof(needMoreHelp))
                        .SetActive(state => true)
                        .SetNext(SetNext2).SetIsNullable(false))

            .Field(new FieldReflector<TrainingForm>(nameof(WasHelpful))
                        .SetActive(state => state.More.Contains("No"))
                        .SetNext(SetNext).SetIsNullable(false)).OnCompletion(async (context, state) =>
                        {
                            if (state.usefull == "No")
                            {
                                await context.PostAsync("Sorry I could not help you");
                            }
                            else if (state.usefull == "Yes")
                            {
                                await context.PostAsync("Glad I could help");

                            }

                            if(state.More == "Yes")
                            {
                                await context.PostAsync("Ok! How can I help?");

                            }

                            context.Done<object>(new object());

                        })                 
                .Build();
        }

1 个答案:

答案 0 :(得分:1)

如果您尝试从加载FormFlow对话框的对话框发送键入活动,则它将无法工作,因为每次加载FormFlow对话框时,父对话框中的代码都不会执行。

但是,您可以修改MessagesController并检查对话框堆栈。如果FormFlow对话框是堆栈上的最后一个对话框,则发送键入:

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity) {
        if (activity.Type == ActivityTypes.Message)
        {
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
            {
                var botData = scope.Resolve<IBotData>();
                await botData.LoadAsync(default(CancellationToken));

                var stack = scope.Resolve<IDialogTask>();                    
                if (stack.Frames != null && stack.Frames.Count > 0)
                {
                    var lastFrame = stack.Frames[stack.Frames.Count - 1];
                    var frameValue = lastFrame.Target.GetType().GetFields()[0].GetValue(lastFrame.Target);
                    if(frameValue is FormDialog<TrainingForm>)
                    {
                        var typingReply = activity.CreateReply();
                        typingReply.Type = ActivityTypes.Typing;

                        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                        await connector.Conversations.ReplyToActivityAsync(typingReply);            

                    }
                }
            }

            await Conversation.SendAsync(activity, () => FormDialog.FromForm(TrainingForm.MYBuildForm));
        }
        else
        {
            this.HandleSystemMessage(activity);
        }

        return Request.CreateResponse(HttpStatusCode.OK); 
}
相关问题