Dmitry Porotnikov / PowerShell: Send telegram message via Bot using RestAPI and Bot Token

Created Tue, 22 Aug 2023 13:52:35 +0000 Modified Tue, 22 Aug 2023 13:52:35 +0000
90 Words

PowerShell: Send telegram message via Bot using RestAPI and Bot Token

$text_to_send = "*" + $VM_Name + "*" + " " + "Deployed by: " + "*" + $env:UserName + "*" + " " + "In RG: " 

$token = "BOT TOKEN"
$preview_mode = "True"
$markdown_mode = "Markdown"

$payload = @{
	"chat_id"					   = "-0000000";
	"text"						   = $text_to_send;
	"parse_mode"				   = $markdown_mode;
	"disable_web_page_preview"	   = $preview_mode;
}
try
{
	$notify = Invoke-WebRequest `
								-Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $token) `
								-Method Post `
								-ContentType "application/json;charset=utf-8" `
								-Body (ConvertTo-Json -Compress -InputObject $payload)
}
catch
{
}