In this post I will go through automating Telegram Messages with Powershell, including a full script as an example.
Truth to be told, I’ve installed, and first used, Telegram about 3 hours before writing this post, but I saw so much potentials that I couldn’t wait to publish this. Consider also that I was actually after something similar for WhatsApp, but there’s no official API from them yet.
So because I’m such a noob here, I will actually go through the steps I’ve followed to get a Bot configured to work. Note that a Bot is an easier way to handle this sort automation, but if you’re an advanced user, you could look directly into Telegram’s API which will be way more flexible.
What can I actually use this for?
Well, of course you can just do it for fun and be able to send a message via powershell. But that’d be wasting this great potential. If I look out of the box, I see a possibility to build a (cheap) notification system and/or a (cheap) runbook system.
For example, imagine that you’ve got a script running that right now sends you an email once done just to tell you that the script has finished in 30 minutes. Why an email? Isn’t it handier having an actual push-notification on your phone telling you that?
Let’s think bigger, you’re deploying a new Virtual Machine with an automated script and, besides sending you a report via email, you want to know when it’s done so that you perhaps can go and work on the VM you just deployed, without continuously checking the status of the deployment. Or imagine adding a simple Message after an SCCM Task Sequence has been completed or even just use it to alert in case of a low disk space etc.
Now, what I like the most, what if Powershell can read what we’re writing into that conversation and based on that take actions? Like a runbook. Example: you write “Restart-Computer”. The PS script could have a part of the code that checks every X amount of time if somebody wrote something and if they did, it checks the message. If the message equals to “Restart-Computer” then go and restart the computer. This is a very basic example, but it contain the core of what this can be used for.
Based on this idea, I actually build a very simple runbook automation script to leverage a Telegram message. Check it out here: Building a runbook with Powershell and Telegram
In the example at the end of this article I will be showing you how to send a message to a Telegram group and a possible action take after somebody replies with a specific keyword.
Set a Bot up
I suggest you to use a computer after step 1 as it’s going to be a bit faster in my opinion. This first bit is super simple.
- The first thing you want to do is register with Telegram if you haven’t done it yet. To do that, just go and download the app from the store (depending on what smartphone you’ve got).
- Launch the BotFather by opening this link: https://telegram.me/botfather
Time to intercept the Chat ID and run a quick test
So, in order for us to leverage the Bot to send a message, we will need to get the Chat ID of the conversation we want the Bot to talk/listen in. (more…)