ContinueDialogAsync does not work when I use the i

2020-04-30 06:27发布

问题:

I have been using the EchoBot Template and have recently used the VirtualAssistant Template. In the previous template, there was no problem in submitting the input form data, but this template does not work with ContinueDialogAsync just by submitting the input form data. How can we solve this? Please help me.

input form of WaterfallStep

       card.Body.Add(new AdaptiveTextBlock()
            {
                HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                Spacing = AdaptiveSpacing.None,
                Size = AdaptiveTextSize.Small,
                Weight = AdaptiveTextWeight.Bolder,
                Color = AdaptiveTextColor.Accent,
                Text = Common.PurchaseDialog_DepartureDate
            });

        card.Body.Add(new AdaptiveDateInput()
        {
            Id = "GoDateVal",
            Value = DateTime.Now.AddDays(4).ToString("yyyy-MM-dd"),
            Spacing = AdaptiveSpacing.None
        });

        if (lowestPriceQuery.tripType == "RT")
        {
            card.Body.Add(new AdaptiveTextBlock()
            {
                HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                Spacing = AdaptiveSpacing.None,
                Size = AdaptiveTextSize.Small,
                Weight = AdaptiveTextWeight.Bolder,
                Color = AdaptiveTextColor.Accent,
                Text = Common.PurchaseDialog_CommingDate
            });

            card.Body.Add(new AdaptiveDateInput()
            {
                Id = "ComeDateVal",
                Value = DateTime.Now.AddDays(8).ToString("yyyy-MM-dd"),
                Spacing = AdaptiveSpacing.None,
            });
        }

if (lowestPriceQuery.tripType == "DS")
        {
            card.Body.Add(new AdaptiveTextBlock()
            {
                HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                Spacing = AdaptiveSpacing.None,
                Size = AdaptiveTextSize.Small,
                Weight = AdaptiveTextWeight.Bolder,
                Color = AdaptiveTextColor.Accent,
                Text = "ReturnDate"
            });

            card.Body.Add(new AdaptiveDateInput()
            {
                Id = "ComeDateVal",
                Value = DateTime.Now.AddDays(8).ToString("yyyy-MM-dd"),
                Spacing = AdaptiveSpacing.None,
            });
        }

        card.Actions.Add(new AdaptiveSubmitAction()
        {
            Type = AdaptiveSubmitAction.TypeName,
            Title = "submit",
            Id = "submit",
        });

        reply.Attachments = new List<Attachment>
        {
            new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(card))
            }
        };
        await turnContext.SendActivityAsync(reply, cancellationToken: cancellationToken);

DilaogBot,

When debugging, nothing happens in var result = await dc.ContinueDialogAsync (); when submitting from input form.

var dc = await _dialogs.CreateContextAsync(turnContext);

                if (turnContext.Activity.Type == ActivityTypes.Message)
                {
                    // Ensure that message is a postBack (like a submission from Adaptive Cards)
                    var channelData = JObject.Parse(dc.Context.Activity.ChannelData.ToString());
                    if (channelData.ContainsKey("postback"))
                    {
                        var postbackActivity = dc.Context.Activity;
                        // Convert the user's Adaptive Card input into the input of a Text Prompt
                        // Must be sent as a string
                        postbackActivity.Text = postbackActivity.Value.ToString();
                        await dc.Context.SendActivityAsync(postbackActivity);
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    var result = await dc.ContinueDialogAsync();
                }
                else
                {
                    await dc.BeginDialogAsync(typeof(T).Name);
                }

回答1:

You need to remove:

await dc.Context.SendActivityAsync(postbackActivity);

When the bot sends a message, it marks the dialog as having been responded to and doesn't continue appropriately.

Additionally, your "//Ensure message is a PostBack" code should be more like this:

var activity = turnContext.Activity;

if (activity.Type == ActivityTypes.Message)
{
    if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
    {
        activity.Text = JsonConvert.SerializeObject(activity.Value);
    }
}

I believe you got that code from an old answer of mine, which I have since improved.