我在做一个提醒功能。并通过调用通知api的Get方法,在Azure函数中触发时间触发设置为每分钟。我需要对时区进行编码吗?我认为azure在使用UTC,而我的时区是GMT+8。
当我将代码放在if date matches块之外时,它就可以这样工作了。
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}");
}
}
}
}
}
}
Azure函数
发布于 2019-10-09 03: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)
中的日期
https://stackoverflow.com/questions/58301369
复制