首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Botframework V4: Botframework未获得结果

Botframework V4: Botframework未获得结果
EN

Stack Overflow用户
提问于 2019-01-14 17:47:33
回答 2查看 891关注 0票数 1

如何在azure botframework v4中获得日期提示符的结果?这会导致错误。我甚至尝试了评论的那些。还是有更好的方式来过生日呢?就像日期时间选择器一样?

代码语言:javascript
复制
 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();

            });
        }
}

我添加了如下内容:

代码语言:javascript
复制
_dialogs.Add(new DateTimePrompt("datePrompt"));

我如何验证这一点呢?我只需要日期来计算年龄。

EN

回答 2

Stack Overflow用户

发布于 2019-01-15 01:12:58

不幸的是,对话框/提示符API中缺少强类型使得这一点很难被发现,但DateTimePrompt的结果实际上是一个IList<DateTimeResolution>

票数 1
EN

Stack Overflow用户

发布于 2019-01-15 05:36:08

这是我用来作为日期时间提示的东西。它使用Microsoft.Recognizers.Text.DataTypes.TimexExpression库。

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54179009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档