by @mcknco

Server Notifications via Telegram

TL;DR: Telegram is perfect for push notifications to your phone.

A while ago my man @levelsio wrote up a terrific little indie hack on monitoring your server on your phone via push notifications to a paid, third-party iOS app called Pushover. His idea is still incredibly quick and useful, but the mechanism has now become obsolete.

There’s a free, bigger and better, cross-platform, desktop + mobile infrastructure you can use to send notifications called Telegram.

I’m really late to the game — I started using Telegram just a few weeks ago to get on wip.chat, but since that moment it’s taken over my modest maker life in a very significant way and now I’m in love!

Telegram

For all practical purposes, Telegram is basically WhatsApp with decent developer APIs that allow you to send messages via their app programatically. Since it’s so easy, I’ll outline how it works.

1. Create a Telegram Bot

Telegram has special user accounts controlled via API called bots. You’ll send messages via a new bot you create for yourself. After installing the Telegram client on desktop or mobile —

  1. Start a chat with @BotFather, a Telegram bot that creates bots.

  2. Send the /newbot command to @BotFather, give a bot name and handle and you’ll be returned a fresh, shiny new API token.

A quick chat with @BotFather for an offer I can't refuse.

The API token is one of two bits of information we’ll need to send messages from your code to your Telegram account.

2. Get Your Telegram ID

We need to grab your Telegram account’s unique ID number so we know where your bot should send its messages to. Here’s how.

  1. Start a chat session with your new Telegram bot and send it a message with some random text. I just wrote “hello”.

  2. Use your bot’s getUpdates API endpoint to download the message you just sent to it. The endpoint is a GET request to https://api.telegram.org/bot<API-TOKEN>/getUpdates. Use curl or even just point your browser at the address above.

  3. In the returned JSON, take a note of the id field. That’s where you’ll want to send messages you generate in your code.

Now you’re setup to send notifications. You just have to do it.

3. Send Notifications via API

The API endpoint for sending messages is again a GET request, this time to https://api.telegram.org/bot<API-TOKEN>/sendMessage?chat_id=<ID>&text=<MESSAGE>. This will look different in various languages, but it’s easy to accomplish in just about any. Here’s PHP.

function tg_msg($txt) {
    $token = "241035397:AcFBgqxMQv0xk0kX7yZeTAR-6FPOs";
    $query = ["chat_id" => "46966708", "text" => $txt];
    $url = "https://api.telegram.org/bot" . $token;
    $url .= "/sendMessage?" . http_build_query($query);

    $ch = curl_init($url);
    curl_exec($ch);
    curl_close($ch);
}

Now you can sprinkle your cron jobs and PHP scripts with random status updates, for example things like tg_msg("Server Reboot") or tg_msg("Failed to Fetch News"). Here’s my chat session.

Example updates from my emoji spewing VPS.

When everything’s fine, it pings me once a day to let me know it’s made a backup and that’s about it. When things go wrong, I’ll get messages regarding any number problems that my code causes.

It can easily be used in conjunction with Pieter’s original tail method to poll and send nginx log files over Telegram, and I’m pretty sure integrating with other services via Zapier wouldn’t be hard.

Dead simple and free.

If it’s new to you and you like it, let me know.

January 27, 2018, @mcknco

Want updates as I navigate my way through the emotional ups and downs of online solopreneurship? Get on my private email list. A few mails a year, max. No spam!

Have feedback?
Thanks for your message! We'll get back to you shortly.
A suggestion? Or a bug? Say hi!