OpsaC - Operating as PowerShell code
published: October 14, 2021 author: Tinu tags: PowerShell - Webhook categories: Messenger
Mit Telegram kann mit Invoke-WebRequest Messages an einen Channel versendet werden wie mit Discord.
Erstelle einen Account auf Telegram.
Telegramm-Bots werden über ein automatisiertes Konto namens BotFather erstellt.
Öffne die Telegram-App und suche nach BotFather. Klicke auf BotFather, um einen Chat zu öffnen.
Send Message with PowerShell
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[Switch]$Html,
[Parameter(Mandatory=$false)]
$Message,
[Parameter(Mandatory=$true)]
$Token,
[Parameter(Mandatory=$true)]
$ChatID
)
begin {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$webhook_url = "https://api.telegram.org/bot$($Token)/sendMessage"
}
process {
if($Html){
$ParseMode = 'html'
if(-not($Message)){
$Message = '
Hello from <b>PowerShell</b>!
This is a html-formated message.
<a href="https://it.martin-walther.ch/telegram/">Visit Tinus IT Wiki</a>
'
}
}else{
$ParseMode = 'MarkdownV2'
if(-not($Message)){
$Message = '
Hello from *PowerShell*\!
This is a text\-message\ formated with MarkdownV2\.
[Visit Tinus EngOps Wiki](https://tinuwalther.github.io//)\.
'
}
}
$headers = "application/json"
$payload = @{
"chat_id" = $ChatID
"text" = $Message
"parse_mode" = $ParseMode
"disable_web_page_preview" = $false
"disable_notification" = $false
}
$Response = Invoke-RestMethod -Uri $webhook_url -Body (ConvertTo-Json -Depth 1 -InputObject $payload) -Method Post -ContentType $headers
}
end {
$Response.result
}
Return
ok result
-- ------
True @{message_id=12; from=; chat=; date=1634212752; text=Hello from PowerShell!; entities=System.Object[]}
Send Telegram Messages from PowerShell, Telegram API