mirror of
https://github.com/Thumbscrew/PSMatrix.git
synced 2025-01-18 09:35:44 +00:00
add Get-MatrixRoomMessages
(#3)
* add `Get-MatrixRoomMessages` * Add custom MatrixMessage type * update README with examples
This commit is contained in:
parent
1e765ccdd4
commit
6ad851f54b
@ -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.
|
||||
|
31
PSMatrixTypes.ps1xml
Normal file
31
PSMatrixTypes.ps1xml
Normal file
@ -0,0 +1,31 @@
|
||||
<Types>
|
||||
<Type>
|
||||
<Name>MatrixMessage</Name>
|
||||
<Members>
|
||||
<NoteProperty>
|
||||
<Name>EventID</Name>
|
||||
<Value>EventID</Value>
|
||||
</NoteProperty>
|
||||
<NoteProperty>
|
||||
<Name>Sender</Name>
|
||||
<Value>Sender</Value>
|
||||
</NoteProperty>
|
||||
<NoteProperty>
|
||||
<Name>Body</Name>
|
||||
<Value>Body</Value>
|
||||
</NoteProperty>
|
||||
<NoteProperty>
|
||||
<Name>Format</Name>
|
||||
<Value>Format</Value>
|
||||
</NoteProperty>
|
||||
<NoteProperty>
|
||||
<Name>FormattedBody</Name>
|
||||
<Value>FormattedBody</Value>
|
||||
</NoteProperty>
|
||||
<NoteProperty>
|
||||
<Name>MsgType</Name>
|
||||
<Value>MsgType</Value>
|
||||
</NoteProperty>
|
||||
</Members>
|
||||
</Type>
|
||||
</Types>
|
56
README.md
56
README.md
@ -1,2 +1,56 @@
|
||||
# PSMatrix
|
||||
PowerShell module for interacting with the Matrix API
|
||||
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
|
||||
```
|
||||
|
79
public/Get-MatrixRoomMessages.ps1
Normal file
79
public/Get-MatrixRoomMessages.ps1
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user