diff --git a/PSMatrix.psd1 b/PSMatrix.psd1 index fe10bf3..69f46a7 100644 --- a/PSMatrix.psd1 +++ b/PSMatrix.psd1 @@ -74,7 +74,8 @@ FunctionsToExport = @( 'Remove-MatrixAccessToken', 'Get-MatrixJoinedRooms', 'Get-MatrixJoinedMembers', - 'Get-MatrixRoomId' + 'Get-MatrixRoomId', + 'Get-MatrixRoomMessages' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/public/Get-MatrixRoomMessages.ps1 b/public/Get-MatrixRoomMessages.ps1 new file mode 100644 index 0000000..8c6b1f8 --- /dev/null +++ b/public/Get-MatrixRoomMessages.ps1 @@ -0,0 +1,77 @@ +function Get-MatrixRoomMessages { + param( + [Parameter(Mandatory)] + [string]$ServerUrl, + + [Parameter(Mandatory)] + [SecureString]$AccessToken, + + [Parameter(Mandatory)] + [string]$UserId, + + [Parameter(Mandatory)] + [string]$RoomId, + + [Parameter(Mandatory = $false)] + [int]$Limit = 50 + ) + + Write-Debug "URL: $url" + $headers = Get-MatrixAuthHeaders -AccessToken $AccessToken + + try { + $filterUrl = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/user/$UserId/filter" + $filter = [PSCustomObject]@{ + room = @{ + rooms = @( + $RoomId + ) + timeline = @{ + limit = $Limit + rooms = @( + $RoomId + ) + types = @( + "m.room.message" + ) + } + } + } | ConvertTo-Json -Depth 3 + + $filterRes = Invoke-RestMethod -Uri $filterUrl -Method "POST" -Headers $headers -Body $filter + } catch { + Write-Error $_ + return + } + + try { + $filterId = $filterRes.filter_id + $url = New-MatrixUrl -ServerUrl $ServerUrl ` + -ApiPath ("_matrix/client/v3/sync" ` + + "?filter=$filterId" ` + + "&full_state=true&set_presence=offline") + + $res = Invoke-RestMethod -Uri $url -Headers $headers + } catch { + Write-Error $_ + } + + $events = $res.rooms.join.($RoomId).timeline.events + + $formattedEvents = @() + + $events | ForEach-Object { + $formattedEvent = [PSCustomObject]@{ + EventID = $_.event_id + Sender = $_.sender + Body = $_.content.body + Format = $_.content.format + FormattedBody = $_.content.formatted_body + MsgType = $_.content.msgtype + } + + $formattedEvents += $formattedEvent + } + + return $formattedEvents +} \ No newline at end of file