From 6ad851f54b2e7ba1800c87c1852885224b923beb Mon Sep 17 00:00:00 2001 From: James Date: Tue, 5 Apr 2022 15:57:39 +0100 Subject: [PATCH] add `Get-MatrixRoomMessages` (#3) * add `Get-MatrixRoomMessages` * Add custom MatrixMessage type * update README with examples --- PSMatrix.psd1 | 9 ++-- PSMatrixTypes.ps1xml | 31 ++++++++++++ README.md | 56 +++++++++++++++++++++- public/Get-MatrixRoomMessages.ps1 | 79 +++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 PSMatrixTypes.ps1xml create mode 100644 public/Get-MatrixRoomMessages.ps1 diff --git a/PSMatrix.psd1 b/PSMatrix.psd1 index fe10bf3..967e652 100644 --- a/PSMatrix.psd1 +++ b/PSMatrix.psd1 @@ -9,7 +9,7 @@ @{ # Script module or binary module file associated with this manifest. -# RootModule = '' +RootModule = 'PSMatrix.psm1' # Version number of this module. ModuleVersion = '0.0.1' @@ -60,7 +60,9 @@ PowerShellVersion = '7.0' # ScriptsToProcess = @() # Type files (.ps1xml) to be loaded when importing this module -# TypesToProcess = @() +TypesToProcess = @( + 'PSMatrixTypes.ps1xml' +) # Format files (.ps1xml) to be loaded when importing this module # FormatsToProcess = @() @@ -74,7 +76,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/PSMatrixTypes.ps1xml b/PSMatrixTypes.ps1xml new file mode 100644 index 0000000..611fe6d --- /dev/null +++ b/PSMatrixTypes.ps1xml @@ -0,0 +1,31 @@ + + + MatrixMessage + + + EventID + EventID + + + Sender + Sender + + + Body + Body + + + Format + Format + + + FormattedBody + FormattedBody + + + MsgType + MsgType + + + + \ No newline at end of file diff --git a/README.md b/README.md index 2f70fea..496d02d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,56 @@ # PSMatrix -PowerShell module for interacting with the Matrix API \ No newline at end of file +PowerShell module for interacting with the Matrix API + +## Installation + +1. Clone repo: + +```bash +git clone https://github.com/Thumbscrew/PSMatrix.git +``` + +2. Import module: + +```powershell +Import-Module ./PSMatrix +``` + +## Getting Started + +1. Create a `PSCredential` object: + +```powershell +$creds = Get-Credential + +PowerShell credential request +Enter your credentials. +User: username +Password for user username: ************** +``` + +2. Get an access token from your Matrix homeserver (this will be required for subsequent authenticated requests): + +```powershell +# DeviceDisplayName is optional and will default to "PSMatrix" +$token = New-MatrixAccessToken -ServerUrl "https://example.matrix.com" -Credentials $creds -DeviceDisplayName "PSMatrix" +``` + +## Examples + +### Get a list Matrix rooms you've joined + +```powershell +$rooms = Get-MatrixJoinedRooms -ServerUrl "https://matrix.example.com" -AccessToken $token +``` + +### Get all members of a joined room + +```powershell +Get-MatrixJoinedMembers -ServerUrl "https://matrix.example.com" -AccessToken $token -RoomId "!ehXvUhWNASUkSLvAGP:matrix.org" +``` + +### Log out of your session + +```powershell +Remove-MatrixAccessToken -ServerUrl "https://matrix.example.com" -AccessToken $token +``` diff --git a/public/Get-MatrixRoomMessages.ps1 b/public/Get-MatrixRoomMessages.ps1 new file mode 100644 index 0000000..7244d68 --- /dev/null +++ b/public/Get-MatrixRoomMessages.ps1 @@ -0,0 +1,79 @@ +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 $_ + return + } + + $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 + } + $formattedEvent.PSObject.TypeNames.Insert(0, 'MatrixMessage') + + $formattedEvents += $formattedEvent + } + + return $formattedEvents +} \ No newline at end of file