kohera-logo-regular.svg

Sending monitoring alerts through Telegram

What if you could get the ease of phone notifications for whatever monitoring alerts you need? Then we have a solution for you with the app Telegram. Some of you may be familiar with this messaging tool, that’s very similar to WhatsApp. Telegram focusses on speed and security, is easy to use and free. Setting up the monitoring alerts through Telegram enables you to always be informed on any device. In this blog, we will show you how to set this up with the language PowerShell. We will also create a “bot”, which will be used to receive the messages that we are sending through PowerShell.

 

Create a Telegram Bot

Open the Telegram app and look for the BotFather account in the search bar. Click the verified BotFather account, this bot will be used to create new bot accounts and manage your existing bots.

Open the chat with the BotFather.

    1. Enter or click the command /newbot

    1. Enter the name for your bot

    1. Enter a username for the bot

    1. Save the API token (it is case sensitive)

The full process looks like this:

 

 

Add the bot to a channel

Your bot is created! Next, we need to add the bot to our group or channel so we can receive our chat ID. Pick whatever channel or group you want to use and add the bot. Once you’ve done that, you will need to make the bot administrator. That way we can get the list of updates for your bot.

For this step, you need to go to the Telegram website and fill in your bot API token.

https://api.telegram.org/bot<YourBOTToken>/getUpdates

Example:

https://api.telegram.org/bot5115011894:AAHf8mpjSX-4sLCzBeVtbEAC6YYhADzBPks/getUpdates

Then look for the “chat” object:
(Note: If you created the group/channel with the bot and you only get: {“ok”:true,”result”:[]} , please remove and add the bot again)

 

 

The PowerShell

Now that we have all the parameters we need, we can go to the last and probably most crucial step: the PowerShell script. We have different ways to set up our script to fulfill different use cases. One is getting data from another application. Let’s say we have a monitoring application like SentryOne. When a certain condition is met within that application, we want a Telegram message to be sent.

{

$MyToken = "5313732773:AAGiZxJk6rgB4RkrlMGnWm28sj2mH_0cYDQ"

$chatID = -1001693547082

$rawBody = @{text='<%Message%>'}

$contentType ='application/json'

$jsonBody = (convertto-json $rawBody)

$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)" -Method POST -ContentType $contentType -Body $jsonBody

Let’s break down the script to see what all these PowerShell lines mean:

    1. The first two lines of code are your chatID and your BotToken. Those are two parameters that you have determined in the previous step.

    1. The third line in the script is a message placeholder. This is an executive action parameter in SentryOne and allows us to pass content.

    1. The last few lines are the ones that will convert the messages into JSON and send the message to Telegram.

The message

When your condition is met and your PowerShell action is defined with the correct parameters, we will get a message like this on our Telegram chat.

Or let’s say we want to decide what we sent. We can change the script a little bit.

$MyToken = "5313732773:AAGiZxJk6rgB4RkrlMGnWm28sj2mH_0cYDQ"
$chatID = -625205559
$rawBody = @{text='Hello everyone, thank you for reading my blog!'}
$Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)" -Method POST -Body $RawBody

The test

So, let’s run the last script and see which result we get.

You received a message from your Telegram bot! Using the above method, you will be able to send monitoring alerts through Telegram by using the app in your PowerShell scripts.

Group of computer programmers working in the office. Focus is on blond woman showing something to her colleague on PC.
Updating your Azure SQL server OAuth2 credentials in Power BI via PowerShell for automation purposes
The better way to update OAuth2 credentials in Power BI is by automating the process of updating Azure SQL Server...
2401-under-memory-pressure-featured-image
Under (memory) pressure
A few weeks ago, a client asked me if they were experiencing memory pressure and how they could monitor it...
2402-fabric-lakehouse-featured-image
Managing files from other devices in a Fabric Lakehouse using the Python Azure SDK
In this blogpost, you’ll see how to manage files in OneLake programmatically using the Python Azure SDK. Very little coding...
2319-blog-database-specific-security-featured-image
Database specific security in SQL Server
There are many different ways to secure your database. In this blog post we will give most of them a...
kohera-2312-blog-sql-server-level-security-featured-image
SQL Server security made easy on the server level
In this blog, we’re going to look at the options we have for server level security. In SQL Server we...
blog-security_1
Microsoft SQL Server history
Since its inception in 1989, Microsoft SQL Server is a critical component of many organizations' data infrastructure. As data has...