如何在azure botframework v4中获得日期提示符的结果?这会导致错误。我甚至尝试了评论的那些。还是有更好的方式来过生日呢?就像日期时间选择器一样?
public class GetNameAndAgeDialog : WaterfallDialog
{
public GetNameAndAgeDialog(string dialogId, IEnumerable<WaterfallStep> steps = null)
: base(dialogId, steps)
{
AddStep(async (stepContext, cancellationToken) =>
{
return await stepContext.PromptAsync(
"datePrompt",
new PromptOptions
{
Prompt = MessageFactory.Text("When is your birthday?"),
},
cancellationToken: cancellationToken);
});
AddStep(async (stepContext, cancellationToken) =>
{
// var bday = (stepContext.Result as DateTimeResolution).Value;
// var bday = Convert.ToDateTime(stepContext.Result);
var bday = (DateTime)stepContext.Result;
await stepContext.Context.SendActivityAsync($"{bday}");
return await stepContext.NextAsync();
});
}
}我添加了如下内容:
_dialogs.Add(new DateTimePrompt("datePrompt"));我如何验证这一点呢?我只需要日期来计算年龄。
发布于 2019-01-15 01:12:58
不幸的是,对话框/提示符API中缺少强类型使得这一点很难被发现,但DateTimePrompt的结果实际上是一个IList<DateTimeResolution>。
发布于 2019-01-15 05:36:08
这是我用来作为日期时间提示的东西。它使用Microsoft.Recognizers.Text.DataTypes.TimexExpression库。
public class CustomDateTimePrompt : ComponentDialog
{
public CustomDateTimePrompt(string dialogId)
: base(dialogId)
{
WaterfallStep[] waterfallSteps = new WaterfallStep[]
{
InitializeAsync,
ResultRecievedAsync
};
AddDialog(new WaterfallDialog("customDateTimePromptWaterfall", waterfallSteps));
AddDialog(new DateTimePrompt("datetime", ValidateDateTime));
}
private Task<DialogTurnResult> InitializeAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
if (stepContext.Options is PromptOptions options)
{
if (options.Prompt == null)
{
throw new ArgumentNullException(nameof(options.Prompt));
}
return stepContext.PromptAsync("datetime", options, cancellationToken);
}
else
{
throw new ArgumentException("Options must be prompt options");
}
}
private Task<DialogTurnResult> ResultRecievedAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
if (stepContext.Result is IList<DateTimeResolution> datetimes)
{
DateTime time = TimexHelper.GetDateTime(datetimes.First().Timex);
return stepContext.EndDialogAsync(time, cancellationToken);
}
else
{
throw new InvalidOperationException("Result is not datetimes");
}
}
private Task<bool> ValidateDateTime(PromptValidatorContext<IList<DateTimeResolution>> promptContext, CancellationToken cancellationToken)
{
IList<DateTimeResolution> results = promptContext.Recognized.Value;
if (results != null)
{
results = results.Where(r => !string.IsNullOrEmpty(r.Timex) && TimexHelper.IsDateTime(r.Timex)).ToList();
return Task.FromResult(results.Count > 0);
}
else
{
return Task.FromResult(false);
}
}
}
public static class TimexHelper
{
public static bool IsDateTime(string timex)
{
TimexProperty timexProperty = new TimexProperty(timex);
return timexProperty.Types.Contains(Constants.TimexTypes.Date) || timexProperty.Types.Contains(Constants.TimexTypes.Time);
}
public static bool IsTimeRange(string timex)
{
TimexProperty timexProperty = new TimexProperty(timex);
return timexProperty.Types.Contains(Constants.TimexTypes.TimeRange) || timexProperty.Types.Contains(Constants.TimexTypes.DateTimeRange);
}
public static DateTime GetDateTime(string timex)
{
TimexProperty timexProperty = new TimexProperty(timex);
DateTime today = DateTime.Today;
int year = timexProperty.Year ?? today.Year;
int month = timexProperty.Month ?? today.Month;
int day = timexProperty.DayOfMonth ?? today.Day;
int hour = timexProperty.Hour ?? 0;
int minute = timexProperty.Minute ?? 0;
DateTime result;
if (timexProperty.DayOfWeek.HasValue)
{
result = TimexDateHelpers.DateOfNextDay((DayOfWeek)timexProperty.DayOfWeek.Value, today);
result = result.AddHours(hour);
result = result.AddMinutes(minute);
}
else
{
result = new DateTime(year, month, day, hour, minute, 0);
if (result < today)
{
result = result.AddYears(1);
}
}
return result;
}
}https://stackoverflow.com/questions/54179009
复制相似问题