首页 > 解决方案 > 在 bot 框架中匹配日期并在 Azure Function 中触发

问题描述

我正在做一个提醒功能。并通过调用通知 API 的 Get 方法在 Azure 函数中触发它,并将时间触发器设置为每分钟。我需要编写有关时区的代码吗?我认为 azure 使用 UTC 而我的时区是 GMT+8

当我将代码放在这样的 if 日期匹配块之外时,它正在工作。

 foreach (var reminder in userstate.Reminders)
        {
            await turnContext.SendActivityAsync($"Reminding you to {reminder.Subject}");

            if (reminder.DateAndTime == DateTime.Now)
            {

            }
        }

但是当我把它放进去时它不会触发。

    namespace SabikoBotV2.Controllers
    {
    [Route("api/notify")]

    public class NotifyController : ControllerBase
    {
        private readonly IBotFrameworkHttpAdapter _adapter;
        private readonly string _appId;
        private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;
        private readonly IStatePropertyAccessor<BasicUserState> _userProfileAccessor;

        public NotifyController(UserState userState, IBotFrameworkHttpAdapter adapter, ICredentialProvider credentials, ConcurrentDictionary<string, ConversationReference> conversationReferences)
        {
            _userProfileAccessor = userState.CreateProperty<BasicUserState>("UserProfile");

            _adapter = adapter;
            _conversationReferences = conversationReferences;
            _appId = ((SimpleCredentialProvider)credentials).AppId;

            if (string.IsNullOrEmpty(_appId))
            {
                _appId = Guid.NewGuid().ToString(); //if no AppId, use a random Guid
            }
        }

        public async Task<IActionResult> Get()
        {

            try
            {
                foreach (var conversationReference in _conversationReferences.Values)
                {
                    await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));

                }

                return new ContentResult()
                {
                    Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
                    ContentType = "text/html",
                    StatusCode = (int)HttpStatusCode.OK,
                };
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            var userstate = await _userProfileAccessor.GetAsync(turnContext, () => new BasicUserState(), cancellationToken);

            if(userstate.Reminders != null)
            {
                foreach (var reminder in userstate.Reminders)
                {
                    if (reminder.DateAndTime == DateTime.Now)
                    {
                        await turnContext.SendActivityAsync($"Reminding you to {reminder.Subject}");
                    }
                }
            }
        }
    }
}

天青功能

enter image description here

标签: datetimebotframeworkazure-functions

解决方案


这里的主要问题可能是您的if支票:if (reminder.DateAndTime == DateTime.Now)

DateTimes 仅在严格相等时才相等,因此请记住,例如“2019-10-09 13:28:55.26566”不同于“2019-10-09 13:28:55.11876”。

在这里,您的日期检查必须基于与设置提醒的频率相比具有逻辑性的粒度。

If the granularity is by minute, then when your code is triggered, compare your reminder date (year + month + day + hour + minute) and the current date (year + month + day + hour + minute).

You can do this check by several ways, for example:

  • Comparing differences in minutes: reminder.DateAndTime.Subtract(DateTime.Now) <= TimeSpan.FromMinutes(1)
  • Comparing dates in string format
  • ...

推荐阅读