My IT Department is using ManageEngine for application/server monitoring. It will send out emails whenever an application/server is down. But, it’s not enough because not everyone have the email sync-ed to their smartphone. So we need to explore new ways to send the notifications. SMS Gateway is definitely out of the equation (I proposed before, but kena shoot down).
Since we already have a WhatsApp group, sending the alerts to the group would be the best solution. But alas, WhatsApp doesn’t provide you API to do this.
I have used Telegram for quite sometime now. It was started when I was invited to my high school group. The group was moved from WhatsApp due to its limitation on how many members in a group.
As I read Telegram documentation, I found that Telegram allows you to create bot. The bot can be controlled through HTTP request. Convinced that the API is good enough to meet my needs. I decided to go ahead. Here’s how.
Before creating the bot, I need to create the group and invite all my colleagues to join.
Note the group id is highlighted in red
After all my colleagues joined the group, it’s time to create the bot. For that we need to talk to the BotFather :D:D (click the below picture to start talking)
Once the BotFather’s chat window is open. Follow the following interactions:
- Reply with the name of the bot.
- Reply with the bot’s new user id
That’s all. Now we have our bot ready. Please record down your bot’s token (highlighted in red rectangle) because we will need it to access the HTTP API.
My work emails is stored in MS Exchange Server. So I assumed that it would be dead simple to retrieve email using C#. I was wrong. After googling for this solution, I found that many people take the IMAP route instead of using Exchange Web Service API (only available in Exchange 2007 or later). Since I had a very limited time allocated to this project, I follow the recommended IMAP client library, AE.Net.Mail.
We can install this library through Nuget:
This step is quite simple. First, we retrieve the email. Second, check whether the email’s subject is inside the list of subjects that should be relayed to Telegram. If yes, send the message to Telegram. Finally, delete the email regardless it relayed to Telegram or not.
Here’s my code to retrieve the email.
static void Main(string args)
var setting = ConfigurationManager.AppSettings;
var lastRun = DateTime.Now.AddMinutes(-6);
var imap = new ImapClient(
993, // IMAP secure port
true, // is Secure?
true // skip SSL validation?
// Entity object containing the list of important email subjects
var db = new ITBotEntities();
// Check email every 5 minutes
if (lastRun.AddMinutes(5) < DateTime.Now)
var msgs = imap.SearchMessages(
foreach (var msg in msgs)
//StripSpaces will remove all the whitespaces
var subject = StripSpaces(msg.Subject);
foreach (var obj in db.LookupParams)
var objHeader = StripSpaces(obj.header);
//Send alerts to Telegram
var content = SendTelegramMessage(obj.header);
lastRun = DateTime.Now;
So how do we actually make the bot send alerts to the group? It's dead simple. Just remember to replace ##TOKEN## and ##GROUP_ID## with values we obtain in step 2.
private static string SendTelegramMessage(string message)
var request =
var resp = request.GetResponse();
var reader = new StreamReader(resp.GetResponseStream());
var content = reader.ReadToEnd();
the JSON object returned
"title": "Richland IT",
"text": "Alarm from the Applications Manager - [ EDI File Monitor is down ]"
It's really easy to create Telegram Bot and to use to send messages. If we read the API further, we should find that Telegram also provide WebHook. This way we can setup a web service to monitor the messages received by the bot, in real-time. Imagine the unlimited possibilities! Restart a server? Just send message to the bot. If we combine this with Raspberry Pi, Home automation is super achievable! But keep in mind to keep the token secured to prevent the unwanted!.
That's all folks. I hope it helps. Cheers!