office365 - 如何使用 Office 365 服务通信 API 从消息中心获取更新的消息
问题描述
我正在寻找一种根据屏幕截图从消息中心获取(更新)消息的有效方法。
使用Office 365 服务通信 API很简单,但根据文档,我们不能过滤也不能按LastUpdatedTime
. 据我所知,检测更新消息的唯一可靠方法是比较StartTime < LastUpdatedTime
.
尝试按LastUpdateTime
结果排序会产生预期的错误消息并与文档匹配。
"message": "The query specified in the URI is not valid. Order by 'LastUpdatedTime' is not allowed. To allow it, set the 'AllowedOrderByProperties' property on EnableQueryAttribute or QueryValidationSettings.",
如果不能为 设置$orderby
ODATA 过滤器LastUpdateTime desc
,就没有有效的方法来获取更新的消息。它需要加载所有消息并在客户端进行过滤。
有数百条消息,并且两者之间的潜在时间段StartTime
未知LastUpdateTime
,因此我什至无法过滤到子集(例如StartTime gt 7 days
)。必须始终处理每条消息,以确保不会错过更新的消息。
我希望收到与消息中心本身和屏幕截图完全相同的消息表示和顺序(更新和未更新)。
我错过了更好的选择吗?
解决方案
由于社区没有给出答案,我也无法在文档中找到任何提示,因此我通过观察实现了自己的客户端逻辑。
解决方案
# Get all Messages from Message Center
function Get-ServiceCommMessages {
[CmdletBinding()]
param (
$DaysToSync
)
# Invoke-ServiceCommMethod is just a wrapper function for Invoke-WebRequest and Access Token acquisition.
# https://github.com/sebastianzolg/PSServiceComm
$messages = Invoke-ServiceCommMethod -Method '/ServiceComms/Messages' | Sort-Object LastUpdatedTime -Descending
If($PSBoundParameters.ContainsKey('DaysToSync')){
$messages = $messages | Where-Object LastUpdatedTime -ge (Get-Date).AddDays(-$daysToSync).Date
}
$messages | Add-Member -MemberType ScriptMethod -Name "IsUpdated" -Value {
($this.LastUpdatedTime -ge ($this.StartTime).AddHours(1)) -and ($this.Title -match [Regex]::Escape('(Updated)'))
} -Force
return $messages
}
# Get all messages for the last 5 days and filter for updates
$messages = Get-ServiceCommMessages -DaysToSync 5
$messages | Where-Object { $_.IsUpdated() }
更新被视为满足以下条件的消息:
Title contains '(Updated)'
LastUpdatedTime > StartTime + 1 Hour
我已将逻辑打包到一个非常简单的PowerShell 核心模块中,您可以在Github上找到该模块。