获取来自 Microsoft teams 频道的所有消息和回复 - 图形分页
Get all messages and replies from Microsoft teams channel - Graph paging
我正在使用已发布在此网站上的脚本 [https://myteamsday.com/2019/10/08/exporting-team-channel-messages/][1]
将频道消息导出到 OneNote 文件。脚本工作正常,我的问题是我无法向脚本添加分页(以便使用“@odata.nextLink”检索所有消息)我是开发和 MS 图形方面的新手,我知道我需要在代码中添加 if 语句或 While(),但我不确定该怎么做。
这里是脚本读取所有消息和回复并将它们写入 Onenote 文件的地方
$messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
$graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
#go through files and copy them to target
foreach ($message in $graphResponse.value)
{
$messageID = $message.id
$pageHtml = $pageHtml + '<p>' + $message.createdDateTime+" "+ $message.from.user.displayName +" <b>" + $message.subject+"</b>:"+$message.body.content +'</p>'
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages/" + $messageID + "/replies"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
foreach ($reply in $repliesResponse.value ) {
$pageHtml = $pageHtml + '<p> ' + $reply.createdDateTime+" reply: "+ $reply.from.user.displayName +" " + $reply.subject+":"+$reply.body.content +'</p>'
}
$pageHtml = $pageHtml + '<p>---------------------------------------------------------------------------------------------</p>'
}
$pageHtml = $pageHtml + ' </body>
</html>
'
$graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"
非常感谢任何帮助
我首先在这里拨打电话,然后通过 nextLink 收到 20 条消息。拿那个 nextLink 我只是使用一个 while 循环,其中 nextLink 不为 null 并再次调用 API 和那个 nextLink 并将结果保存在 $result 变量中。试试下面的代码,它对我有用。
$result= $null
$messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
$graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
while($graphResponse."@odata.nextLink" -ne $null)
{
$result = $result + $graphResponse."value"
$graphResponse = Invoke-RestMethod -Method Get -Uri $graphResponse."@odata.nextLink" -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
}
$result = $result + $graphResponse."value"
#go through files and copy them to target
foreach ($message in $result)
{
$messageID = $message.id
$pageHtml = $pageHtml + '<p>' + $message.createdDateTime+" "+ $message.from.user.displayName +" <b>" + $message.subject+"</b>:"+$message.body.content +'</p>'
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages/" + $messageID + "/replies"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
foreach ($reply in $repliesResponse.value ) {
$pageHtml = $pageHtml + '<p> ' + $reply.createdDateTime+" reply: "+ $reply.from.user.displayName +" " + $reply.subject+":"+$reply.body.content +'</p>'
}
$pageHtml = $pageHtml + '<p>---------------------------------------------------------------------------------------------</p>'
}
$pageHtml = $pageHtml + ' </body>
</html>
'
$graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"
我正在使用已发布在此网站上的脚本 [https://myteamsday.com/2019/10/08/exporting-team-channel-messages/][1]
将频道消息导出到 OneNote 文件。脚本工作正常,我的问题是我无法向脚本添加分页(以便使用“@odata.nextLink”检索所有消息)我是开发和 MS 图形方面的新手,我知道我需要在代码中添加 if 语句或 While(),但我不确定该怎么做。
这里是脚本读取所有消息和回复并将它们写入 Onenote 文件的地方
$messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
$graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
#go through files and copy them to target
foreach ($message in $graphResponse.value)
{
$messageID = $message.id
$pageHtml = $pageHtml + '<p>' + $message.createdDateTime+" "+ $message.from.user.displayName +" <b>" + $message.subject+"</b>:"+$message.body.content +'</p>'
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages/" + $messageID + "/replies"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
foreach ($reply in $repliesResponse.value ) {
$pageHtml = $pageHtml + '<p> ' + $reply.createdDateTime+" reply: "+ $reply.from.user.displayName +" " + $reply.subject+":"+$reply.body.content +'</p>'
}
$pageHtml = $pageHtml + '<p>---------------------------------------------------------------------------------------------</p>'
}
$pageHtml = $pageHtml + ' </body>
</html>
'
$graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"
非常感谢任何帮助
我首先在这里拨打电话,然后通过 nextLink 收到 20 条消息。拿那个 nextLink 我只是使用一个 while 循环,其中 nextLink 不为 null 并再次调用 API 和那个 nextLink 并将结果保存在 $result 变量中。试试下面的代码,它对我有用。
$result= $null
$messagesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages"
$graphResponse = Invoke-RestMethod -Method Get -Uri $messagesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
while($graphResponse."@odata.nextLink" -ne $null)
{
$result = $result + $graphResponse."value"
$graphResponse = Invoke-RestMethod -Method Get -Uri $graphResponse."@odata.nextLink" -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
}
$result = $result + $graphResponse."value"
#go through files and copy them to target
foreach ($message in $result)
{
$messageID = $message.id
$pageHtml = $pageHtml + '<p>' + $message.createdDateTime+" "+ $message.from.user.displayName +" <b>" + $message.subject+"</b>:"+$message.body.content +'</p>'
$repliesURI = "https://graph.microsoft.com/beta/teams/" + $teamID + "/channels/" + $ChannelID + "/messages/" + $messageID + "/replies"
$repliesResponse = Invoke-RestMethod -Method Get -Uri $repliesURI -Headers @{"Authorization"="Bearer $delegatedaccessToken"}
foreach ($reply in $repliesResponse.value ) {
$pageHtml = $pageHtml + '<p> ' + $reply.createdDateTime+" reply: "+ $reply.from.user.displayName +" " + $reply.subject+":"+$reply.body.content +'</p>'
}
$pageHtml = $pageHtml + '<p>---------------------------------------------------------------------------------------------</p>'
}
$pageHtml = $pageHtml + ' </body>
</html>
'
$graphResponse = Invoke-RestMethod -Method Post -Uri $addPageURL -Headers @{"Authorization"="Bearer $delegatedaccessToken"} -Body $pageHtml -ContentType "text/html"