Delivery Notification
Charly's Column – Telegram API
Our columnist Charly K¸hnast doesn't want to be out when a package delivery service is standing at his front door with some goodies. When the courier rings, he can have the Telegram API send a notification to his mobile phone.
I mainly find notifications on my mobile phone annoying and have disabled most of them. But things are different when something requires my attention at home. The classic example is the doorbell: I want to know when a delivery person is standing at my front door even if I'm flooding my noise-killing headset with electric guitar music. I already solved this problem back in 2016 with a Raspberry Pi and described the setup in this column [1].
With my earlier solution, notifications reached me as Twitter direct messages, mainly because I didn't want to install a notification app just for this purpose. Twitter is already installed on my phone and provides a useful programming interface. However, Twitter is increasingly setting up hurdles for access to the API. For example, you now need to describe in a small, free text essay the purpose of your proposed use of the API. This may be understandable from the supplier's point of view, but I would prefer things to be a little simpler.
This explains why I now use the Telegram API [2]. Among other things, I can create bots that can be used to do many funny things. On top of this, I can get the bot to send me messages. You can create a bot by starting a chat with BotFather in the Telegram app (Figure 1).
Start with the /newbot
command. BotFather asks you for a name and a username for your bot. In return, you are sent a numeric ID and an access token – that is, a string that you have to pass to the API to identify yourself. It could look like this, for example:
919445171: AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA
The first few digits are your bot's numeric ID, followed by a colon and the access token. Now you have to start a chat with your bot in the app or the web interface [3]. This chat also has a numeric ID, which you need to discover. The curl
command from Listing 1 does the trick.
Listing 1
Start Telegram Chat
$ curl -X POST https://api.telegram.org/bot919445171:AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA/getUpdates
Note that you need to prefix your bot's numeric ID with the bot
keyword. For the most part, the long answer is unimportant for the purposes of this article, but it does contain a string reading "id":930313820
– this is the chat ID. Now you have all the information you need.
If you now want to automatically send a message to the bot chat, using a Bash script for example, the curl
call from Listing 2 is all it takes. The phone vibrates and a message appears on the display (Figure 2). Now, I'll never miss my deliveries again.
Listing 2
Sending a Message to the Bot Chat
$ curl -X POST 'https://api.telegram.org/bot919445171:AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA/sendMessage?chat_id=930313820&text="Hello world!"'
Infos
- "The sys admin's daily grind: Charly's doorbell Pi" by Charly Kühnast, Linux Magazine , issue 186, May 2016, p. 62, http://www.linux-magazine.com/Issues/2016/186/Charly-s-Column-Doorbell-Pi/(language)/eng-US
- Telegram API: https://core.telegram.org/api
- Telegram web interface: https://web.telegram.org
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.