首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >bot框架中的匹配日期与Azure函数中的触发

bot框架中的匹配日期与Azure函数中的触发
EN

Stack Overflow用户
提问于 2019-10-09 09:45:31
回答 1查看 49关注 0票数 0

我在做一个提醒功能。并通过调用通知api的Get方法,在Azure函数中触发时间触发设置为每分钟。我需要对时区进行编码吗?我认为azure在使用UTC,而我的时区是GMT+8。

当我将代码放在if date matches块之外时,它就可以这样工作了。

代码语言:javascript
代码运行次数:0
运行
复制
 foreach (var reminder in userstate.Reminders)
        {
            await turnContext.SendActivityAsync($"Reminding you to {reminder.Subject}");

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

            }
        }

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

代码语言:javascript
代码运行次数:0
运行
复制
    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}");
                    }
                }
            }
        }
    }
}

Azure函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-09 11:34:04

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

DateTimes只有在它们严格相等的情况下才等于,所以请记住"2019-10-09 13:28:55.26566“与"2019-10-09 13:28:55.11876”不同。

在这里,您的日期检查必须根据粒度进行,这与设置提醒的频率相比是合乎逻辑的。

如果粒度是按分钟计算的,那么在触发代码时,比较提醒日期(年份+月+日+小时+小时+分钟)和当前日期(年份+月+日+小时+分钟)。

您可以通过多种方式进行此检查,例如:

以分钟为单位比较差异的reminder.DateAndTime.Subtract(DateTime.Now) <= TimeSpan.FromMinutes(1)

  • Comparing

  • :字符串format

  • ...

中的日期

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58301369

复制
相关文章

相似问题

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