Abandoning Pushbullet for Telegram Bot API

If you are already using Telegram, using the Telegram bot API will help you accomplish so much more. Lets start with simple notifications.

Pushbullet has long been my go-to app for getting notifications through various services. That was until I found out about Telegram Bot API, and boy, I was missing out. The simplicity of Telegram bot API quickly made me move most of my current notification pipelines to Telegram channels and bot. Here we’re going to discuss the benefits, creation, and simple usage of the Telegram Bot API.

Pushbullet has stagnated over the last year after it began its controversial premium tier. Even the dark theme was behind a subscription. I didn’t mind most of these as I was just using it as a dedicated app for notification from automated testing to IFTTT. But trying out Telegram bot API and how simple it blew me away. I was up and running within few minutes. No more authentication headers pushbullet wants. Just a private channel, a bot you control, and a simple web request and that’s it.

Benefits of Telegram Bot API

The benefits of using Telegram bot API were too many to continue using Pushbullet. As I’m already using Telegram, using a different app for notifications seemed unnecessary. There is also a ton more you can do with Telegram API. Like check emails, get stocks update, weather, and even get payments. Telegram discusses the possibilities here.

Here are the benefits I see from using Telegram bot API

  1. Crazy simple bot/API creation (from app or web)
  2. Can be implemented in any programming language
  3. Can programmatically interact with the bot
  4. Just needs a GET request to create notifications
  5. App with regular update (and dark mode)
  6. Can create unlimited private channels to categories your notifications
  7. Can mute channels so notifications don’t bother you
  8. No third-party logins
  9. And best of all. No subscriptions.

One of the thing I do miss is being able to create link notifications, which could be opened from the notification on Android. Notification mirroring is also one thing that I used before but have not recently.

Getting Started

To get started, you need a Telegram account. Then get in chat with the @BotFather. Send a /newbot command to the bot. Give it a name and a username that ends with ‘bot‘. Usernames without bot at the end aren’t valid. BotFather will then send you the API code for your bot.

Screenshot of steps involved in creating a telegram bot with @BotFather.
Steps for creating a telegram bot with @BotFather.

You might have a hard time finding a short name though, as most of them are already taken. Telegram also doesn’t release old usernames which makes it hard to get a proper named bot. But as it’s just a bot, it shouldn’t matter much, right? I still spent a while to get a short name which was available. The HTTP API token is the essential part about your bot. It’s required to control and send messages through the bot. After the bot is created, you can send it a message to it to get started.

Create a notification channel

Telegram offers you to create public and private channels. Private channels are great for keeping things to yourself without anyone else snooping. If you require additional participants, you can still send invitations to other users. You can create channel through the floating action bar on your Telegram app. Channel creation feature isn’t available on web app yet.

Using the bot for notification

The HTTP API really makes it easy to send and receive messages. You just need a single URL and edit three parts of it to accomplish this. To make the bot send and receive message, you have to start a chat with it. To make it send messages to channels, you have to add it as an Admin to your channel.

Example dummy URL for sending “Hello World” message to yourself.

https://api.telegram.org/bot1853935264:AAd4XtRsWSy7r3j7Sz1FdaS5EqT5sd2u65o/sendMessage?chat_id=@yourchannel&text=Hello+World

Parts you should edit with your credentials

https://api.telegram.org/bot + <API-KEY-TOKEN> + /sendMessage?chat_id= +<chatid> + &text= + <message>

For public channels, channel usernames can be used as they are easy to remember. As for private channels and users, one more step is required to gather their ID. It is done by checking the bot message log. The easiest way to do this is for the user to message the bot directly. For channels, add the bot as an admin and then send any message on the channel. Then check the bots message log in the following URL.

https://api.telegram.org/bot<API-KEY-TOKEN>/getUpdates

Screenshot of a json response from the bot API after a message is sent
JSON response from the bot API after a message is sent

The “id” is the chat_id of your user or channel.

You’ve got the API token and your chat_id where you want to send the notifications. Now you can use it to send notification to yourself from anywhere on the internet. From your automation script to Tasker plugin on your phone or others phone.

Here’s a simple python code to get you started:

from urllib import request, parse
token = '<your-http-api-token>'		#HTTP API token
chatid = '<your-chat-id>'			#userid/channel username
message = "Hello World"				#message
message = parse.quote_plus(message)	#encode message to behave properly

#combination of everything to an URL ready to be sent
notification = f'https://api.telegram.org/bot{token}/sendMessage?chat_id={chatid}&text={message}'

#send the http request
request.urlopen(notification)

Here’s how the code would look like with dummy API token and chat id

from urllib import request, parse
token = '1853935264:AAd4XtRsWSy7r3j7Sz1FdaS5EqT5sd2u65o'
message = "Hello World"
message = parse.quote_plus(message)

notification = f'https://api.telegram.org/bot{token}/sendMessage?chat_id={chatid}&text={message}'

request.urlopen(notification)

Finally

Hopefully this gets you started toward making your bot much more than just send notifications. There are already bots that can check your email, translate your conversations, play games, and moderate channels. You could even build a simple assistant to manage your to-do list or monitor your IoT devices at home or office. The possibilities only rests with your imagination and coding abilities.

Leave a Reply