主要问题
我在 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);
}
当前错误抛出 我目前遇到了这个问题,但找不到解决方案:
最佳答案
来自 DatePicker 的日期值将作为 Activity.Value 上的 JObject 进入机器人
以下代码将从 .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/
关于botframework - 自适应卡片 - DateInput 和 SubmitAction,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50159360/