Botframework V4:DateTimePrompt没有得到结果 [英] Botframework V4: DateTimePrompt not getting result
本文介绍了Botframework V4:DateTimePrompt没有得到结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在azure botframework v4中获得datetime提示的结果?这会导致错误.我什至尝试了那些被评论的.还是有更好的方法来获得生日?像日期时间选择器一样?
How can i get the result of a datetimeprompt in azure botframework v4? This results in an error. I even tried the ones that are commented. Or is there a betterway to get a Birthday? Like a date time picker?
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();
});
}
}
我正在添加这样的内容:
Im adding the like this:
_dialogs.Add(new DateTimePrompt("datePrompt"));
该如何验证呢?我只需要日期即可计算出年龄.
And how can I validate this? I only need the date to compute the Age.
推荐答案
这是我用作日期时间提示的东西.它使用 Microsoft.Recognizers.Text.DataTypes.TimexExpression
库.
This is something I use as a date time prompt. It uses the Microsoft.Recognizers.Text.DataTypes.TimexExpression
library.
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;
}
}
这篇关于Botframework V4:DateTimePrompt没有得到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文