How to Send Telegram Messages from a PowerShell Script

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 will need to designate the bot token as well as the ID of your intended user (or conversation):

$tg_token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$tg_chat_id="987654321"

Connection to the Telegram API necessitates the TLS 1.2 protocol. Make sure to enable the version of the TLS 1.2 protocol in  Windows. Ordinarily, PowerShell establishes connections using the aged SSL 3.0, TLS 1.0, or TLS 1.1 protocols. Execute the command shown below to engage the TLS version 1.2 within 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)" rel="nofollow" target="_blank">

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:

<code>$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"</code>

To work with PowerShell scripts, I recommend using the VS Code editor.

If you are accessing the Internet through a proxy server, you can use the <code>-Proxy</code> parameter of the Invoke-WebRequest cmdlet to specify the proxy settings. Use the <code>-ProxyCredential</code> 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 have the ability to construct a function from a script that transmits a message to Telegram and attach it to the PowerShell profile file located 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

}

Launch a text document with a PowerShell profile that gets automatically implemented when you start powershell.exe/pwsh.exe:

notepad $PSHOMEProfile.ps1

With this, you now have the means to send a message to a Telegram channel originating from any PowerShell script.

Send-Telegram "My test message"

If you use Teams as your primary messenger, you can also

use PowerShell to send a message to a Teams channel.


Posted

in

, ,

by

Tags: