---
title: 'PowerShell: Sending messages to Telegram | Zomro'
description: '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 au'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/01_MPfaK7Y.png'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [FAQ](https://zomro.com/blog/faq/)
  * PowerShell: Sending messages to Telegram 



# PowerShell: Sending messages to Telegram

03-09-2023

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:

  1. Write @BotFather in the Telegram search box. This is the official bot for creating other bots in Telegram.
  2. Click on "Start" or "Start" to start interacting with the bot.
  3. Enter the command "/newbot" and follow the instructions. You will have to choose a name and username for your bot.
  4. 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: Sending messages to Telegram - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/01_MPfaK7Y.png)

![PowerShell: Sending messages to Telegram - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/02_LfcHAux.png)

## 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.

![PowerShell: Sending messages to Telegram - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/03_OGI824s.png)

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 } 

![PowerShell: Sending messages to Telegram - 4](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/04_rQV9FZA.png)

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.

![PowerShell: Sending messages to Telegram - 5](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48%40server-panel.net/2023/09/03/055_lBL3FHI.png)

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.

We also suggest you other useful articles:

  * [Monitoring server availability via Windows Scheduler](https://zomro.com/blog/faq/418-monitoring-server-availability-via-windows-scheduler)
  * [Disable automatic installation of Windows updates](https://zomro.com/blog/faq/426-otklyuchenie-avtomaticheskoj-ustanovki-obnovlenij-windows)
  * [How to view shutdown and restart logs in Windows Server](https://zomro.com/blog/faq/258-kak-posmotret-logi-shutdown-i-restart-v-windows-server)



Similar articles:

[How to enable previous session restore on Windows Server 2016, 2019?](https://zomro.com/blog/faq/311-kak-vkljuchit-vosstanovlenie-predyduschej-sessii-na-servere-windows-server-2016-2019) [External FTP storage: Connect folder to the Windows server.](https://zomro.com/blog/faq/217-vneshnee-ftp-hranilische-podkljuchenie-k-serveru) [Service availability check via MTR](https://zomro.com/blog/faq/257-proverka-dostupnosti-uslugi-cherez-mtr) [How to enable two-factor authentication in your personal account](https://zomro.com/blog/faq/321-how-to-enable-two-factor-authentication-in-your-personal-account) [How to track SSH connections to the server using a Telegram bot](https://zomro.com/blog/faq/329-kak-otslezhivat-ssh-podklyucheniya-k-serveru-s-pomoshyu-telegram-bota)

[ Premium Cloud VPS from €7.95/mon ](/vps)
