PoshGram – a PowerShell Module for Telegram

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:

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

#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" }

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.

6 Responses

  1. Mitch says:

    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!

  2. Joe says:

    Thanks for a really useful module!

    I have a python script that creates a text file of results after it has run. I’d like to pipe out some lines from the results straight to Telegram, via your module, rather than send the whole file. Is that possible? Using ‘sls’ I can select the lines I need but I’m not sure how to pass them easily to TelegramTextMessage? I’m using a similar module on Linux (using python) where I can “grep “contents” file | telegram-notify –text -“. Does your module have something similar?

    Thank you very much for your time.

  3. Gabriel says:

    Hello, i’m trying to send a content of txt file to my bot, but if the txt file have more than one line, the sendtextmessage fail.

    Can you help me?
    I’m using the poshgram 1.0.2 and powershell core 6.2.0

    • Charleson says:

      You can try forEach.

      ForEach ($Message in Get-Content “C:\Users\Charleson\Desktop\Nieuwtekstdocument.txt”)
      {
      Write-Host $Message
      $Response = Invoke-RestMethod -Uri “https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)&text=$($Message)”
      }

Leave a Reply

Your email address will not be published. Required fields are marked *