You can use your Telegram messenger as a notification tool to get instant reports on various infrastructure events, script execution results, or scheduler tasks. This article shows you how to use PowerShell to send a text notification to a Telegram channel or group through the Bot API.
First, create a new Telegram bot using @BotFather
. Find it in your Telegram client and send it the following commands:
/start
/newbot
Specify the bot’s name and username. BotFather will generate an HTTP token that you must copy and save.
To send a message to a Telegram chat or a specific user, you need to know their ID. In this example, I will send notifications to myself, so use @my_id_bot
to find my Telegram ID:
/start
Your user ID: 987654321
To send a message to any Telegram chat, you need to specify the bot token and the ID of the target user (or chat):
$tg_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tg_chat_id="987654321"
The TLS 1.2 protocol is required to connect to the Telegram API. Ensure that the TLS 1.2 protocol version is enabled in Windows. By default, PowerShell uses the legacy SSL 3.0, TLS 1.0, or TLS 1.1 protocols for connections. Run the following command to use TLS version 1.2 in the current PowerShell session
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
And here’s a command to send a message to Telegram:
$message="Test message alert from PowerShell"
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)"
Once you run that one, you should get a message from the bot.
You can use emoji and HTML text formatting to make notifications more visually appealing and readable:
$message= $currend_data + "⚠️ Update Script <b>SAP_DB_Update</b> completed with errors"
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)&parse_mode=html"
If you are accessing the Internet through a proxy server, you can use the -Proxy
parameter of the Invoke-WebRequest cmdlet to specify the proxy settings. Use the -ProxyCredential
argument to authenticate to a proxy.
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($Telegramtoken)/sendMessage?chat_id=$($Telegramchatid)&text=$($Message)" –Proxy "http://192.168.13.155:3128"
In version PowerShell Core 7.x+, the Invoke-WebRequest cmdlet uses the proxy settings specified in the environment variables. Learn more about using a proxy in PowerShell.
[/alert]
You can create a function from a script that sends a message to Telegram and add it to the PowerShell profile file in Windows:
function Send-Telegram { [CmdletBinding()] param( [Parameter()] [string] $Message ) $tg_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" $tg_chat_id="987654321" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($tg_token)/sendMessage?chat_id=$($tg_chat_id)&text=$($Message)&parse_mode=html" return $Response }
notepad $PSHOME\Profile.ps1
It is now possible to send a message to a Telegram channel from any PowerShell script.
Send-Telegram "My test message"