implement sending messages

master
--global 5 years ago
commit fe23666fc0

@ -0,0 +1,70 @@
param (
[Parameter(Mandatory=$true)]
[string]
$HomeServer,
[Parameter(Mandatory=$true)]
[string]
$User,
[Parameter(Mandatory=$true)]
[securestring]
$AccessToken
)
function Send-MatrixEvent {
param (
[Parameter(Mandatory=$true)]
[string]
$RoomId,
[Parameter(Mandatory=$true)]
[string]
$Event,
[Parameter(Mandatory=$true)]
[string]
$EventType
)
#txn_id should be unique per client, so we use timestamp+random
$txn_id = '{0}{1}' -f (Get-Date -UFormat '%s'),(Get-Random)
$uri = '{0}/_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $HomeServer,$RoomId,$EventType,$txn_id
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{
Uri = $uri
Method = 'Put'
Body = $Event
}
$response = Invoke-RestMethod @header_splat @http_splat
if($response.event_id) {
Write-Output ('Event {0} sent to room {1}' -f $response.event_id,$RoomId)
}
}
function Send-MatrixNotice {
param (
[Parameter(Mandatory=$true)]
[string]
$RoomId,
[Parameter(Mandatory=$true)]
[string]
$Message
)
$event = @{
msgtype = 'm.notice'
body = $Message
} | ConvertTo-Json -Compress
Send-MatrixEvent -RoomId $RoomId -Event $event -EventType 'm.room.message'
}
Send-MatrixNotice -RoomId '!amKUIaaKdERatZsJgB:matrix.org' -Message ('asef :) {0}' -f (Get-Date))
Loading…
Cancel
Save