如何使用 Office 365 服务通信从消息中心获取更新的消息 API

How to get Updated Messages from Message Center using the Office 365 Service Communications API

我正在寻找一种高效方法来根据屏幕截图从消息中心获取(更新)消息。

使用 Office 365 服务通信 API 很简单,但是根据 documentation 我们不能过滤,也不能按 [=12 排序=].据我所知,检测更新消息的唯一可靠方法是比较 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.",

如果无法为 LastUpdateTime desc 设置 $orderby ODATA 过滤器,则无法有效地获取更新消息。它需要加载 所有 消息并在客户端进行过滤。

有数百条消息,StartTimeLastUpdateTime 之间的潜在时间段未知,所以我什至无法过滤到一个子集(例如 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 Core 模块中,您可以在 Github.

上找到它