This post is a continuum to Part one, in which I describe how to create the Telegram Chatbot and check the pending messages. This post will be a bit detailed, so beginners can get how to basic Telegram and PowerShell integration works. In this part (Part 2). we will discuss the following points:

– Read incoming messages via Powershell.
– Send a Message to Telegram Chatbot via Powershell.

The Chatbot should do the following

  • Receive messages from a trusted person only.
  • Able to send back messages.
  • The Bot should not execute any script before authentication, through the following condition (All in Part 3):
    • A passphrase should be sent to the Chatbot as an auth key.
    • The First Name and last name should be trusted.
    • The ability to log off the session.
    • Limit the possible command which can be executed.

Endpoints

We will focus on two Endpoints, the /Getupdates and /SendMessage.
To make the explanation easy, consider the endpoint the last part of the URL, in this example the URL is

https://api.telegram.org/bot{Token}

The endpoint will be appending the /Getupdates or /SendMessage to the URL

The /Getupdates endpoint will provide us with recent messages, the full URL will be https://api.telegram.org/bot{Token}/getUpdates

The /SendMessage endpoint will be used to send the message back to the sender, and the full URL will be: https://api.telegram.org/bot{Token}/sendMessage
Don’t forget to replace {Token} with your token.

Read Incoming Telegram messages via Powershell

I already sent a message from my Telegram messenger to the bot with the following text Hi Mr. Bot.. are you alive or dead, Lets navigate to the /getUpdates endpoint and see the format of the message

"message":{"message_id":4,"from":{"id":1423175957,"is_bot":false,"first_name":"Faris","last_name":"Malaeb","language_code":"en"},"chat":{"id":1423175957,"first_name":"Faris","last_name":"Malaeb","type":"private"},"date":1606054313,"text":"Hi Mr. Bot.. are you alive or dead"}}]}

This is the raw message, we can see some parameters that will be needed, such as First Name, Last Name, Date, and ID “which is the Chat_ID”.

The Code to read the incoming message is

$token='1418701162:AAEIklEHZowjxorXL6VNfgzV54CmnPoUa3c' #Replace this with your bot Token
$URL='https://api.telegram.org/bot{0}' -f $Token
$inMessage=Invoke-RestMethod -Method Get -Uri ($URL +'/getUpdates') -ErrorAction Stop

calling the $inMessage variable will return two members ok and result, all the work will be with sub-property of the result property named message, let get the members of result.message and see its members

$inMessage.result.message | get-member
These are the key for all the communication 🙂

Lets see the content of each property

From: contain the chat_id of the sender so you can use this ID to send the message back. Also, it includes the First Name and Last Name of the sender.
Chat: similar to from, we won’t use it much
Date: the date and time in the EPOCH format
Text: The message content

Send a Message to Telegram Chatbot via Powershell

Referring to the Telegram API, we will use the /sendMessage endpoint. This endpoint requires two parameters:

Parameter nameDescription for the Parameter
Chat_IDWhich is the ID of the chat session the bot will communicate with. We can get this number from the /getUpdates endpoint
MessageA 1 to 4096 character text message

We can get the Chat_id from the /GetUpdates endpoint, by checking this endpoint we will see the text

Please note that the message should be a JSON format, so we can use the following code:

$token='1418701162:AAEIklEHZowjxorXL6VNfgzV54CmnPoUa3c' #Replace this with your bot HTTP API Token
$URL='https://api.telegram.org/bot{0}' -f $Token
$MessageToSend = New-Object psobject 
$MessageToSend | Add-Member -MemberType NoteProperty -Name 'chat_id' -Value "1423175957"
$MessageToSend | Add-Member -MemberType NoteProperty -Name 'text' -Value "Message From Bot to you Human "
Invoke-RestMethod -Method Post -Uri ($URL +'/sendMessage') -Body ($MessageToSend | ConvertTo-Json) -ContentType "application/json"

Execute these lines and you will receive the message on your Telegram Messenger application.

botreply

I hope this explanation is clear and straight forward, I tried to explain a bit more here as this will be the building block for Part 3

You can now create a basic chatbot that sends and receive messages, but don’t forget, before putting it in production, we need to protect it and add a basic level of security.

In the next post, we will discuss the basic protection and integrate the entire code.

<– Control your Infrastructure using… Telegram Messenger (Part 1 – Create the Bot)
Control your Infrastructure using… Telegram Messenger (Part 2 – Send and Receive.)
Control your Infrastructure using… Telegram Messenger (Part 3 – Basic Security) –>
Control your Infrastructure using… Telegram Messenger (Part 4 – Execution) –>

5/5 - (1 vote)