From fe23666fc0a0e2147a0dc02ce23a6e3ad06545f7 Mon Sep 17 00:00:00 2001 From: --global Date: Mon, 11 Nov 2019 10:29:50 +0100 Subject: [PATCH] implement sending messages --- bot.ps1 | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 bot.ps1 diff --git a/bot.ps1 b/bot.ps1 new file mode 100644 index 0000000..66906b1 --- /dev/null +++ b/bot.ps1 @@ -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)) \ No newline at end of file