add `Get-MatrixRoomId` (#2)

This commit is contained in:
James 2022-03-31 23:08:22 +01:00 committed by GitHub
parent a0d3272f2c
commit 1e765ccdd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -73,7 +73,8 @@ FunctionsToExport = @(
'New-MatrixAccessToken', 'New-MatrixAccessToken',
'Remove-MatrixAccessToken', 'Remove-MatrixAccessToken',
'Get-MatrixJoinedRooms', '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. # 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.

View File

@ -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 $_
}
}