From d15beccf7fe886be8dc46cff3e5aa0f52c4d86f1 Mon Sep 17 00:00:00 2001 From: Thumbscrew Date: Thu, 31 Mar 2022 23:06:59 +0100 Subject: [PATCH] add `Get-MatrixRoomId` --- PSMatrix.psd1 | 3 ++- public/Get-MatrixRoomId.ps1 | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 public/Get-MatrixRoomId.ps1 diff --git a/PSMatrix.psd1 b/PSMatrix.psd1 index 44be4a0..fe10bf3 100644 --- a/PSMatrix.psd1 +++ b/PSMatrix.psd1 @@ -73,7 +73,8 @@ FunctionsToExport = @( 'New-MatrixAccessToken', 'Remove-MatrixAccessToken', 'Get-MatrixJoinedRooms', - 'Get-MatrixJoinedMembers' + 'Get-MatrixJoinedMembers', + 'Get-MatrixRoomId' ) # 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-MatrixRoomId.ps1 b/public/Get-MatrixRoomId.ps1 new file mode 100644 index 0000000..7a0c9bf --- /dev/null +++ b/public/Get-MatrixRoomId.ps1 @@ -0,0 +1,42 @@ +<# + .Synopsis + Get the Matrix Room ID for the given Room Alias. + + .Description + Get the Matrix Room ID for the given Room Alias from _matrix/client/v3/directory/room/{roomAlias}. See https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3directoryroomroomalias. + + .Parameter ServerUrl + URL for the Matrix server to query, for example "https://matrix.example.com". + + .Parameter RoomAlias + The Matrix Room Alias to retrieve the Room ID for (e.g. '#matrix:matrix.org'). + + .Example + Get-MatrixRoomId -ServerUrl $matrix -RoomAlias "#synapse:matrix.org" +#> +function Get-MatrixRoomId { + param( + [Parameter(Mandatory)] + [string]$ServerUrl, + + [Parameter(Mandatory)] + [string]$RoomAlias + ) + + $encodedRoomAlias = $RoomAlias.Replace('#', '%23') + $url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/directory/room/$encodedRoomAlias" + Write-Debug "URL: $url" + + try { + $res = Invoke-RestMethod -Uri $url + + $roomId = [PSCustomObject]@{ + RoomId = $res.room_id + Servers = $res.servers + } + + return $roomId + } catch { + Write-Error $_ + } +} \ No newline at end of file