PoshGram – a PowerShell Module for Telegram

Send Telegram messages using PowerShell and incorporate Telegram notifications into your PowerShell scripts

PoshGram Overview

PoshGram is a PowerShell module that enables you to send messages via the Telegram Bot API. It is written for PowerShell 6.1, which natively supports the form parameter for Invoke-WebRequest and Invoke-RestMethod. The Telegram bot API requires local media to be sent using multipart/form-data which is difficult to achieve in previous versions of PowerShell. While PowerShell 6.1 is required as a prerequisite, several functions are 5.1 compatible if you wish to use them individually. Additionally, you can easily call PoshGram from other versions of PowerShell by referencing the 6.1 pwsh.exe directly. I provide an example of this in the sections below.

The goal of the project is to enable sending Telegram messages with simple PowerShell cmdlets. Text messages as well as multimedia messaging are supported, referencing local media or URL media. Error control and verification tests are included to ensure that messages adhere to size and file type restrictions required for each message type.

PoshGram live demonstration GIF

Video Walk-through

If you prefer video format over written documentation you can view the following PoshGram video demo:

Installing PoshGram

The prerequisites for PoshGram, as well as the methods for installation are documented in detail on the PoshGram GitHub repository.

What can PoshGram do?

The Telegram Bot API requires very specific formatting criteria for Bot messaging. The goal of this project to abstract that complexity away in favor of simple and direct PowerShell functions.

Sending Telegram messages via PowerShell opens up several programmatic use cases:

  • Custom scripts tied to task scheduler could alert you to potential system conditions
    • Test-LowDisk.ps1 tied to task scheduler –> send Telegram message if low disk condition found
  • Enable existing script to provide Telegram notifications (even older scripts – see below)
  • Create a PowerShell based ForEach to easily message multiple chat groups that your bot is a member of
  • The possibilities are really only limited to your imagination!

Will PoshGram work if environment still requires older version of PowerShell?

Yes! Powershell 6.1 installs into a separate directory and even has a new exe name (pwsh.exe) – this means it can run side-by-side with whatever version of powershell you prefer.

In fact, you can call PowerShell 6.1 and send Telegram messages via PoshGram from other versions of PowerShell:

#here is an example of calling PowerShell 6.1 from PowerShell 5.1 to send a Telegram message with PoshGram
& 'C:\Program Files\PowerShell\6-preview\pwsh.exe' -command { Import-Module PoshGram;$token = "#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx";$chat = "-#########";Send-TelegramTextMessage -BotToken $token -ChatID $chat -Message "Test from 5.1 calling 6.1 to send Telegram Message via PoshGram" }

This will allow you to take older scripts and incorporate Telegram notifications into them easily.

Code Examples

Here are a few quick examples to get you started with sending message with the module:

I want to contribute to the project

That’s fantastic! Please feel free to submit bugs, suggestions, and code improvements to the GitHub.

2 Comments

  1. Hi,

    Thanks for the guide, it’s very useful! I’ve managed to send messages to Telegram using PoshGram and it’s working perfectly.

    I had one question I was hoping you could assist with:

    Because the PoshGit module needs to be run on PowerShell 6.1, I’m having a hard time passing variables from PowerShell 5.1 to the PowerShell 6.1 executable.

    For Example: I have $processes = Get-Process | Select ProcessName | Out-String defined, but if I do & ‘.\Program Files\PowerShell\6-preview\pwsh.exe’ -command Import-Module PoshGram;$token = “#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx”;$chat = “-#########”;Send-TelegramTextMessage -BotToken $token -ChatID $chat -Message “The following processes are running $processes” my Bot message says “The following process are running” with nothing where the $processes value should be.

    Is there any way around this?

    Thanks in advance!

Leave a Reply

Your email address will not be published.


*