datetime - 在 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}");
}
}
}
}
}
}
天青功能
解决方案
这里的主要问题可能是您的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
- ...
推荐阅读
- r - 我从 Excel 中的日期导入为字符而不是数字
- c# - 如何在 MVVM Light ViewModelLocator 中使用参数实例化服务?
- r - ValueError:在用户代码中:-在 R 中训练模型时出现隐秘的错误消息(调用 model.fit)
- node.js - WebRTC Video Track 到 Node 中的 ffmpeg
- reactjs - 使用 Redux 基于按钮单击添加/删除数组中的对象
- ffmpeg - 无效的流说明符:元数据
- python - Tkinter:为什么下一个“标签”的 0 列会有所不同?
- ios - UICollectionViewCompositionalLayout - 具有fractionalWidth 宽度和自定尺寸单元格的意外布局
- python - github get_organization 给出 Not Found Error github.GithubException.UnknownObjectException: 404
- visual-studio-code - 使用 haxe 在 Visual Studio Code 中运行和调试问题