自适应卡 - DateInput和SubmitAction

时间:2018-05-03 15:59:55

标签: botframework adaptive-cards

主要问题

大家好,我在Microsoft Bot Framework中使用自适应卡比较新。我遇到了一个问题,我不知道如何在用户选择他想要的日期后从DateInput获取值。以下是我的代码:

public async Task StartAsync(IDialogContext context)
    {
        string pIdentifier = serviceFactory.ProfileService.GetPatientIdentifier(nric);
        string response = serviceFactory.DentalAppointmentService.GetSlotSearchDates(nric,apptId,caseNumber, institutionCode, pIdentifier);
        string[] split = response.Split(' ');
        DateTime earliestStartDate = DateTime.ParseExact(split[0], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
        DateTime latestEndDate = DateTime.ParseExact(split[1], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);

        await context.PostAsync("Your appointment can only be rescheduled within this period " + earliestStartDate.ToShortDateString() + " to " + latestEndDate.ToShortDateString());



        AdaptiveCard card = new AdaptiveCard();

        card.Body.Add(new TextBlock()
        {
            Text = "Enter new Date",
            Size = TextSize.Large,
            Weight = TextWeight.Bolder
        });

        DateInput input = new DateInput()
        {
            Id = "Date",
            Placeholder = "New Date"
        };

        card.Body.Add(input);

        card.Actions.Add(new SubmitAction()
        {
            Title = "Submit"
        });

        Attachment cardAttachment = new Attachment()
        {
            ContentType = AdaptiveCard.ContentType,
            Content = card
        };

        var message = context.MakeMessage();
        message.Attachments = new List<Attachment>();
        message.Attachments.Add(cardAttachment);

        await context.PostAsync(message);
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var temp = await result as Activity;
        string date = temp.Text;


        //DateTime newDate = DateTime.ParseExact(value.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);

        Debug.WriteLine("Entered Msg received Async:" + date);
        await context.PostAsync(date);
    }

当前错误抛出

我目前遇到此问题,但我无法找到解决方案: 抛出异常:&#39; Microsoft.CSharp.RuntimeBinder.RuntimeBinderException&#39;在mscorlib.dll中

我希望得到任何帮助,我希望从讨论中学习,如果有的话:)

1 个答案:

答案 0 :(得分:3)

DatePicker中日期的值将作为activity.Value上的JObject进入bot。

以下代码将从.Text属性或.Value:

中提取日期
public async Task StartAsync(IDialogContext context)
{
    context.Wait(this.MessageReceivedAsync);
}


public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var temp = await result as Activity;

    DateTime dateTime;
    var datePresent = DateTime.TryParse(temp.Text, out dateTime);
    if (!datePresent && temp.Value != null)
    {
        var jObjectValue = temp.Value as JObject;

        var dateAsString = jObjectValue.Value<string>("Date");
        if (!string.IsNullOrEmpty(dateAsString))
        {
            dateTime = DateTime.ParseExact(dateAsString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            datePresent = true;
        }
    }

    if (!datePresent)
    {
        //since the user did not send a date, show the card
        AdaptiveCard card = new AdaptiveCard();

        card.Body.Add(new AdaptiveTextBlock()
        {
            Text = "Enter new Date",
            Size = AdaptiveTextSize.Large,
            Weight = AdaptiveTextWeight.Bolder
        });

        AdaptiveDateInput input = new AdaptiveDateInput()
        {
            Id = "Date",
            Placeholder = "New Date"
        };

        card.Body.Add(input);

        card.Actions.Add(new AdaptiveSubmitAction()
        {
            Title = "Submit"
        });

        Attachment cardAttachment = new Attachment()
        {
            ContentType = AdaptiveCard.ContentType,
            Content = card
        };

        var message = context.MakeMessage();
        message.Attachments = new List<Attachment>();
        message.Attachments.Add(cardAttachment);

        await context.PostAsync(message);
    }
    else
    {
        await context.PostAsync($"Date Entered: {dateTime}");
    }

    context.Wait(this.MessageReceivedAsync);
}

另外,请将您的AdaptiveCards库升级到1.0版:https://www.nuget.org/packages/AdaptiveCards/