In this article, we will discuss how to send messages to Telegram using PowerShell. PowerShell is a command line shell and scripting language developed by Microsoft to automate and manage system tasks.
Telegram is a popular instant messaging service that provides APIs for interacting with its services. This means that you can use PowerShell to automatically send messages to Telegram.
And you can use PowerShell to automatically send messages to Telegram.
Creating a Telegram bot
Before you can start sending messages via PowerShell, you need to create a bot in Telegram. Here are the steps you’ll need to follow:
- Write @BotFather in the Telegram search box. This is the official bot for creating other bots in Telegram.
- Click “Start” or “Start” to interact with the bot.
- Enter the command “/new bot” and follow the instructions. You will have to choose a name and username for your bot.
- After creating the bot, BotFather will provide you with an access token. This token will be used to authorize requests to the Telegram API.
PowerShell and Telegram API
Now that you have a bot and an access token, you can use PowerShell to send messages via the Telegram API. Here’s a basic example of a PowerShell script to send a message:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$botToken = “Your token”
$chatID = “Your chat id”
$messageText = “Test massage”
$url = “https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatID&text=$messageText”
Invoke-RestMethod -Uri $url -Method Post
In this example, we use the PowerShell command Invoke-RestMethod to send a POST request to the Telegram API. $botToken is the access token you received from BotFather, $chatID is the ID of the chat room you want to send a message to, and $messageText is the text of the message you want to send.
$messageText is the text of the message you want to send.
To find out the chat ID, you can send the bot any message and then go to https://api.telegram.org/ЬотВашТокен/getUpdates in your browser. You’ll see the chat ID in the reply.
And then you’ll see the chat ID in the reply.
Let’s look at a small script that will send a message that will indicate whether the server is running or not.
To check if the server is available, you can use the PowerShell Test-Connection cmdlet, which is similar to the ping cmdlet on the command line. Here’s an example script that sends a message to Telegram, depending on whether the server is available or not:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$botToken = “Your token”
$chatID = “Your chat id”
$serverIP = “Server IP”
# Check server availability
if (-not (Test-Connection -ComputerName $serverIP -Count 1 -Quiet)) { $messageText = “Server down!”
$url = “https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatID&text=$messageText”
Invoke-RestMethod -Uri $url -Method Post }
In this example, the Test-Connection command checks if the server is available. If the server is unavailable, Test-Connection returns False, and thanks to the -not operator, the condition in if becomes True. Then the script sends the message “Warning! The server is unavailable” to Telegram. If the server is available, the script does nothing.
Now you can add this script in your scheduler, to run it, for example, every 30 min. We will see how to do this in the next article.
Conclusion
PowerShell and the Telegram API provide powerful tools for automating messaging. Be careful not to share your access token, chat ID and other sensitive data. PowerShell and the Telegram API provide powerful tools to automate messaging.